Get the Google Analytics profile id from the accounts list

Just a quick post about an easy way to get the profile id for a Google Analytics account using my PHP Class. The ->load_accounts() function loads the accounts into an associative array which you can then access from ->accounts. The profile id is stored in there.

Here’s a code snippet illustrating this below, where we want to access data for the www.electrictoolbox.com profile:

$api = new analytics_api();
 if($api->login($login, $password)) {
 $api->load_accounts();
 $profile_id = $api->accounts['www.electrictoolbox.com']['tableId'];
 $data = $api->data($profile_id, ...);
 }
 

Alternatively you don’t need to store it in a separate variable and could do it like this instead:

$api = new analytics_api();
 if($api->login($login, $password)) {
 $api->load_accounts();
 $data = $api->data($api->accounts['www.electrictoolbox.com']['tableId'], ...);
 }
 

This uses my PHP class which can be downloaded here. More info and other posts about this here.

Categories PHP

Make jQuery Facebox dialog modal

The jQuery Facebox lightbox can be closed by clicking the close button, clicking anywhere outside the dialog or using the escape key. This post shows how to make the dialog modal so that the only way to close it is by clicking the close button. You could alternatively add a link etc into the content of the dialog which would close it, link to another page etc.

PHP class for using the Google Analytics API

Just quickly following up my post from earlier on today where I looked at how to log into the Google Analytics API using PHP and CURL using Username/Password Authentication I wrapped the login code up into a class with methods for logging in and then calling API functions. I’ll look at parsing the XML result data at a later stage.

Update April 28th 2009: I’ve started a series about this. The full class, which I will continue to update over the next few days, can be downloaded here. The class presented below is an earlier version of it. Original post continues…

Here’s the class:

class analytics_api {
 
 protected $auth;
 
 public function login($email, $password) {
 
 $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_POST, true);
        
         $data = array(
             'accountType' => 'GOOGLE',
             'Email' => $email,
             'Passwd' => $password,
             'service' => 'analytics',
             'source' => ''
         );
        
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         $output = curl_exec($ch);
         $info = curl_getinfo($ch);
         curl_close($ch);
        
         $this->auth = '';
         if($info['http_code'] == 200) {
             preg_match('/Auth=(.*)/', $output, $matches);
             if(isset($matches[1])) {
                 $this->auth = $matches[1];
             }
         }
        
         return $this->auth != '';
    
     }
    
     public function call($url) {
 
         $headers = array("Authorization: GoogleLogin auth=$this->auth");
        
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         $output = curl_exec($ch);
         $info = curl_getinfo($ch);
         curl_close($ch);
 
         if($info['http_code'] == 200) {
             return $output;
         }
         else {
             return false;
         }
        
     }
    
 }
 

And here’s an example using it, calling https://www.google.com/analytics/feeds/accounts/default which gets a list of all the accounts that are available to view data for.

$api = new analytics_api();
 if($api->login('email@example.com', 'my-secret-password')) {
 $xml = $api->call('https://www.google.com/analytics/feeds/accounts/default');
 // ... process the xml ...
 }
 else {
 // login failed
 }
 

The class could easily be extended to parse the XML for common functions and have a function for getting the list of accounts into an array (or similar) and so on for other functions.

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

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

Get the ID for the Google Analytics API IDS parameter

Google announced an API for the Google Analytics product last week and I posted the start of a PHP class for using the API on Thursday and will post extensions to the class for getting data from the XML this week. In order to get data you need to know the ID for the Google Analytics Account Profile and this post looks at one way to get it without needing any programming code.

Using the Analytics Web Interface

Log into Google Analytics. The table in the main section of the welcome area has a list of your accounts as shown in the example screenshot below. (I have a number of accounts under my login but have Photoshopped them out). Click the account name you want to get the ID for. Where to click is highlighted with the red arrow in the screenshot below.

select the google analytics account

Next you’ll see a list of profiles for the account. Click “View Report” next to the profile you want the ID for. Note that the ID is not the same as the UA-XXXX-XX style code which is shown on this page and what is in the Javascript code that it added to your web pages. Again, the red arrow shows what to click (and I’ve again Photoshopped out the other profiles under this account).

select the analytics profile

Now that you’re on the account profile page look in the browser address bar and you can see the ID there. I’ve highlighted the ID with a red box in the screenshot below. The ID is 7426158 in this example.

get the id from the browser address bar

API Call

The API call to https://www.google.com/analytics/feeds/accounts/default will return a list of all account IDs you have access to with your login and password and is another way to get the IDs. I will show how to do this tomorrow.

jQuery Facebox: setting the width

I’ve been looking at the jQuery Facebox plugin recently which is a Facebook style lightbox for loading remote content, images, local content etc. In this post I look at how to change the width of the Facebox dialog with CSS or programatically using Javascript.