0
votes

Can I use google analytics API to get information about visitors session?

I can get his clientId by calling ga.getAll()[0].get('clientId'); on client side. I can see his session data on the GA-panel (visit time, platform, source, number of sessions, duration, session list, etc.) Can I transfer this single user information into a database on my site via API (PHP)? The goal is to see little analytics by each visitor directly on the administrative area of the site, not ga pages. Is there docs or examples or just advice how to implement it?

1

1 Answers

0
votes

If you can find a way of showing the information you are looking for in the Google analytics website then you could extract it via the api. Just remember Google analytics data is not linked to a specific user in a way that you can see it.

There is no dimension for Clietid or userid. So unless you are sending a custom dimension with every hit with your internal user id your not going to be able to see this information.

I recommend the google api php clinet library if you would like to try to do this with php.

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2015-06-15");
$dateRange->setEndDate("2015-06-30");

// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");

//Create the Dimensions object.
$browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:browser");

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("XXXX");
$request->setDateRanges($dateRange);
$request->setDimensions(array($browser));
$request->setMetrics(array($sessions));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analyticsreporting->reports->batchGet( $body );