a3fa7958
Alex Savenko
base
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php
/**
* Created by PhpStorm.
* User: Alex Savenko
* Date: 28.12.2016
* Time: 21:52
*/
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
/* @var $this yii\web\View */
$this->title = Yii::t('app', 'Report');
$this->params['breadcrumbs'][] = $this->title;
?>
<div>
<h1><?= Html::encode($this->title) ?></h1>
|
88d016ad
Alex Savenko
testing
|
21
|
</div>
|
050d419e
Alex Savenko
testing
|
22
|
|
88d016ad
Alex Savenko
testing
|
23
|
<?php
|
050d419e
Alex Savenko
testing
|
24
|
|
88d016ad
Alex Savenko
testing
|
25
|
// Загрузка клиентской библиотеки PHP для Google API.
|
2d10f818
Alex Savenko
testing
|
26
|
|
2d10f818
Alex Savenko
testing
|
27
|
|
88d016ad
Alex Savenko
testing
|
28
|
$client = new Google_Client();
|
5c42bcd6
Alex Savenko
ga
|
29
|
$client->setAuthConfig('./client_secrets.json');
|
6981a7c2
Alex Savenko
ga
|
30
|
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
|
2d10f818
Alex Savenko
testing
|
31
|
|
2d10f818
Alex Savenko
testing
|
32
|
|
88d016ad
Alex Savenko
testing
|
33
34
|
// Если пользователь уже авторизовал это приложение, предоставьте токен доступа.
// В противном случае перенаправьте пользователя на страницу авторизации доступа в Google Analytics.
|
59a93b93
Alex Savenko
ga
|
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Установка токена доступа на клиентском компьютере.
$client->setAccessToken($_SESSION['access_token']);
// Создание авторизованного объекта службы аналитики.
$analytics = new Google_Service_AnalyticsReporting($client);
// Вызов the Analytics Reporting API V4.
$response = getReport($analytics);
// Вывод ответа.
printResults($response);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
function getReport($analytics) {
// Замена на свой идентификатор представления, напр. XXXX.
$VIEW_ID = "119240817";
// Создание объекта DateRange.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
// Создание объекта Metrics.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
// Создание объекта ReportRequest.
$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 );
}
function printResults($reports) {
for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
$report = $reports[ $reportIndex ];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows[ $rowIndex ];
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
}
for ($j = 0; $j < count( $metricHeaders ) && $j < count( $metrics ); $j++) {
$entry = $metricHeaders[$j];
$values = $metrics[$j];
print("Metric type: " . $entry->getType() . "\n" );
for ( $valueIndex = 0; $valueIndex < count( $values->getValues() ); $valueIndex++ ) {
$value = $values->getValues()[ $valueIndex ];
print($entry->getName() . ": " . $value . "\n");
}
}
}
}
}
|