Ive been trying to get the metrics for all urls that contain a specific id in them. From this question: Adding ga:pagePath dimension to get page views for a particular URL using Google Analytics Reporting API v4 I saw the query approach and tried it instead of the object oriented version. In the query approach, the code works fantastic with the exception of it will only return the last metric sent (in this example it only returns unique page views since it is listed last for metrics). I need the values for all three returned without having to hit the api three separate times. Here is my code:
$query = [ "viewId" => $profileId, "dateRanges" => [ "startDate" => "2018-01-25", "endDate" => "2018-01-25" ], "metrics" => [ "expression" => "ga:pageviews", "expression" => "ga:avgTimeOnPage", "expression" => "ga:uniquePageviews" ], "dimensions" => [ "name" => "ga:pagepath" ], "dimensionFilterClauses" => [ 'filters' => [ "dimension_name" => "ga:pagepath", "operator" => "PARTIAL", "expressions" => $theId ] ] ]; // build the request and response $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests(array($query)); $report = $analytics->reports->batchGet($body);
Any thoughts on how I would do this correctly?
Advertisement
Answer
Try the following function, just replace the ENTER_ID with you ID. This seems to fetch traffic for all pages though as well
function segmentRequest(&$analyticsreporting) { // Create the DateRange object. $dateRange = new Google_Service_AnalyticsReporting_DateRange(); $dateRange->setStartDate("2018-02-01"); $dateRange->setEndDate("2018-02-15"); // Create the Metrics object. $pageviews = new Google_Service_AnalyticsReporting_Metric(); $pageviews->setExpression("ga:pageviews"); $pageviews->setAlias("pageviews"); //Create the browser dimension. $path = new Google_Service_AnalyticsReporting_Dimension(); $path->setName("ga:pagePath"); // 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:pagePath"); $dimensionFilter->setOperator("PARTIAL"); $dimensionFilter->setExpressions(array("ENTER_ID")); // Create Segment Filter Clause. $segmentFilterClause = new Google_Service_AnalyticsReporting_SegmentFilterClause(); $segmentFilterClause->setDimensionFilter($dimensionFilter); // 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. $segmentDefinition = new Google_Service_AnalyticsReporting_SegmentDefinition(); $segmentDefinition->setSegmentFilters(array($segmentFilter)); // Create the Dynamic Segment. $dynamicSegment = new Google_Service_AnalyticsReporting_DynamicSegment(); $dynamicSegment->setSessionSegment($segmentDefinition); $dynamicSegment->setName("Visits to /en/listings.php"); // Create the Segments object. $segment = new Google_Service_AnalyticsReporting_Segment(); $segment->setDynamicSegment($dynamicSegment); // Create the ReportRequest object. $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId("XXXXX"); $request->setDateRanges(array($dateRange)); $request->setDimensions(array($path, $segmentDimensions)); $request->setSegments(array($segment)); $request->setMetrics(array($pageviews)); // Create the GetReportsRequest object. $getReport = new Google_Service_AnalyticsReporting_GetReportsRequest(); $getReport->setReportRequests(array($request)); // Call the batchGet method. $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests( array( $request) ); $response = $analyticsreporting->reports->batchGet( $body ); printResults($response->getReports()); }