如何在谷歌分析报告api v4中使用多个过滤器?

时间:2017-03-17 08:08:13

标签: php google-analytics google-analytics-v4

https://developers.google.com/analytics/devguides/reporting/core/v4/samples用于从Google Analytics中查找数据。

我想使用多维过滤器查找数据但不能这样做 我正在使用该代码。

function buildSimpleSegment($ segmentName,$ dimension,$ dimensionFilterExpression){

// 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($dimension);
$dimensionFilter->setOperator("EXACT");
$dimensionFilter->setExpressions(array($dimensionFilterExpression));

//print_r($dimensionFilter);die;
// 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($segmentName);

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

}

function getReport($ analyticsreporting){

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();

if (isset($_GET['startdat']) && isset($_GET['enddate'])) {
    $dateRange->setStartDate($_GET['startdat']);
    $dateRange->setEndDate($_GET['enddate']);
} else {
    $dateRange->setStartDate(date('Y-m-d', strtotime(date('Y-m-d') . ' - 1 month')));
    $dateRange->setEndDate(date('Y-m-d'));
}
$domain = $_GET['domain'];
$VIEW_ID = "xyz";
// Create the Metrics object.

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

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

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

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

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

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

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges(array($dateRange));
$request->setDimensions(array($source, $eventAction, $eventLabel, $eventCategory, $segmentDimensions));
$request->setMetrics(array($totalevent));

$sourceSegment = buildSimpleSegment("Source", "ga:source", $domain);
$videoSegment = buildSimpleSegment("Category", "ga:eventCategory", "JW Player Video Plays");
$request->setSegments(array($sourceSegment, $videoSegment));


// Call the batchGet method.
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();



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

}

2 个答案:

答案 0 :(得分:1)

我认为有点晚了,但仍然有用,因为我在SO上找不到任何答案。

要定义多个过滤器,您需要多次实例化类 Google_Service_AnalyticsReporting_DimensionFilter 。例如:

$filter1 = new Google_Service_AnalyticsReporting_DimensionFilter();
$filter1->setDimensionName('first_dimension_name');
$filter1->setOperator('first_operator');
$filter1->setExpressions('first_expression');

$filter2 = new Google_Service_AnalyticsReporting_DimensionFilter();
$filter2->setDimensionName('second_dimension_name');
$filter2->setOperator('second_operator');
$filter2->setExpressions('second_expression');

然后定义与上面定义的过滤器绑定的filter子句:

$filter_clause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$filter_clause->setOperator('your_filter_operator');
$filter_clause->setFilters(array($filter1, $filter2));

然后照常执行请求。

答案 1 :(得分:0)

@Maurizio提供的答案很好,但我需要一个解决方案来应对数量众多的广告系列。客户可以跟踪一到五个。这是我登陆的地方,以防它帮助处理Google的人(相当糟糕的API文档)。

<div class="circle"> </div>
<div class="circle2"> </div>