Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How creating dynamic segment with not exact exact operator in google analytic api v4 within php?

I'm trying to create a dynamic segment to filter my trafic data.
I'm using this exemple https://developers.google.com/analytics/devguides/reporting/core/v4/samples#dimensions_and_metrics.
But I don't want to use the 'EXACT' operator in the Google_Service_AnalyticsReporting_SegmentDimensionFilter objet. I'd like to use something like contain but I don't know what word to use and I can't find it in the documentation. Here is my code :

    $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 );

and here is my error message when I try to put something else than 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"    } }

For various reason, I can't use a segment previously created in the google interface and share it with my service account.
Thank you for your answer.

like image 621
Marie P Avatar asked Jun 02 '16 07:06

Marie P


1 Answers

A bit late to this one. I had a similar problem and it looks like these are the operators you can use, the one you want is PARTIAL. Hope this helps anyone else with a similar problem.

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

like image 151
MrHappySandwich Avatar answered Nov 15 '22 01:11

MrHappySandwich