【发布时间】:2015-07-29 20:02:19
【问题描述】:
我正在寻找将过滤器附加到 Google Analytics(分析)配置文件的示例代码。我已将过滤器添加到帐户中,效果很好。但我找不到将其附加到某个配置文件的代码。
我查看了php api客户端的源码,看到了management_profileLinkFilter,但我不明白我需要哪些变量。
【问题讨论】:
标签: php google-analytics-api google-api-php-client
我正在寻找将过滤器附加到 Google Analytics(分析)配置文件的示例代码。我已将过滤器添加到帐户中,效果很好。但我找不到将其附加到某个配置文件的代码。
我查看了php api客户端的源码,看到了management_profileLinkFilter,但我不明白我需要哪些变量。
【问题讨论】:
标签: php google-analytics-api google-api-php-client
经过几个小时的测试、反复试验,终于成功了!
首先,对范围使用 ANALYTICS 和 ANALYTICS_EDIT。然后使用以下代码:
// Construct the filter expression object.
$details = new Google_Service_Analytics_FilterExpression();
$details->setField("GEO_DOMAIN");
$details->setMatchType("EQUAL");
$details->setExpressionValue("example.com");
$details->setCaseSensitive(false);
// Construct the filter and set the details.
$filter = new Google_Service_Analytics_Filter();
$filter->setName("Exclude example.com");
$filter->setType("EXCLUDE");
$filter->setExcludeDetails($details);
$filterResult = $analytics->management_filters->insert($accountId, $filter);
// Construct the filter reference.
$filterRef = new Google_Service_Analytics_FilterRef();
$filterRef->setAccountId($accountId);
$filterRef->setId($filterResult->getId());
// Construct the body of the request.
$body = new Google_Service_Analytics_ProfileFilterLink();
$body->setFilterRef($filterRef);
$analytics->management_profileFilterLinks->insert($accountId, $propertyId, $profileId, $body);
【讨论】: