GA API v4几个DimensionFilter的作品很奇怪(php)

时间:2016-12-07 17:43:34

标签: php google-analytics-api

我试图在php上使用GA APi v4通过事件获取统计信息。 这是页面上的ga js:

    <script type="text/javascript">
       ga("send", "event", "eventCategory", "eventAction", "eventLabel");
    </script>

我想获得此事件(eventCategory)的执行次数,过滤者,eventAction和eventLabel。以下是我的代码,基于Google指南:

    function getReport($analytics) {

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

      $dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
      $dimensionFilter->setDimensionName('ga:eventCategory');
      $dimensionFilter->setOperator('EXACT');
      $dimensionFilter->setExpressions('My_event name');                        

      $dimensionFilter2 = new Google_Service_AnalyticsReporting_DimensionFilter();
      $dimensionFilter2->setDimensionName('ga:eventAction');
      $dimensionFilter2->setOperator('EXACT');
      $dimensionFilter2->setExpressions('My_event_action'); 

      $dimensionFilter3 = new Google_Service_AnalyticsReporting_DimensionFilter();
      $dimensionFilter3->setDimensionName('ga:eventLabel');
      $dimensionFilter3->setOperator('EXACT');
      $dimensionFilter3->setExpressions('My_event_label');  

      $dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
      $dimensionFilterClause->setFilters([$dimensionFilter, $dimensionFilter2, $dimensionFilter3]);     

      $request = new Google_Service_AnalyticsReporting_ReportRequest();
      $request->setViewId($VIEW_ID);
      $request->setDateRanges($dateRange);
      $request->setMetrics(array($totalEvents));
      $request->setDimensionFilterClauses([$dimensionFilterClause]);
      $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
      $body->setReportRequests( array( $request) );
      return $analytics->reports->batchGet( $body );
    }

但我得到了奇怪的结果。

      $dimensionFilterClause->setFilters([$dimensionFilter]);

据我所知,这是我的事件My_event_name的所有执行。答案来自API,totalEvents:1737

      $dimensionFilterClause->setFilters([$dimensionFilter, $dimensionFilter2]);

在这种情况下,我希望获得My_event名称的所有执行,但是通过ga:eventAction = My_event_action进行过滤。答案来自API,totalEvents:1738 过滤事件的数量大于所有事件的数量?

      $dimensionFilterClause->setFilters([$dimensionFilter, $dimensionFilter2, $dimensionFilter3]);

如果所有过滤器的答案再次为1737。

在此设置中,我希望收到My_event名称的执行次数,通过ga:eventAction = My_event_action和ga:eventLabel = My_event_label进行过滤,但出现了问题。当计数超过1时,过滤器似乎不起作用。 也许我做错了什么?

1 个答案:

答案 0 :(得分:0)

您必须指定Google应该如何对待不同的过滤器:AND或OR?默认情况下,它是OR。要进行切换,请添加以下行: $dimensionFilterClause->setOperator('AND');