Commit 0ecea44c6fcdd17c40a6c0cbaa58834548fe6bdd

Authored by Yarik
2 parents 67cb0d76 05a089dd

Merge remote-tracking branch 'origin/master'

backend/controllers/SiteController.php
... ... @@ -108,6 +108,6 @@
108 108  
109 109 public function actionAnalytic()
110 110 {
111   - return $this->renderPartial('analytic');
  111 + return $this->render('analytic');
112 112 }
113 113 }
... ...
backend/views/site/analytic.php
1 1 <?php
  2 + use artbox\gentelella\widgets\XPanel;
  3 + use dosamigos\highcharts\HighCharts;
2 4 use yii\helpers\VarDumper;
3 5  
4 6 $client = new Google_Client();
... ... @@ -15,16 +17,256 @@
15 17 $dateRange->setEndDate("today");
16 18  
17 19 $sessions = new Google_Service_AnalyticsReporting_Metric();
18   - $sessions->setExpression('ga:users');
19   - $sessions->setAlias('Пользователи');
  20 + $sessions->setExpression('ga:sessions');
  21 + $sessions->setAlias('Сеансы');
  22 +
  23 + $users = new Google_Service_AnalyticsReporting_Metric();
  24 + $users->setExpression('ga:users');
  25 + $users->setAlias('Пользователи');
  26 +
  27 + $views = new Google_Service_AnalyticsReporting_Metric();
  28 + $views->setExpression('ga:pageviews');
  29 + $views->setAlias('Просмотры');
  30 +
  31 + $new_sessions = new Google_Service_AnalyticsReporting_Metric();
  32 + $new_sessions->setExpression('ga:percentNewSessions');
  33 + $new_sessions->setAlias('Новые сессии');
  34 +
  35 + $dimensions = new Google_Service_AnalyticsReporting_Dimension();
  36 + $dimensions->setName('ga:date');
20 37  
21 38 $request = new Google_Service_AnalyticsReporting_ReportRequest();
22 39 $request->setViewId($profile_id);
23 40 $request->setDateRanges($dateRange);
24   - $request->setMetrics([ $sessions ]);
  41 + $request->setMetrics(
  42 + [
  43 + $sessions,
  44 + $users,
  45 + $views,
  46 + $new_sessions,
  47 + ]
  48 + );
  49 + $request->setDimensions($dimensions);
25 50  
26 51 $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
27 52 $body->setReportRequests([ $request ]);
28 53 $response = $analytics->reports->batchGet($body);
29 54  
30   - VarDumper::dump($response, 10, true);
31 55 \ No newline at end of file
  56 + // VarDumper::dump($response, 10, true);die();
  57 +
  58 + function printResults($reports)
  59 + {
  60 + $data = [];
  61 + for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {
  62 + $report = $reports[ $reportIndex ];
  63 + $header = $report->getColumnHeader();
  64 + $dimensionHeaders = $header->getDimensions();
  65 + $metricHeaders = $header->getMetricHeader()
  66 + ->getMetricHeaderEntries();
  67 + $rows = $report->getData()
  68 + ->getRows();
  69 + $totals = $report->getData()
  70 + ->getTotals();
  71 + $total_values = $totals[ 0 ]->getValues();
  72 +
  73 + $data[ 'sessions' ] = $total_values[ 0 ];
  74 + $data[ 'users' ] = $total_values[ 1 ];
  75 + $data[ 'views' ] = $total_values[ 2 ];
  76 + $data[ 'new' ] = $total_values[ 3 ];
  77 +
  78 + for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  79 + $row = $rows[ $rowIndex ];
  80 + $dimensions = $row->getDimensions();
  81 + $metrics = $row->getMetrics();
  82 + for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  83 + // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
  84 + }
  85 +
  86 + for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {
  87 + $values = $metrics[ $j ];
  88 + for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {
  89 + $value = $values->getValues()[ $valueIndex ];
  90 + $data[ $valueIndex ][] = (int) $value;
  91 + }
  92 + }
  93 + }
  94 + }
  95 +
  96 + return $data;
  97 + }
  98 +
  99 + $data = printResults($response->getReports());
  100 +
  101 +?>
  102 +
  103 +<div class="row">
  104 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  105 + <div class="tile-stats">
  106 + <div class="icon"><i class="fa fa-clock-o"></i>
  107 + </div>
  108 + <div class="count"><?= $data[ 'sessions' ] ?></div>
  109 +
  110 + <h3>Sessions</h3>
  111 + <p>Lorem ipsum psdea itgum rixt.</p>
  112 + </div>
  113 + </div>
  114 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  115 + <div class="tile-stats">
  116 + <div class="icon"><i class="fa fa-user"></i>
  117 + </div>
  118 + <div class="count"><?= $data[ 'users' ] ?></div>
  119 +
  120 + <h3>Users</h3>
  121 + <p>Lorem ipsum psdea itgum rixt.</p>
  122 + </div>
  123 + </div>
  124 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  125 + <div class="tile-stats">
  126 + <div class="icon"><i class="fa fa-eye"></i>
  127 + </div>
  128 + <div class="count"><?= $data[ 'views' ] ?></div>
  129 +
  130 + <h3>Page views</h3>
  131 + <p>Lorem ipsum psdea itgum rixt.</p>
  132 + </div>
  133 + </div>
  134 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  135 + <div class="tile-stats">
  136 + <div class="icon"><i class="fa fa-plus"></i>
  137 + </div>
  138 + <div class="count"><?= round(intval($data[ 'new' ]), 2) ?> %</div>
  139 +
  140 + <h3>New sessions</h3>
  141 + <p>Lorem ipsum psdea itgum rixt.</p>
  142 + </div>
  143 + </div>
  144 +</div>
  145 +
  146 +<div class="row">
  147 + <div class="col-md-12">
  148 + <?php $panel = XPanel::begin(
  149 + [
  150 + 'title' => 'Analytics',
  151 + 'toolbarLayout' => '{collapse}',
  152 + ]
  153 + ); ?>
  154 +
  155 + <?php
  156 + echo HighCharts::widget(
  157 + [
  158 + 'clientOptions' => [
  159 + 'colors' => [
  160 + '#9ABCC3',
  161 + '#A8E3D6',
  162 + ],
  163 + 'chart' => [
  164 + 'type' => 'area',
  165 + 'zoomType' => 'x',
  166 + ],
  167 + 'title' => [
  168 + 'text' => 'Analytics',
  169 + ],
  170 + 'yAxis' => [
  171 + 'title' => [
  172 + 'text' => 'Sessions count',
  173 + ],
  174 + ],
  175 + 'series' => [
  176 + [
  177 + 'name' => 'Sessions',
  178 + 'data' => $data[ 0 ],
  179 + ],
  180 + [
  181 + 'name' => 'Users',
  182 + 'data' => $data[ 1 ],
  183 + ],
  184 + ],
  185 + 'credits' => [
  186 + 'enabled' => false,
  187 + ],
  188 + 'plotOptions' => [
  189 + 'area' => [
  190 + 'marker' => [
  191 + 'enabled' => false,
  192 + 'symbol' => 'circle',
  193 + 'radius' => 2,
  194 + 'states' => [
  195 + 'hover' => [
  196 + 'enabled' => true,
  197 + ],
  198 + ],
  199 + ],
  200 + ],
  201 + ],
  202 + ],
  203 + ]
  204 + );
  205 + ?>
  206 +
  207 + <?php $panel::end(); ?>
  208 +
  209 + </div>
  210 +</div>
  211 +
  212 +<div class="row">
  213 + <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
  214 + <?php $panel = XPanel::begin(
  215 + [
  216 + 'title' => 'Analytics',
  217 + 'toolbarLayout' => '{collapse}',
  218 + ]
  219 + ); ?>
  220 +
  221 + <?php
  222 + echo HighCharts::widget(
  223 + [
  224 + 'clientOptions' => [
  225 + 'exporting' => [
  226 + 'enabled' => false,
  227 + ],
  228 + 'colors' => [
  229 + '#9ABCC3',
  230 + '#A8E3D6',
  231 + ],
  232 + 'chart' => [
  233 + 'plotBackgroundColor' => null,
  234 + 'plotBorderWidth' => null,
  235 + 'plotShadow' => false,
  236 + 'type' => 'pie',
  237 + ],
  238 + 'title' => [
  239 + 'text' => 'Analytics',
  240 + ],
  241 + 'series' => [
  242 + [
  243 + 'name' => 'Sessions',
  244 + 'data' => [
  245 + [
  246 + 'name' => 'New visitor',
  247 + 'y' => round(intval($data[ 'new' ]), 2),
  248 + ],
  249 + [
  250 + 'name' => 'Returning Visitor',
  251 + 'y' => 100 - round(intval($data[ 'new' ]), 2),
  252 + ],
  253 + ],
  254 + ],
  255 + ],
  256 + 'credits' => [
  257 + 'enabled' => false,
  258 + ],
  259 + 'plotOptions' => [
  260 + 'pie' => [
  261 + 'allowPointSelect' => true,
  262 + 'cursor' => 'pointer',
  263 + ],
  264 + ],
  265 + ],
  266 + ]
  267 + );
  268 + ?>
  269 +
  270 + <?php $panel::end(); ?>
  271 + </div>
  272 +
  273 +</div>
32 274 \ No newline at end of file
... ...
composer.lock
... ... @@ -4,10 +4,72 @@
4 4 "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5 "This file is @generated automatically"
6 6 ],
7   - "hash": "7024a3493d0dd61f5018018e8987f846",
8   - "content-hash": "f3f1ac35100ae05210344f07161221a4",
  7 + "hash": "aa326c5f3fbc6bf643995c57820a41a5",
  8 + "content-hash": "3827d959ce5cb915b9885f6c8387da17",
9 9 "packages": [
10 10 {
  11 + "name": "2amigos/yii2-highcharts-widget",
  12 + "version": "1.0.1",
  13 + "source": {
  14 + "type": "git",
  15 + "url": "https://github.com/2amigos/yii2-highcharts-widget.git",
  16 + "reference": "e820aa981335d0c7269706ad6cb2d0ae81da495b"
  17 + },
  18 + "dist": {
  19 + "type": "zip",
  20 + "url": "https://api.github.com/repos/2amigos/yii2-highcharts-widget/zipball/e820aa981335d0c7269706ad6cb2d0ae81da495b",
  21 + "reference": "e820aa981335d0c7269706ad6cb2d0ae81da495b",
  22 + "shasum": ""
  23 + },
  24 + "require": {
  25 + "bower-asset/highcharts-release": "4.1.4",
  26 + "yiisoft/yii2": "*"
  27 + },
  28 + "require-dev": {
  29 + "phpunit/phpunit": "4.*",
  30 + "scrutinizer/ocular": "~1.1"
  31 + },
  32 + "type": "yii2-extension",
  33 + "extra": {
  34 + "branch-alias": {
  35 + "dev-master": "1.0-dev"
  36 + },
  37 + "asset-installer-paths": {
  38 + "bower-asset-library": "vendor/bower"
  39 + }
  40 + },
  41 + "autoload": {
  42 + "psr-4": {
  43 + "dosamigos\\highcharts\\": "src"
  44 + }
  45 + },
  46 + "notification-url": "https://packagist.org/downloads/",
  47 + "license": [
  48 + "BSD-3-Clause"
  49 + ],
  50 + "authors": [
  51 + {
  52 + "name": "2amigOS! Consulting Group",
  53 + "email": "hola@2amigos.us",
  54 + "homepage": "http://2amigos.us",
  55 + "role": "Developer"
  56 + }
  57 + ],
  58 + "description": "Highcharts JS widget for Yii2.",
  59 + "homepage": "http://yiiwheels.com/extension/highcharts-widget",
  60 + "keywords": [
  61 + "2amigos",
  62 + "chart",
  63 + "extension",
  64 + "highcharts",
  65 + "widget",
  66 + "yii",
  67 + "yii 2",
  68 + "yii2"
  69 + ],
  70 + "time": "2015-03-16 21:55:20"
  71 + },
  72 + {
11 73 "name": "2amigos/yii2-tinymce-widget",
12 74 "version": "1.1.1",
13 75 "source": {
... ... @@ -211,6 +273,29 @@
211 273 ]
212 274 },
213 275 {
  276 + "name": "bower-asset/highcharts-release",
  277 + "version": "v4.1.4",
  278 + "source": {
  279 + "type": "git",
  280 + "url": "https://github.com/highcharts/highcharts-dist.git",
  281 + "reference": "8ec04ca0aa02fe7e9584dc24b6902e4338360763"
  282 + },
  283 + "dist": {
  284 + "type": "zip",
  285 + "url": "https://api.github.com/repos/highcharts/highcharts-dist/zipball/8ec04ca0aa02fe7e9584dc24b6902e4338360763",
  286 + "reference": "8ec04ca0aa02fe7e9584dc24b6902e4338360763",
  287 + "shasum": ""
  288 + },
  289 + "type": "bower-asset-library",
  290 + "extra": {
  291 + "bower-asset-main": [
  292 + "highcharts.js",
  293 + "highcharts-more.js",
  294 + "modules/exporting.js"
  295 + ]
  296 + }
  297 + },
  298 + {
214 299 "name": "bower-asset/icheck",
215 300 "version": "1.0.2",
216 301 "source": {
... ...