Home / PHP / Load the Google Analytics API accounts list with PHP

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.

     <title type="text">www.electrictoolbox.com</title>
     <link rel="alternate" type="text/html" href="http://www.google.com/analytics" />
     <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" />

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();
 $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');
 case 'ga:accountName':
 $this->accounts[$title]['accountName'] = $property->getAttribute('value');
 case 'ga:webPropertyId':
 $this->accounts[$title]['webPropertyId'] = $property->getAttribute('value');
 case 'ga:profileId':
 $this->accounts[$title]['profileId'] = $property->getAttribute('value');

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:

 [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.