I am creating a script in PHP to retrieve the information, via Google Analytics V4 API, that appears in the Google Analytics tab:
Behavior -> Site Speed -> Site Speed Page Timings -> Distribution
I cannot figure out how to obtain this data in histogram form (Page load time bucket per page load sample) with PHP code.
I was expecting that the ga:pageLoadTime
metric had a dimension that would allow me to define the bucket intervals as a histogram.... but it does not seem to exist.
I have been able to obtain the distribution metrics for the sessions count as seen in the screen:
Audience -> Behaviour -> Frequency & Recency
Note: I followed the example indicated in the page: Google Analytics API v4: Histogram Buckets
For the metric ga:sessions
I can use the dimension ga:sessionCount
so I get what the screen shows.
Current PHP code:
$VIEW_ID ="xxxxxx";
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2018-04-05");
$dateRange->setEndDate("2018-04-07");
// Create the Metrics object.
$loadTime = new Google_Service_AnalyticsReporting_Metric();
$loadTime->setExpression("ga:pageLoadTime");
$loadTime->setAlias("loadTime");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($loadTime));
//This next parameter without a dimension is useless!
//$request->setFiltersExpression("ga:pageLoadTime>40000");
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
Ideally I would want to do something like this but it does not work:
// Create the Dimensions object.
$buckets = new Google_Service_AnalyticsReporting_Dimension();
$buckets->setName("ga:pageLoadSample");
$buckets->setHistogramBuckets(array(1,8,100,201));
// Create the Ordering.
$ordering = new Google_Service_AnalyticsReporting_OrderBy();
$ordering->setOrderType("HISTOGRAM_BUCKET");
$ordering->setFieldName("ga:pageLoadSample");
$request->setDimensions(array($buckets));
$request->setOrderBys($ordering);
I also accept suggestions for alternatives to the API!