Interacting With the Google Analytics Core Reporting API

I needed a way for my clients to access analytics data that for a variety of reasons didn’t need or have full access to Google Analytics data through the traditional web app. I found a simple, yet soon to be deprecated, solution to my problem–the Google Analytics Core Reporting API.

There are a couple of libraries that an individual can use, depending on the language you’re working in, and since my expertise is in php, I chose to use the Google Analytics PHP Interface. This happens to interact with the older v2.4 API; unfortunately, I didn’t realize this until I already built the thing…moving on.

Start Me Up

OK, so let’s just assume you already have a Google Analytics account, because frankly why else would you need something like this? So, let’s define a few things, include the gapi class and fill in our information to connect to the core reporting API.

define('ga_email','');     // GA Email
define('ga_password','elkkdafasff');     // 2-part authorization password
define('profile_id','2345678');          // Analytics profile ID

require_once 'gapi/gapi.class.php';
$ga = new gapi(ga_email,ga_password);

So now we’ve got an object to work with and we can start grabbing some data for our report.

With the core reporting api, you have certain metrics that you can look at depending on the dimension you’re interested in. To find exactly what metrics go with what dimensions, browse the Dimensions and Metrics Reference.

I already know that I’m interested in seeing the visits, pageviews, pages/visit, and bounce rate of all traffic sources (referrals, search engines, etc.). So, using the object we’ve already created I can define the dimensions and metrics I’m interested in, sort it by greatest number of visits, and limit my results to 50. You’ll also see that you can choose a date range. For this example I’m not providing one, so by default it will show the last month’s worth of data.

$dimensions  = array('source');
$metrics     = array('visits','pageviews','bounces');
$sort_metric = '-visits';

It is also possible to filter your data, so that if you wanted to see only referrals as a source, then you could replace $filter=null with $filter='medium==referral'. For a quick reference of what you might be able to filter, take a look at a list of common queries. Really just explore the documentation there in depth.

So we’ve got our data, now let’s put it in a format that we can browse:

      <th>Bounce Rate</th>
  $i = 1;
  foreach($ga->getResults() as $result):
      <td><?php echo $i; ?>.</td>
      <td><?php echo $result; ?></td>
      <td><?php echo number_format($result->getVisits()); ?></td>
      <td><?php echo number_format($result->getPageviews()); ?></td>
      <td><?php echo number_format(($result->getPageviews()/$result->getVisits()),2); ?></td>
      <td><?php echo number_format(($result->getBounces()/$result->getVisits())*100,2); ?>%</td>

How easy was that?

Utilizing that as a foundation, I was able to turn it into a tool for clients, or for myself, making it easy to get a quick look at stats. If you’re at all interested in using it yourself, let me know in the comments.