Google Analytics API – Part 2
Now its time to get data from Analytics API. We already coded a login procedure and set up an AnalyticsService object. Now we can use it to get data that you normally view with Googles Analytics online tool.
Before we will get data, we should get an idea oh the hierarchy of an Analytic account. This is important when you want to request data.
An account is divided in three levels:
an account is represented as an unique Analytics ID. This ID exists, because with it you can make accounts where you can control more than one Analytics account. Normally you will only have one Analytics ID for each Google account.
A profile is the last level. There you will find data.
We can step through this hierarchy the following way:
$accounts = $analytics->management_accounts->listManagementAccounts();
$a_items = $accounts->getItems();
echo count($a_items)." Accounts<br>";
foreach($a_items as $account)
echo 'Account ID: '.$account->getID().'<br>';
echo 'Account Name: '.$account->getName().'<br>';
//get web properties
$webProperties = $analytics->management_webproperties->listManagementWebproperties($account->getID());
$w_items = $webProperties->getItems();
echo count($w_items)." Webproperties<br>";
foreach($w_items as $webproperty)
echo '*Webproperty ID: '.$webproperty->getId().'<br>';
echo '*Webproperty Name: '.$webproperty->getName().'<br>';
$profiles = $analytics->management_profiles->listManagementProfiles($account->getID(), $webproperty->getId());
$p_items = $profiles->getItems();
//get analytics data
When you get to the profile level, you can request data for this profile!