Load the Google Analytics API accounts list with PHP

After Google made an API available for Google Analytics the other day I started a series of posts about how to get analytics data with PHP and CURL. Today’s post extends the class I’ve already posted by adding a function to get the list of accounts you have access to. This enables you to get the ID needed to pass to further API calls. Tomorrow’s post will add a common calling function so you can get any data you need.

Calling https://www.google.com/analytics/feeds/accounts/default returns a list of accounts in XML format. A snippet from the returned XML is shown below, showing just the data returned for the website profile for this blog.

<entry>
 <id>http://www.google.com/analytics/feeds/accounts/ga:7426158</id>
 <updated>2008-02-27T22:52:58.000-08:00</updated>
     <title type="text">www.electrictoolbox.com</title>
     <link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
     <dxp:tableId>ga:7426158</dxp:tableId>
     <dxp:property name="ga:accountId" value="144582" />
     <dxp:property name="ga:accountName" value="The Electric Toolbox" />
     <dxp:property name="ga:profileId" value="7426158" />
     <dxp:property name="ga:webPropertyId" value="UA-144582-3" />
 </entry>
 

The following method (for the class I’ve already posted) parses the data above for each profile and adds it to an associative array (declared as “public $accounts;” in the class) where the <title> i.e. the website’s domain name, is used as the index. 

public function load_accounts() {
 
 $xml = $this->call('https://www.google.com/analytics/feeds/accounts/default');
 
 $dom = new DOMDocument();
 $dom->loadXML($xml);
 
 $entries = $dom->getElementsByTagName('entry');
 $this->accounts = array();
 foreach($entries as $entry) {
 
 $titles = $entry->getElementsByTagName('title');
 $title = $titles->item(0)->nodeValue;
 
 $this->accounts[$title] = array();
 
 $tableIds = $entry->getElementsByTagName('tableId');
 $this->accounts[$title]['tableId'] = $tableIds->item(0)->nodeValue;
 
 $properties = $entry->getElementsByTagName('property');
 foreach($properties as $property) {
 switch($property->getAttribute('name')) {
 case 'ga:accountId':
 $this->accounts[$title]['accountId'] = $property->getAttribute('value');
 break;
 case 'ga:accountName':
 $this->accounts[$title]['accountName'] = $property->getAttribute('value');
 break;
 case 'ga:webPropertyId':
 $this->accounts[$title]['webPropertyId'] = $property->getAttribute('value');
 break;
 case 'ga:profileId':
 $this->accounts[$title]['profileId'] = $property->getAttribute('value');
 break;
 }
 }
 
 }
 
 }
 

The resulting output from the array would look similar to the following. In the example below I’ve only included the profiles for two of my accounts:

print_r($api->accounts);
 
Array
 (
 [www.electrictoolbox.com] => Array
 (
 [tableId] => ga:7426158
 [accountId] => 144582
 [accountName] => The Electric Toolbox
 [profileId] => 7426158
 [webPropertyId] => UA-144582-3
 )
 
 
 [www.electricbookmarks.com] => Array
 (
 [tableId] => ga:13502852
 [accountId] => 144582
 [accountName] => The Electric Toolbox
 [profileId] => 13502852
 [webPropertyId] => UA-144582-11
 )
 
 )
 

Tomorrow I’ll complete the class by adding a ->data() method which accepts the profile id, dimension, metric, sort order, start and end dates and maximum data to return counts and will provide the full class in a downloadable file.

In posts later this week and next week I’ll some other useful functions to the class for easy collection of data. Having this API is certainly going to make my work easier/faster at the start of each month collating data for the previous month.

Update April 28th 2009: I’ve started a series about this and created a PHP class. The full class, which I will continue to update over the next few days, can be downloaded here.

Categories PHP

PHP Caching Headers

Yesterday I looked at how to control browser caching with Apache’s mod_expires and today look at how to set the caching/expiry time with headers in PHP to either make sure the resulting data is never cached by the browser, or is cached for a set amount of time.

Categories PHP

Multiple variable assignment with PHP

This post shows how it is possible to assign multiple variables with the same value with PHP. This can be useful if initializing multiple variables with the same initial value or if needing to make multiple copies of a value and then manipulate each separately.

Categories PHP

Logging errors with PHP

PHP has several settings for logging errors; in this post I will look at the log_errors and error_log settings which control whether errors should be logged and where to.

Categories PHP

PHP command line syntax checking

The PHP CLI (command line interface) allows basic syntax checking of a PHP file. I’ll show the basic usage of this in this post and then a couple of ways to check all files in a directory, and all files recursively down the directory tree as posted by a couple of people on Twitter.

Categories PHP