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.