Home » Php » Using Google Analytics API with PHP

Using Google Analytics API with PHP

Posted by: admin July 12, 2020 Leave a comment


I am using the Google Analytics PHP class to get data from Google Analytics.

I would like to get a report of “Bounce Rate” For “Top Contnet”.

The thing is I am not familiar with the terminology.

When I am trying to get a “content” report or “topcontent” or “top_content” it says that there in no such metric. I simply don’t know the right expressions.

Does anyone know where can I find a list of all expressions? metrics & dimensions?


How to&Answers:

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 protected]','password');

The Google Analytics Export API has a data feed query explorer that should help you out considerably when using GAPI:

Also, here’s a list of all available dimensions and metrics you can pull from the API:

Definitely read over the GAPI documentation:


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'] = '';