How to use Google's Analytics API in PHP using the GAPI interface

You’ve all seen those most popular/most read panels on websites, well here’s how to use the Google Analytics API to create one using PHP and GAPI (Google Analytics Php Interface). Not only are they useful, these most popular lists can provide an interesting insight into what your visitors like most about your site.

Please Note: The library and connection method that I use in this article are out of date. I have written a new guide that details how to use Google’s new PHP API interface with oAuth2, this is now the preferred method of retrieving data from the Analytics API. This article is still a useful introduction to the concepts however.

Using the Google Analytics API

You’ve most likely got a Google Analytics account that is fat with information about your visitors, so let’s extract information about your most popular pages from there. Google provides an analytics API that allows you to extract analytics information programatically. Much like the Google Analytics web interface you can access a large amount of data and filter it in many different ways via the Analytics API.

In this article you we are going to request content with the most unique page views for the last few days. I’m going to show you how to do this in PHP but it shouldn’t be too difficult to take the same idea and apply it to a different language.

The Google Analytics API has been around for a while now, so as you can imagine some lovely people have already written some nice interfaces that take care of the legwork of connecting to Google for you. We are going to be using an interface called GAPI (Google Analytics Php Interface), which is an easy to use PHP interface. To make things easy, to begin with we are going to create a simple PHP script that will be executed on the command line.

Create a new PHP script, call it ‘mostpopular.php’ for now and put the following code into it:

<?php
require 'gapi.class.php';

$gaUsername = '';
$gaPassword = '';
$profileId = '';
$dimensions = array('pagePath');
$metrics = array('uniquePageviews');
$sort = '-uniquePageviews';
$fromDate = date('Y-m-d', strtotime('-2 days'));
$toDate = date('Y-m-d');

$ga = new gapi($gaUsername, $gaPassword);
$mostPopular = $ga->requestReportData($profileId, $dimensions, $metrics, $sort, null, $fromDate, $toDate, 1, 10);

var_dump($mostPopular);
?>

As you can see you will need to enter your Google Analytics username and password into the script, you will also need to enter the profile ID for the particular site that you want to report on. Note that this is different to the tracker ID that you use on your site to log page views with Analytics. To get this ID go into your Google Analytics account and click on ‘Admin’ in the top right, then click on ‘Profile settings’. You should see a field named profile ID there, set this as the value of the $profileId variable, you should now be able to execute the script on the command line and see some output.

As you can see we have requested 10 pagepaths/urls from the last two days in descending order of unique page views. We request the page path so you can relate the data to your site, for instance you will most likely have a particular URL pattern for articles on your site so you can use the page path to figure out which article each Google API result relates too.

Consider this bit of code here:

foreach($mostPopular as $mostPopularEntry){
    $matches = array();
    $metrics = $mostPopularEntry->getMetrics();
    $dimensions = $mostPopularEntry->getDimensions();
    preg_match('/\/[a-zA-Z0-9\-]+\/(?P<id>\d+)$/', $dimensions['pagePath'], $matches);
    if (isset($matches['id'])) {
        // Look up the piece of content
    }
}

The idea here is to extract a meaningful piece of information from the URL that can be used to look up an object in a database, from this you can build a list of most popular objects (such as article, galleries, post or whatever) and display the list on your site.

You will most likely want to ensure that only certain pieces of content are returned by the Google Analytics API request, for instance you might want to only retrieve a list of most popular articles, ignoring things like the homepage, landing pages, contact pages and that sort of thing. To do this you need to apply a regex filter to the pagePath dimension.

Consider this modified example that adds in a filter:

<?php
require 'gapi.class.php';

$gaUsername = '';
$gaPassword = '';
$profileId = '';
$dimensions = array('pagePath');
$metrics = array('uniquePageviews');
$sort = '-uniquePageviews';
$filter = 'pagePath=~\/[a-zA-Z0-9\-]+\/\d+$';
$fromDate = date('Y-m-d', strtotime('-2 days'));
$toDate = date('Y-m-d');

$ga = new gapi($gaUsername, $gaPassword);
$mostPopular = $ga->requestReportData($profileId, $dimensions, $metrics, $sort, $filter, $fromDate, $toDate, 1, 10);

var_dump($mostPopular);
?>

We have introduced a bit of regex that will filter the page path and ensure that only results that follow a certain URL pattern will be returned.

This is just one small use of the Google Analytics API, you can request any dimension or metric and filter in any way that you want. The Google Analytics Query Explorer allows you to test and debug your queries to the API and is an invaluable tool.

References and further reading

GAPI (Google Analytics Php Interface)

Google Analytics Query Explorer