1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: Tanya Kalashnik |
4
|
|
|
* Date: 21.07.14 11:18 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
use Yandex\Metrica\Stat\StatClient; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
$data = []; |
11
|
|
|
$errorMessage = false; |
12
|
|
|
|
13
|
|
|
//Is auth |
14
|
|
View Code Duplication |
if (isset($_COOKIE['yaAccessToken']) && isset($_COOKIE['yaClientId'])) { |
|
|
|
|
15
|
|
|
$settings = require_once '../../settings.php'; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
try { |
19
|
|
|
$statClient = new StatClient($_COOKIE['yaAccessToken']); |
20
|
|
|
|
21
|
|
|
if (isset($_GET['counter-id']) && $_GET['counter-id']) { |
22
|
|
|
$counterId = $_GET['counter-id']; |
23
|
|
|
|
24
|
|
|
$paramsModel = new Yandex\Metrica\Stat\Models\TableParams(); |
25
|
|
|
$paramsModel->setPreset(\Yandex\Metrica\Stat\AvailableValues::PRESET_TECH_PLATFORMS) |
26
|
|
|
/** |
27
|
|
|
* Список измерений, разделенных запятой |
28
|
|
|
*/ |
29
|
|
|
->setDimensions(\Yandex\Metrica\Stat\DimensionsConst::S_BROWSER) |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Идентификатор счетчика |
33
|
|
|
*/ |
34
|
|
|
->setId($counterId); |
35
|
|
|
/** |
36
|
|
|
* @see http://api.yandex.ru/metrika/doc/beta/api_v1/data.xml |
37
|
|
|
*/ |
38
|
|
|
$data = $statClient->data()->getTable($paramsModel); |
39
|
|
|
} |
40
|
|
|
} catch (\Exception $ex) { |
41
|
|
|
$errorMessage = $ex->getMessage(); |
42
|
|
|
if ($errorMessage === 'PlatformNotAllowed') { |
43
|
|
|
$errorMessage .= '<p>Возможно, у приложения нет прав на доступ к ресурсу. Попробуйте ' |
44
|
|
|
. '<a href="' . rtrim(str_replace($_SERVER['DOCUMENT_ROOT'], '', __DIR__), "/") . '/../OAuth/' . '">авторизироваться</a> и повторить.</p>'; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
?> |
49
|
|
|
<!doctype html> |
50
|
|
|
<html lang="en-US"> |
51
|
|
|
<head> |
52
|
|
|
<meta charset="UTF-8"> |
53
|
|
|
<title>Yandex.SDK: Metrica Demo</title> |
54
|
|
|
|
55
|
|
|
<link rel="stylesheet" href="//yandex.st/bootstrap/3.0.3/css/bootstrap.min.css"> |
56
|
|
|
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet"> |
57
|
|
|
<link rel="stylesheet" href="/examples/Disk/css/style.css"> |
58
|
|
|
|
59
|
|
|
</head> |
60
|
|
|
<body> |
61
|
|
|
|
62
|
|
|
<div class="container"> |
63
|
|
|
<div class="jumbotron"> |
64
|
|
|
<h2><a href="/examples/Metrica"><span class="glyphicon glyphicon-tasks"></span></a> Пример работы с Яндекс Метрикой</h2> |
65
|
|
|
</div> |
66
|
|
|
<ol class="breadcrumb"> |
67
|
|
|
<li><a href="/examples">Examples</a></li> |
68
|
|
|
<li><a href="/examples/Metrica">Metrica</a></li> |
69
|
|
|
<li><a href="/examples/Metrica/Stat">Stat</a></li> |
70
|
|
|
<li class="active">Отчет "Технологии - Браузеры"</li> |
71
|
|
|
</ol> |
72
|
|
|
<?php |
73
|
|
|
if (!isset($_COOKIE['yaAccessToken']) || !isset($_COOKIE['yaClientId'])) { |
74
|
|
|
?> |
75
|
|
|
<div class="alert alert-info"> |
76
|
|
|
Для просмотра этой страницы вам необходимо авторизироваться. |
77
|
|
|
<a id="goToAuth" href="/examples/OAuth" class="alert-link">Перейти на страницу авторизации</a>. |
78
|
|
|
</div> |
79
|
|
|
<?php |
80
|
|
|
} else { |
81
|
|
|
if ($errorMessage) { |
|
|
|
|
82
|
|
|
?> |
83
|
|
|
<div class="alert alert-danger"><?= $errorMessage ?></div> |
84
|
|
|
<?php |
85
|
|
|
} else { |
86
|
|
|
?> |
87
|
|
|
<div> |
88
|
|
|
<?php |
89
|
|
|
if ($data) { |
90
|
|
|
?> |
91
|
|
|
<h3>Отчет «Технологии — Браузеры»:</h3> |
92
|
|
|
<table id="accountTable" class="table table-striped table-bordered table-hover"> |
93
|
|
|
<thead> |
94
|
|
|
<tr> |
95
|
|
|
<td>Браузер</td> |
96
|
|
View Code Duplication |
<?php for ($i = 0; $i < count($data->getQuery()->getMetrics()); $i++) { ?> |
|
|
|
|
97
|
|
|
<td><?= $data->getQuery()->getMetrics()[$i] ?></td> |
98
|
|
|
<?php } ?> |
99
|
|
|
</tr> |
100
|
|
|
</thead> |
101
|
|
|
<tbody> |
102
|
|
|
<?php |
103
|
|
|
if (!is_null($data->getData())) { |
104
|
|
|
foreach ($data->getData() as $dimensions) { ?> |
105
|
|
|
<tr> |
106
|
|
|
<td><?= $dimensions->getDimensions()->current()->getName() ?></td> |
107
|
|
View Code Duplication |
<?php for ($i = 0; $i < count($data->getQuery()->getMetrics()); $i++) { ?> |
|
|
|
|
108
|
|
|
<td><?= $dimensions->getMetrics()[$i] ?></td> |
109
|
|
|
<?php } ?> |
110
|
|
|
</tr> |
111
|
|
|
<?php |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
?> |
115
|
|
|
</tbody> |
116
|
|
|
</table> |
117
|
|
|
<?php |
118
|
|
|
} |
119
|
|
|
?> |
120
|
|
|
</div> |
121
|
|
|
<?php |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
?> |
125
|
|
|
</div> |
126
|
|
|
|
127
|
|
|
<script src="http://yandex.st/jquery/2.0.3/jquery.min.js"></script> |
128
|
|
|
<script src="http://yandex.st/bootstrap/3.0.3/js/bootstrap.min.js"></script> |
129
|
|
|
|
130
|
|
|
</body> |
131
|
|
|
</html> |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.