Google APIs Client Library for PHP

The Google API Client Library enables you to work with Google APIs such as Analytics, Adsense, Google+, Calendar, Moderator, Tasks, or Latitude on your server. The PHP quickstart for service accounts. More info can be found on the Google API discussing group.

Google Analytics Core Reporting API

Inside the /examples/analytics/simple.php file you’ll find some example functions which allow you to access a list of web properties (that’s the list of sites you have in your account), a list of accounts, a list of all the advanced segments you’ve created and their settings and a list of all your goals.

The data are returned as PHP arrays, which means they’re easy to manipulate from the server side, or directly within a templating engine like Smarty, if you use one. This is much better than the old method of using multiple APIs and the data are easier to handle.

You can access your web properties array by using the following code:

$props = $service->management_webproperties->listManagementWebproperties("~all");
You can access your accounts array by using the following code:
$accounts = $service->management_accounts->listManagementAccounts();

You can access your advanced segments array by using the following code:

$segments = $service->management_segments->listManagementSegments();

You can access your advanced segments array by using the following code:

$goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");

When it comes to running Google Analytics queries against your data Google requires you to use what it describes in the code comments as the “analytics profile ID”. To obtain it you currently have to go to the Google Analytics interface and look at the URL of the page when logged into the desired account. In version 5 of GA, you’ll find the required code after the “p”.

Running API queries on your GA data

Queries are running using the get() function which is part of the ga method in the apiAnalyticsService.php class. This is called using the following code:

$data = $service->data_ga->get($ids,$start_date,$end_date,$metrics,$optParams);

The get() function requires five things:

  • $ids contains the ID of the profile you extracted from the URL of your account (ie ga:123456789
  • $start_date is a date in YYYY-MM-DD format
  • $end_date is a date in YYYY-MM-DD format
  • $metrics is a comma-separated list of metrics (ie ga:visits,ga:pageviews)
  • $optParams is an array, which I’ll explain below

The $optParams array has six possible keys:

  • ‘max-results’ => is an integer containing the maximum number of results to return
  • ‘sort’ => is a comma-separated list of dimensions or metrics to sort by
  • ‘dimensions’ => is a comma-separated list of dimensions to use
  • ‘start-index’ => is an integer used to index the first item returned, which is used for paginating large data sets
  • ‘segment’ => is the analytics advanced segment to apply to the data
  • ‘filters’ => is a comma-separated list of metric or dimension filters to apply

$optParams = array(‘dimensions’ => $dimensions, ‘filters’ => $filters, ‘segment’ => $segment, ‘sort’ => $sort, ‘max-results’ => $maxresults, ‘start-index’ => $startindex);

Here’s a simple example which retrieves visits and pageviews by browser:

$ids = "ga:123456789";
$start_date = "2011-01-01";
$end_date = "2011-11-30";
$metrics = "ga:visits,ga:pageviews";
$dimensions = "ga:browser";
$optParams = array('dimensions' => $dimensions);
$data = $service->data_ga->get($ids,$start_date,$end_date,$metrics,$optParams);

Sorting data

To sort data you need to add the sort key to the $optParams array and provide one or more dimensions or metrics to sort by. To sort by ascending pageviews, for example, you’d add ‘sort’ => ‘ga:pageviews’ to the array. This would return data with the lowest number of pageviews first. To achieve a descending sort, in which data is returned from the highest to lowest numbers, you prefix the ga: part with a hyphen. For example, ‘sort’ => ‘-ga:pageviews’. You can combine multiple sort dimensions or metrics by separating them with commas like this: -ga:pageviews,-ga:visits and put them in different orders to give certain items precedence.

Filtering data

Adding filters is much like adding dimensions, in that you just provide a comma-separated list of filters with your chosen operators or regular expressions and add them to the $optParams array using the key ‘filters’ => $filters, with the filters in $filters. Filters are divided into metric filters and dimension filters, as they achieve slightly different things. The metric filters work against integer values to handle things like less than and greater than, while the dimension filters work against strings using regular expressions and operators which identify a substring.

Metric filters

Leave a Reply