Selecting a Google Analytics API Profile ID

in Google Analytics API

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:

Google Analytics Account Hierarchy
Google Analytics Account Hierarchy

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:

Selecting a Google Analytics Account
Selecting a Google Analytics Account

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:

Selecting a Google Analytics Web Property
Selecting a Google Analytics Web Property

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:

Selecting a Google Analytics Profile
Selecting a Google Analytics Profile

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:

Account Hierarchy Properties
Account Hierarchy Properties

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:

require_once(dirname(__FILE__) . "/google-api-php-client/src/Google_Client.php");
require_once(dirname(__FILE__) . "/google-api-php-client/src/contrib/Google_AnalyticsService.php");
Importing the Library 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:

$clientId = "YOUR_CLIENT_ID";
$clientSecret = "YOUR_CLIENT_SECRET";
$oauthInfo = 'YOUR_OAUTH_PARAMETERS';
 
$client = new Google_Client();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setAccessToken($oauthInfo);
$client->setUseObjects(true);
$analytics = new Google_AnalyticsService($client);
Creating a Google Analytics API Service

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:

$result = $analytics->management_accounts->listManagementAccounts();
$accounts = $result->items;
foreach ($accounts as $account) {
    print "Found an account with an ID of {$account->id} and a name of {$account->name}\n"; 
}
Downloading Accounts from the GA API

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:

$accountId = $accounts[0]->id;
$result = $analytics->management_webproperties->listManagementWebproperties($accountId);
$webProperties = $result->items;
 
foreach ($webProperties as $webProperty) {
    print "Found a web property for the site {$webProperty->websiteUrl}, with an ID of {$webProperty->id} and a name of {$webProperty->name}\n"; 
}
Downloading Web Properties from the GA API

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:

$webPropertyId = $webProperties[0]->id;
$result = $analytics->management_profiles->listManagementProfiles($accountId, $webPropertyId);
$profiles = $result->items;
 
foreach ($profiles as $profile) {
    print "Found a profile with an ID of {$profile->id} and a name of {$profile->name}\n"; 
}
Downloading Profiles from the GA API

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:

$profileId = $profiles[0]->id;
print "$profileId\n";
Extracting the Profile ID

Full Sample Code

Here's the complete example script described above:

require_once(dirname(__FILE__) . "/google-api-php-client/src/Google_Client.php");
require_once(dirname(__FILE__) . "/google-api-php-client/src/contrib/Google_AnalyticsService.php");
 
$clientId = "YOUR_CLIENT_ID";
$clientSecret = "YOUR_CLIENT_SECRET";
$oauthInfo = 'YOUR_OAUTH_PARAMETERS';
 
$client = new Google_Client();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setAccessToken($oauthInfo);
$client->setUseObjects(true);
$analytics = new Google_AnalyticsService($client);
 
$result = $analytics->management_accounts->listManagementAccounts();
$accounts = $result->items;
foreach ($accounts as $account) {
    print "Found an account with an ID of {$account->id} and a name of {$account->name}\n"; 
}
 
$accountId = $accounts[0]->id;
$result = $analytics->management_webproperties->listManagementWebproperties($accountId);
$webProperties = $result->items;
 
foreach ($webProperties as $webProperty) {
    print "Found a web property for the site {$webProperty->websiteUrl}, with an ID of {$webProperty->id} and a name of {$webProperty->name}\n"; 
}
 
$webPropertyId = $webProperties[0]->id;
$result = $analytics->management_profiles->listManagementProfiles($accountId, $webPropertyId);
$profiles = $result->items;
 
foreach ($profiles as $profile) {
    print "Found a profile with an ID of {$profile->id} and a name of {$profile->name}\n"; 
}
 
$profileId = $profiles[0]->id;
print "$profileId\n";
Downloading the Account Structure from Google Analytics

Comments

I'm keen to get feedback on my posts, so if you have any questions or comments, then please send me a message and I'll be happy to help.