gpt4 book ai didi

php - 如何在 php 中的 google analytic api v4 中使用不完全精确的运算符创建动态段?

转载 作者:可可西里 更新时间:2023-10-31 22:40:59 24 4
gpt4 key购买 nike

我正在尝试创建一个动态分段来过滤我的流量数据。
我正在使用这个例子 https://developers.google.com/analytics/devguides/reporting/core/v4/samples#dimensions_and_metrics .
但我不想在 Google_Service_AnalyticsReporting_SegmentDimensionFilter 对象中使用“EXACT”运算符。我想使用 contain 之类的东西,但我不知道该用什么词,而且我在文档中也找不到它。这是我的代码:

    $client = new Google_Client();
$client->setAuthConfigFile('authFile.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

$analytics = new Google_Service_AnalyticsReporting($client);
//var_dump($analytics);

// Replace with your view ID. E.g., XXXX.
$VIEW_ID = "XXXX";
//$VIEW_ID =

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2016-04-01");
$dateRange->setEndDate("2016-04-30");

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

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

// 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:source");
$dimensionFilter->setOperator("EXACT");
$dimensionFilter->setExpressions("XXXX");

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

// 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.
$sessionSegment = new Google_Service_AnalyticsReporting_SegmentDefinition();
$sessionSegment->setSegmentFilters($segmentFilter);

$dynamicSegment = new Google_Service_AnalyticsReporting_DynamicSegment();
$dynamicSegment->setSessionSegment($sessionSegment);
$dynamicSegment->setName('No spam');

// Create a Segment 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($dateRange);
$request->setMetrics(array($sessions));
$request->setDimensions(array($source, $segmentDimensions));
$request->setSegments(array($segment));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$reports = $analytics->reports->batchGet( $body );

这里是我尝试输入 EXACT 以外的内容时的错误消息:

{      "error":{  
"code":400,
"message":"Invalid value at 'report_requests[0].segments[0].dynamic_segment.session_segment.segment_filters.simple_segment.or_filters_for_segment[0].segment_filter_clauses[0].dimension_filter.operator' (TYPE_ENUM), \"LIKE\"",
"errors":[
{
"message":"Invalid value at 'report_requests[0].segments[0].dynamic_segment.session_segment.segment_filters.simple_segment.or_filters_for_segment[0].segment_filter_clauses[0].dimension_filter.operator' (TYPE_ENUM), \"LIKE\"",
"domain":"global",
"reason":"badRequest"
}
],
"status":"INVALID_ARGUMENT" } }

由于各种原因,我无法使用之前在 Google 界面中创建的分割并与我的服务帐户共享。
谢谢你的回答。

最佳答案

这个有点晚了。我遇到了类似的问题,看起来这些是您可以使用的运算符,您想要的是 PARTIAL。希望这对遇到类似问题的其他人有所帮助。

https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#Operator

关于php - 如何在 php 中的 google analytic api v4 中使用不完全精确的运算符创建动态段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37585750/

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