Google Analytics(分析)报告获取所有结果,而不是仅过滤事件类别

时间:2018-08-03 03:25:58

标签: google-analytics

这是分析报告的屏幕截图: Analytics Report Image

当我尝试将Google Analytics(分析)报告php API(v.2.2.2)与过滤器事件类别(按“视频”类别过滤)一起使用时:

$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("3daysago");
$dateRange->setEndDate("today");


// Create the Metrics object.
$ev = new Google_Service_AnalyticsReporting_Metric();
$ev->setExpression("ga:eventValue");
$ev->setAlias("EventValue");

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

$avg = new Google_Service_AnalyticsReporting_Metric();
$avg->setExpression("ga:avgEventValue");
$avg->setAlias("Avg Value");

//Create the dimensions
// $sc = new Google_Service_AnalyticsReporting_Dimension();
// $sc->setName("ga:subContinent");

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

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

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


// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");

// Create Dimension Filter.
$dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
$dimensionFilter->setDimensionName("ga:eventCategory");
$dimensionFilter->setOperator("EXACT");
$dimensionFilter->setExpressions(array("Videos"));

// Create Segment Filter Clause.
$segmentFilterClause = new Google_Service_AnalyticsReporting_SegmentFilterClause();
$segmentFilterClause->setDimensionFilter($dimensionFilter);

// Create the Or Filters for Segment.
$orFiltersForSegment = new Google_Service_AnalyticsReporting_OrFiltersForSegment();
$orFiltersForSegment->setSegmentFilterClauses(array($segmentFilterClause));

// Create the Simple Segment.
$simpleSegment = new Google_Service_AnalyticsReporting_SimpleSegment();
$simpleSegment->setOrFiltersForSegment(array($orFiltersForSegment));

// Create the Segment Filters.
$segmentFilter = new Google_Service_AnalyticsReporting_SegmentFilter();
$segmentFilter->setSimpleSegment($simpleSegment);

// Create the Segment Definition.
$segmentDefinition = new Google_Service_AnalyticsReporting_SegmentDefinition();
$segmentDefinition->setSegmentFilters(array($segmentFilter));

// Create the Dynamic Segment.
$dynamicSegment = new Google_Service_AnalyticsReporting_DynamicSegment();
$dynamicSegment->setSessionSegment($segmentDefinition);
$dynamicSegment->setName("Video");

// Create the Segments object.
$segment = new Google_Service_AnalyticsReporting_Segment();
$segment->setDynamicSegment($dynamicSegment);


// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges(array($dateRange));
$request->setSegments(array($segment));
$request->setDimensions(array($segmentDimensions,$ec,$ea,$el));
$request->setMetrics(array($ev, $avg, $tEv));

// Create the GetReportsRequest object.
$getReport = new Google_Service_AnalyticsReporting_GetReportsRequest();
$getReport->setReportRequests(array($request));

// Call the batchGet method.
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$response = $analyticsreporting->reports->batchGet( $body );

printResults($response->getReports());

结果返回所有类别(横幅和视频):

ga:segment: Video
ga:eventCategory: Banner
ga:eventAction: click
ga:eventLabel: click on banner
EventValue: 0
Avg Value: 0.0
Total Events: 52
----------------------------
ga:segment: Video
ga:eventCategory: Videos
ga:eventAction: play
ga:eventLabel: Fall Campaign
EventValue: 0
Avg Value: 0.0
Total Events: 29

我是一个初学者,你能告诉我我的问题吗?

./谢谢

1 个答案:

答案 0 :(得分:0)

您似乎正在创建一个细分而不是一个过滤器。您想要更改代码以改为使用过滤器。过滤器将仅向您显示与过滤器匹配的数据,而一个细分将创建所有具有至少一个类别为视频的事件的所有GA会话的一部分。如果其他事件也出现在同一会话中,则将其包含在该部分中。

考虑这一点的一种好方法是,段仅过滤整个会话,而过滤器可以切片特定API调用的实际返回数据。