Archive for April, 2012

Page 1 of 1

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.

Why I Don’t Like Google (All That Much)

I had the opportunity over the past couple of days to experience the automated Google suspension hammer for one of their services. Without warning of any kind. Their system made a decision based on algorithms without any knowledge of the circumstances surrounding the situation, and continued to insist on its accuracy even after contacting a live person–until we could show concrete proof of our position.

Why is the onus on me to prove I’m doing something legitimate when their system makes a mistake, and why can they never own up to it? The best part, after resolving the problem, their email says they can’t guarantee the system won’t make the same mistake again. Huh?

Now they want me to complete a survey. Google is terrible at dealing with customers. Full stop.

I love certain Google products, so this isn’t to say the company is awful, but it’s pretty obvious how they feel about their customer base; they could not care less, as they’re mostly interested in advertising to us.

Rant over. Get back to work. Also, click that Google+ sharing button down there.