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

查看:111
本文介绍了过滤来自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.

Google Analytics(分析)报告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天全站免登陆