I
I
Igor M2019-06-12 13:44:21
PHP
Igor M, 2019-06-12 13:44:21

How to query the Google Analytics Reporting API?

Good day!
There is a task: to connect the site visitor with the data that google analytics has collected from him.
Found a great solution that suits me perfectly .
The problem is that I do not understand how I can execute the request from the example from PHP.
Here is the code from the example of an introduction to this whole thing, maybe it will help somehow

spoiler
function getReport($analytics) {
 
  // Replace with your view ID, for example XXXX.
  $VIEW_ID = "<REPLACE_WITH_VIEW_ID>";
 
  // Create the DateRange object.
  $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate("7daysAgo");
  $dateRange->setEndDate("today");
 
  // Create the Metrics object.
  $sessions = new Google_Service_AnalyticsReporting_Metric();
  $sessions->setExpression("ga:sessions");
  $sessions->setAlias("sessions");
 
  // Create the ReportRequest object.
  $request = new Google_Service_AnalyticsReporting_ReportRequest();
  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);
  $request->setMetrics(array($sessions));
 
  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests( array( $request) );
  return $analytics->reports->batchGet( $body );
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sl0, 2019-06-12
@ufozz

If I understood everything correctly, then you just need to send a POST request with the required fields.
The easiest option is via curl
https://www.php.net/manual/ru/function.curl-init.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question