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

时间:2016-06-02 07:51:26

标签: php google-analytics-api google-api-php-client

我试图创建一个动态细分来过滤我的交通数据 我使用这个例子https://developers.google.com/analytics/devguides/reporting/core/v4/samples#dimensions_and_metrics 但我不想使用' EXACT' Google_Service_AnalyticsReporting_SegmentDimensionFilter对象中的运算符。我想使用像contains这样的东西,但我不知道要使用哪个词,我无法在文档中找到它。 这是我的代码:

    $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界面中创建的细分,并与我的服务帐户共享。
谢谢你的回答。

1 个答案:

答案 0 :(得分:6)

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

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