3

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

Google Analytics 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

Google Analytics 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!

Tiago Alcobia
  • 99
  • 1
  • 5
  • You have done well to describe what you are trying to achieve but you haven't actually posted any code showing what you have done. A lot of these reports are internal reports and there is no way to really get the exact same information out using the API your going to have to do a lot of the processing on your own. – Linda Lawton - DaImTo Apr 10 '18 at 12:47
  • @DaImTo Thanks for the comment. I don't mind doing the processing but right now I don't even see what the path could be. I have attached the sample code above for review. – Tiago Alcobia Apr 10 '18 at 14:25

0 Answers0