John Smith
John Smith

Reputation: 11

Google Analytics API get Profile ID instead or with Domains

This is how I print Domains from any Google Analytics account. How do I print Profile IDs instead (or with the Domains)?

global $_params, $output_title, $output_body;
$output_title = 'Adwords';
$output_nav = '<li><a href="'.$scriptUri.'?logout">Logout</a></li>'."\n";
$output_body = '<h1>Google Adwords Access demo</h1>
                <p>The following domains are in your Google Adwords account</p><ul>';
$props = $service->management_webproperties->listManagementWebproperties("~all");
foreach($props['items'] as $item) {
    $output_body .= sprintf('<li>%1$s</li>', $item['name']);
}
$output_body .= '</ul>';

This line is the function that gets the Domains:

$props = $service->management_webproperties->listManagementWebproperties("~all");

I need something to get Profile IDs for multiple Domains now.

Thanks in advance.

Upvotes: 1

Views: 1506

Answers (1)

Matt
Matt

Reputation: 5168

This example below should help you print all the profile ids for a particular account XXXX and property UA-XXXX-Y.

 /**
 * This example requests a list of views (profiles) for the authorized user.
 */
$profiles = $analytics->management_profiles->listManagementProfiles('XXXX', 'UA-XXXX-Y');

foreach ($profiles->getItems() as $profile) {
    print("view (profile) id: $profile->getId()");
}

You can checkout the API documentation for view (profiles) for more detailed examples.

You may also find it useful to use the account summaries api. It provides an easy way to iterate through all levels of the Google Analytics Accounts -> Properties -> view (profiles)

$accounts = $analytics->management_accountSummaries
      ->listManagementAccountSummaries();

foreach ($accounts->getItems() as $account) {
  $html = <<<HTML
<pre>
Account id   = {$account->getId()}
Account kind = {$account->getKind()}
Account name = {$account->getName()}
HTML;

  // Iterate through each Property.
  foreach ($account->getWebProperties() as $property) {
  $html .= <<<HTML
Property id          = {$property->getId()}
Property kind        = {$property->getKind()}
Property name        = {$property->getName()}
Internal property id = {$property->getInternalWebPropertyId()}
Property level       = {$property->getLevel()}
Property URL         = {$property->getWebsiteUrl()}
HTML;

    // Iterate through each view (profile).
    foreach ($property->getProfiles() as $profile) {
      $html .= <<<HTML
Profile id   = {$profile->getId()}
Profile kind = {$profile->getKind()}
Profile name = {$profile->getName()}
Profile type = {$profile->getType()}
HTML;
    }
  }
  $html .= '</pre>';
  print $html;
}

Upvotes: 3

Related Questions