过滤来自 Google Analytics Reporting API 的结果 [英] Filtering results from Google Analytics Reporting API

查看:22
本文介绍了过滤来自 Google Analytics Reporting API 的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用报告 API(版本 4)和 PHP 客户端库成功地从 Google Analytics 下载结果.但我还没有弄清楚如何正确过滤这些结果.

I am successfully downloading results from Google Analytics using the reporting API (version 4), with the PHP client library. But I have not figured out how to correctly filter these results.

我看到这将如何通过 cURL 工作,但不是通过客户端库.翻了翻客户端库代码,有一个类方法:

I see how this would work via cURL, but not through the client library. I looked through the client library code, and there is a class method:

apiclient-services/Google/Service/AnalyticsReporting/ReportRequest.php:
public function setMetricFilterClauses($metricFilterClauses)

我没有看到任何文档或相关 get 方法的任何用法:

I do not see any documentation or any usage of the associated get method:

public function getMetricFilterClauses()

是否有通过 PHP 客户端库使用过滤器的示例?

Are there examples of using filters through the PHP client library?

推荐答案

背景

Google API 客户端库是从 Google 发现服务.PHP 客户端库 生成 setPropertygetProperty 用于资源的每个属性.

Background

The Google API Client libraries are generated from the Google Discovery Service. And the PHP client library generates a setProperty and getProperty for every property of a resource.

Analytics Reporting API V4 参考文档详尽地描述了API.开发人员指南提供了客户端库将生成的底层 JSON 示例:

The Analytics Reporting API V4 reference docs exhustively describe the API. The Developer Guide gives the underlying JSON example which the client libraries will generate:

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
  "reportRequests":
  [
    {
      "viewId": "XXXX",
      "dateRanges": [
        {"endDate": "2014-11-30", "startDate": "2014-11-01"}
      ],
      "metrics": [
        {"expression": "ga:pageviews"},
        {"expression": "ga:sessions"}
      ],
      "dimensions": [{"name": "ga:browser"}, {"name": "ga:country"}],
      "dimensionFilterClauses": [
        {
          "filters": [
            {
              "dimensionName": "ga:browser",
              "operator": "EXACT",
              "expressions": ["Chrome"]
            }
          ]
        }
      ]
    }
  ]
}

示例页面提供了许多使用 Python、Java、PHP 和JavaScript,它应该能让您很好地了解如何使用各个客户端库.但是您说得对,没有使用过滤器的 PHP 的明确示例.

And the Samples page gives many examples requests in Python, Java, PHP and JavaScript, which should give you a good sense of how to work with the individual client libraries. But you are correct there is not an explicit example of PHP using a filter.

下面是与上面的请求相同的例子:

Below is the same example as the request above:

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2014-11-01");
$dateRange->setEndDate("2014-11-30");

// Create the Metrics object.
$pageviews = new Google_Service_AnalyticsReporting_Metric();
$pageviews->setExpression("ga:pageviews");

$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");

//Create the Dimensions object.
$browser = new Google_Service_AnalyticsReporting_Dimension();
$browser->setName("ga:browser");

$country = new Google_Service_AnalyticsReporting_Dimension();
$country->setName("ga:country");

// Create the DimensionFilter.
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName('ga:browser');
$dimensionFilter->setOperator('EXACT');
$dimensionFilter->setExpressions(array('Chrome'));

// Create the DimensionFilterClauses
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters(array($dimensionFilter));

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("XXXX");
$request->setDateRanges($dateRange);
$request->setDimensions(array($browser, $country));
$request->setDimensionFilterClauses(array($dimensionFilterClause));
$request->setMetrics(array($pageviews, $sessions));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analyticsreporting->reports->batchGet( $body );

您可能已经注意到,我从未使用过 $object->getProperty().基本上它会做的就是给我它的当前值.在调用 API 时,您应该只需要 $object->setProperty($value); 因此,我为您提供了生成客户端库的背景.

As you probably noticed I never once used a $object->getProperty(). Basically All it would do is give me its current value. When Calling the API you should only ever need to $object->setProperty($value); Hence why I gave you the background that the client libraries are generated.

Analytics Reporting API 本身很复杂,并且有许多客户端库语言.并非总是可以在每种可能的客户端库语言中给出 API 的每种可能用法的示例.这就是为什么有必要了解如何查看参考文档并了解如何根据所描述的结构生成客户端库.

The Analytics Reporting API itself is complex and there are many client library languages. It is not always possible to give an example every possible usage of an API in every possible client library language. That is why it is necessary to understand how to look at the reference docs and understand how the client libraries are generated from the structure described.

这篇关于过滤来自 Google Analytics Reporting API 的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆