Using Google Analytics API with PHP Using Google Analytics API with PHP php php

Using Google Analytics API with PHP


Top content isn't a metric, it's just a list of the pages on your site with the highest number of page views.

The metric you're looking for is 'entranceBounceRate' and the dimension is 'pagePath'. You want to get the bounce rate for the top X most visited pages on your site, so you'll want to limit your results and sort the results by '-pageviews' (pageviews descending).

If you want to get the bounce rate for the top 10 most viewed pages on your site, your query should look like this:

$ga = new gapi('email@yourdomain.com','password');$ga->requestReportData(145141242,array('pagePath'),array('entranceBounceRate','pageviews'),array('-visits'),null,null,null,10);

The Google Analytics Export API has a data feed query explorer that should help you out considerably when using GAPI:http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html

Also, here's a list of all available dimensions and metrics you can pull from the API:http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html

Definitely read over the GAPI documentation:http://code.google.com/p/gapi-google-analytics-php-interface/wiki/GAPIDocumentation


If you would like to get the global Bounce Rate for the last 30days (by default), here is how. Very simple once you know it.

//Check Bounce Rate for the last 30 days$ga = new gapi(ga_email, ga_password);$ga->requestReportData(145141242, NULL ,array('bounces', 'visits'));$data = round(($ga->getBounces() / $ga->getVisits()) * 100) . "%";

Note that the GAPI has a bug, they mention the dimension parameter is optional (2nd parameter) but it's not. You have to open the gapi.class.php file and patch line 128 with this:

  //Patch bug to make 2nd parameter optional  if( !empty($dimensions) ) {      $parameters['dimensions'] = 'ga:'.$dimensions;  } else {      $parameters['dimensions'] = '';  }