· 6 years ago · Oct 05, 2019, 08:26 AM
1<?php
2
3// Load the Google API PHP Client Library.
4 require_once __DIR__ . '/../../vendor/autoload.php';
5
6 $analytics = initializeAnalytics();
7 $response = getReport($analytics);
8 printResults($response);
9
10
11 /**
12 * Initializes an Analytics Reporting API V4 service object.
13 *
14 * @return An authorized Analytics Reporting API V4 service object.
15 */
16 function initializeAnalytics()
17 {
18
19 // Use the developers console and download your service account
20 // credentials in JSON format. Place them in this directory or
21 // change the key file location if necessary.
22 $KEY_FILE_LOCATION = __DIR__ . '/xxx.json';
23
24 // Create and configure a new client object.
25 $client = new Google_Client();
26 $client->setApplicationName("Hello Analytics Reporting");
27 $client->setAuthConfig($KEY_FILE_LOCATION);
28 $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
29 $analytics = new Google_Service_AnalyticsReporting($client);
30
31 return $analytics;
32 }
33
34
35 /**
36 * Queries the Analytics Reporting API V4.
37 *
38 * @param service An authorized Analytics Reporting API V4 service object.
39 * @return The Analytics Reporting API V4 response.
40 */
41 function getReport($analytics) {
42
43 // Replace with your view ID, for example XXXX.
44 $VIEW_ID = "160837360";
45
46 // Create the DateRange object.
47 $dateRange = new Google_Service_AnalyticsReporting_DateRange();
48 $dateRange->setStartDate("1000daysago");
49 $dateRange->setEndDate("today");
50
51 // Create the Metrics object.
52 $users = new Google_Service_AnalyticsReporting_Metric();
53 $users->setExpression("ga:users");
54 $users->setAlias("users");
55
56 // Create the ReportRequest object.
57 $request = new Google_Service_AnalyticsReporting_ReportRequest();
58 $request->setViewId($VIEW_ID);
59 $request->setDateRanges($dateRange);
60 $request->setMetrics(array($users));
61
62 $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
63 $body->setReportRequests( array( $request) );
64 return $analytics->reports->batchGet( $body );
65
66 }
67
68
69 /**
70 * Parses and prints the Analytics Reporting API V4 response.
71 *
72 * @param An Analytics Reporting API V4 response.
73 */
74 function printResults($reports) {
75 for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
76 $report = $reports[ $reportIndex ];
77 $header = $report->getColumnHeader();
78 $dimensionHeaders = $header->getDimensions();
79 $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
80 $rows = $report->getData()->getRows();
81
82 for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
83 $row = $rows[ $rowIndex ];
84 $dimensions = $row->getDimensions();
85 $metrics = $row->getMetrics();
86 for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
87 print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
88 }
89
90 for ($j = 0; $j < count($metrics); $j++) {
91 $values = $metrics[$j]->getValues();
92 for ($k = 0; $k < count($values); $k++) {
93 $entry = $metricHeaders[$k];
94 print($values[$k] . "\n");
95 }
96 }
97 }
98 }
99 }