Google Analytics API PHP Class - Q&A May 13th 2009Google Analytics API PHP Class - Q&A May 13th 2009

Posted May 13th, 2009 in PHP (Updated May 15th, 2009)

I had some questions in an email about my Google Analytics API PHP class last night and have posted them here to help other people too.

For more information about my Google Analytics API PHP class and to download it, refer to The Google Analytics API and PHP: A series post which has a full list of posts about the class.

How do I get the account name with a profile_ID?

My analytics class has a function called load_accounts() which loads all the accounts the login has access to into an associative array which is indexed by the domain. I can easily enough make some modifications so it could be indexed in a different way, but for the moment the solution would be to loop through the data and compare the profile id you want with the values in the array.

An example entry from the array looks like this:

[www.electrictoolbox.com] => Array
    (
        [tableId] => ga:7426158
        [accountId] => 144582
        [accountName] => The Electric Toolbox
        [profileId] => 7426158
        [webPropertyId] => UA-144582-3
    )

And the code needed to find the account name for the profile id could look like so:

$my_profile_id = 7426158;
foreach($api->accounts as $array) {
    if($array['profileId'] == $my_profile_id) {
        $my_account_name = $array['accountName'];
        break;
    }
}
echo $my_account_name, "\n";

With my example data above it would echo "The Electric Toolbox".

Update May 15th 2009: It turns out that the data returned for the person who asked me this question is different than the format that I have it in and looks like this instead (note that I've substituted my profile name and domain for theirs in the example data below):

[The Electric Toolbox] => Array
       (
           [tableId] => ga:XXXXXXX
           [accountId] => XXXXXX
           [accountName] => www.electrictoolbox.com
           [profileId] => XXXXXX
           [webPropertyId] => UA-XXXXXX-1
       )

The code for getting the account name, which in this instance is the key name instead of one of the values of the subarray, is this:

$my_profile_id = 7426158;
foreach($api->accounts as $my_account_name => $array) {
   if($array['profileId'] == $my_profile_id) {
       break;
   }
}
echo $my_account_name, "\n";

How do I format timeOnSite - I get 1.6133192E7 returned?

The timeOnSite metric is the total amount of seconds spent on the site for all visitors for the given dimension and date range. If no dimension is specified then it's the time on site for all visitors for the date range. So it's going to be a big number if it's a busy site which is why you are seeing that particular format.

If you want to get the average time spent by each visitor, then do this:

$data = $api->data($id, '', 'ga:timeOnSite,ga:visits');
$average_time_on_site = $data['ga:timeOnSite'] / $data['ga:visits'];

Update May 15th 2009: You can get this easily formatted into hh:mm:ss format using this function by Jon Haworth.

How do I get "absolute unique visitors"?

Looking at the dimensions and references page in the documentation there doesn't appear to be a metric for this. There is ga:visitors and ga:visits but I don't see one for absolute unique. This is surprising given you can do it in the Analytics web interface.

Update May 15th 2009: The metric for absolute visitors is ga:visitors according to this post at the Google Analytics API group at Google Groups.

Related posts:

Comments

blog comments powered by Disqus