Google AnalyticsAPI代替或使用域名获取个人资料ID

时间:2014-08-22 21:43:20

标签: api google-analytics google-analytics-api

这是我从任何Google Analytics帐户打印域名的方式。如何(或使用域名)打印配置文件ID?

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>';

这一行是获取域名的功能:

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

我现在需要一些东西来获取多个域的配置文件ID。

提前致谢。

1 个答案:

答案 0 :(得分:2)

以下示例可帮助您打印特定帐户XXXX和属性UA-XXXX-Y的所有个人资料ID。

 /**
 * 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()");
}

您可以查看API documentation for view (profiles)以获取更详细的示例。

您可能还会发现使用account summaries api很有用。它提供了一种简单的方法来迭代所有级别的Google Analytics帐户 - &gt;属性 - &gt;查看(个人资料)

$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;
}