I have this code on google analytics API v4 with PHP.
$eCPM_Adsense = new Google_Service_AnalyticsReporting_Metric(); $eCPM_Adsense->setExpression("ga:adsenseECPM"); $eCPM_Adsense->setAlias("eCPM Adsense"); // Create the Ordering. $ordering = new Google_Service_AnalyticsReporting_OrderBy(); $ordering->setFieldName("ga:adsenseECPM"); $ordering->setOrderType("VALUE"); $ordering->setSortOrder("DESCENDING");
The Ordering not works for me. Can you help me? Thanks
Advertisement
Answer
the problem is that you need to have the setOrderBys() on the request. This is not detailed on the API documentation…
an example
$ordering = new Google_Service_AnalyticsReporting_OrderBy(); $ordering->setFieldName("ga:pageviews"); $ordering->setOrderType("VALUE"); $ordering->setSortOrder("DESCENDING"); $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId($VIEW_ID); $request->setDateRanges($dateRange); $request->setDimensions(array($path)); $request->setMetrics(array($sessions)); $request->setOrderBys($ordering); // note this one!