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.
Analytics hierarchy
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:
- Account
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. - Webproperty
- Profil
A profile is the last level. There you will find data.
We can step through this hierarchy the following way:
//get Accounts $accounts = $analytics->management_accounts->listManagementAccounts(); $a_items = $accounts->getItems(); echo count($a_items)." Accounts<br>"; if(count($a_items)!=0) { 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>"; if(count($w_items)!=0) { foreach($w_items as $webproperty) { echo '*Webproperty ID: '.$webproperty->getId().'<br>'; echo '*Webproperty Name: '.$webproperty->getName().'<br>'; //get profiles $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!