gpt4 book ai didi

google-analytics - 如何在 Google Analytics Reporting API V4 中使用正则表达式

转载 作者:行者123 更新时间:2023-12-03 15:30:59 25 4
gpt4 key购买 nike

我正在使用适用于 PHP 的 Google 客户端库使用 Google Analytics Reporting API 版本 4 从 Google Analytics 获取数据。问题是它不接受我的正则表达式。我已经确认正则表达式在 Google 的 Query Explorer 中有效。当我尝试将它与 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]+');

最佳答案

是的。代码有错误。您不应包含维度名称,即 ga:pagePathsetExpression方法。

我假设您正在尝试添加 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);

看看它是否有效。

关于google-analytics - 如何在 Google Analytics Reporting API V4 中使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46960709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com