071 - 576 13 23

Retrieve Google Analytics Visits and PageViews with PHP

In 2009 we released a Google Analytics API client in PHP. It was the first library released in PHP. Six years later, this software is outdated. Google changed the API and it’s no longer of use.

As an alternative you can use GAPI (Github). Go to github.com/erebusnz/gapi-google-analytics-php-interface for instructions and to get the source code.

This is a basic example how you can use GAPI:


$ga=new gapi('XXXX@developer.gserviceaccount.com','oauthkeyfile.p12');


  echo'<strong>'.$result.'</strong><br />';
  echo'Pageviews: '.$result->getPageviews().' ';
  echo'Visits: '.$result->getVisits().'<br />';

echo'<p>Total pageviews: '.$ga->getPageviews().' total visits: '.$ga->getVisits().'</p>';