我正在使用Google客户端库来使用Google Analytics Reporting API第4版从Google Analytics获取数据。问题是它不接受我的正则表达式。我已经确认正则表达式适用于Google的查询资源管理器。当我尝试将它与Google_Service_AnalyticsReporting_DimensionFilter()类一起使用时,它无效。输出显示Totals等于零意味着没有返回任何记录。请看下面的代码:
function getReport($analytics)
{
// Replace with your view ID, for example XXXX.
$VIEW_ID = $this->viewId;
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate($this->startDate);
$dateRange->setEndDate($this->endDate);
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression($this->metrics);
$sessions->setAlias($this->metricAlias);
//Create the Dimensions object.
$pagePath = new Google_Service_AnalyticsReporting_Dimension();
$pagePath->setName("ga:pagePath");
// Create Dimension Filter 1, Must use: =~
$dimensionFilter1 = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter1->setDimensionName("ga:pagePath");
$dimensionFilter1->setOperator('REGEXP');
$dimensionFilter1->setExpressions('27-of-the-most-beautiful-small-towns-to-visit-in-europe');
$dimensionFilter1->setNot(FALSE);
// Create Dimension Filter 2, Must use: !~
$dimensionFilter2 = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter2->setDimensionName("ga:pagePath");
$dimensionFilter2->setOperator('REGEXP');
$dimensionFilter2->setExpressions('\/.*?\/[0-9]+');
$dimensionFilter2->setNot(TRUE);
// Create Dimension Filter 3, Must use !~
$dimensionFilter3 = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter3->setDimensionName("ga:pagePath");
$dimensionFilter3->setOperator('REGEXP');
$dimensionFilter3->setExpressions('\/.*?\/images/images');
$dimensionFilter3->setNot(TRUE);
// Create the DimensionFilterClauses
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters([$dimensionFilter1, $dimensionFilter2, $dimensionFilter3]);
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics([$sessions]);
$request->setDimensions([$pagePath]);
$request->setDimensionFilterClauses([$dimensionFilterClause]);
$request->setIncludeEmptyRows(false);
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$request]);
return $analytics->reports->batchGet($body);
}
这段代码不正确吗?
// Create Dimension Filter 1.
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName("ga:pagePath");
$dimensionFilter->setOperator('REGEXP');
$dimensionFilter->setExpressions('ga:PagePath !~ \/.*?\/[0-9]+');
答案 0 :(得分:0)
是。代码中有错误。您不应在ga:pagePath
方法中包含维度名称,即setExpression
。
我假设您尝试在过滤器中添加Does not match Regular Expression
运算符。
所以你的代码应该是这样的:
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName("ga:pagePath");
$dimensionFilter->setOperator('REGEXP');
$dimensionFilter->setExpressions('\/.*?\/[0-9]+');
$dimensionFilter->setNot(TRUE);
看看它是否有效。