This post explains how to use the Google Analytics Management API to obtain the ID of an Analytics profile. Before you start working with the various Analytics APIs, such as the Core Reporting API or the Cost Data Upload, you need to find the ID of the profile that you want to target. I will begin by explaining the structure of Analytics accounts, then present code to download each level of the structure and extract the name and id of each entity within it.
GA Account Structure
The following diagram shows how Analytics accounts are organized within a Google Account login:
At the highest level of the hierarchy is a Google Account: This is the entrance point to the Google services that you use, and is associated with security credentials, such as your username/password or OAuth access tokens.
A Google Account can have zero or more Analytics accounts associate with it; you are allowed more than one, which enables you to manage other people's accounts with the same login credentials. When you have many accounts available to you, you can select one from the drop-down list in the top left corner of the Analytics interface:
The next level down in the hierarchy is the Web Property, which is a website that you are monitoring within an Analytics account. Each Analytics account can have multiple web properties within it, and you can view them when you expand an account:
A web property is identified using a Tracking ID, which you should be familiar with if you've installed the tracking code before: The Tracking ID is the
UA-XXXXXXX-Y code that you pass to the
_setAccount() function. The
XXXXXXX part identifies the Analytics account, and the
Y part is incremented each time you add a new property.
Finally, Google Analytics allows you to create multiple Profiles per web property. Profiles offer more flexibility because you can use them to define different settings, such as Goals and Filters, for the same website without requiring additional web properties and tracking code. You select a profile to start viewing a specific set of reports:
GA Account Attributes
Each level of the account hierarchy has properties that you can access via the Management API, but for this discussion I'm just going to focus on the ones you'll need to traverse the structure; you can consult the reference documentation to get a complete list. Here's the entities again with their attributes displayed:
Each one has an ID that you can use to select it, and a name that you can use to display to users of your application; web properties and profiles have an additional website URL that also can be used for display.
The API documentation specifies that the IDs are
String types, but you can use them all as
Long types, apart from the web property ID that contains the
UA-XXXXXXX-Y code used in the tracking snippet.
I've also added a property, called
oauthInfo, to the Google Account that isn't part of the data returned from the Management API, but you'll need to store it for authentication.
Example Code for Downloading GA Account Hierarchies
Now that you know the structure of a GA account, you can write some code to query each level of the hierarchy. I'm going to present the examples using PHP, so you first need to download the latest Client Library for PHP and extract it into the root of your project. You should then be able to import it into a script:
This is importing the
Google_Client class, which is the base class for accessing the other APIs, and the
Google_AnalyticsService class to make calls to the Analytics API. To instantiate the
Google_AnalyticsService, you need to pass it a configured instance of
Google_Client. Here's some code that does that:
To generate the
$oauthInfo JSON object, you need to follow this procedure, then you can pass it to the
Google_Client using the
setAccessToken() method. You also need to set your OAuth client ID and secret each time you create a
Google_Client, so it can refresh your access token if it's expired. Finally, note that I'm using the
setUseObjects() method to specify that I want the
Google_AnalyticsService to return its results as objects instead of associative arrays.
Listing Accounts, Web Properties, and Profiles
With the setup out-of-the-way, you can use the following code to download a list of accounts:
This is using the
listManagementAccounts() method of the
management_accounts service, which returns an object with some header data, such as paging information, and an array of accounts in the
items property. The code extracts the accounts into a variable called
$accounts, then loops through each account and prints out its id and name.
You'll probably want to build some form of UI to select an account, but for the purpose of this example, I'm just going to take the ID of the first one and list its web properties:
This is working in a similar fashion to the accounts example, but using the
listManagementWebproperties() method of the
management_webproperties service. This method requires an account ID, which I've extracted from the first account of in the
$accounts array. I'm printing out the ID and name of each web property, in the same way as before, and I'm also displaying the URL of the site being tracked (which, by default, is the same as the property name, but the name can be modified).
You can get the profiles for a web property using very similar code:
This passes a web property ID and its account ID to the
listManagementProfiles() method of the
management_profiles service, and processes the resulting profiles in the same way as before. You can now store the ID of a profile, ready for further exploration of the Analytics API:
Full Sample Code
Here's the complete example script described above: