Commit 5e021d7a25fe00ba46ee26ba02150aab7febc0f6

Authored by Alexey Boroda
1 parent 3e16e218

-Tabs for analytics in progress

backend/controllers/SiteController.php
... ... @@ -79,11 +79,24 @@
79 79 'viewId' => $settings->analytics_key,
80 80 ]
81 81 );
  82 + $data = $analytics->generateData();
82 83  
  84 + $browsers = $data[ 'table' ][ 0 ];
  85 + arsort($browsers);
  86 +
  87 + $cityes = $data[ 'table' ][ 1 ];
  88 + arsort($cityes);
  89 +
  90 + $countries = $data[ 'table' ][ 2 ];
  91 + arsort($countries);
  92 +
83 93 return $this->render(
84 94 'analytics',
85 95 [
86   - 'data' => $analytics->generateData(),
  96 + 'data' => $data,
  97 + 'browsers' => $browsers,
  98 + 'cityes' => $cityes,
  99 + 'countries' => $countries,
87 100 ]
88 101 );
89 102 }
... ... @@ -99,7 +112,7 @@
99 112 if (!Yii::$app->user->isGuest) {
100 113 return $this->goHome();
101 114 }
102   -
  115 +
103 116 $model = new LoginForm();
104 117 if ($model->load(Yii::$app->request->post()) && $model->login()) {
105 118 return $this->goBack();
... ... @@ -121,7 +134,7 @@
121 134 public function actionLogout()
122 135 {
123 136 Yii::$app->user->logout();
124   -
  137 +
125 138 return $this->goHome();
126 139 }
127 140  
... ...
backend/models/Analytics.php
... ... @@ -2,6 +2,7 @@
2 2 namespace backend\models;
3 3  
4 4 use yii\base\Model;
  5 + use yii\helpers\ArrayHelper;
5 6 use yii\helpers\VarDumper;
6 7  
7 8 class Analytics extends Model
... ... @@ -85,6 +86,12 @@
85 86 $country,
86 87 ]
87 88 );
  89 +
  90 + $ordering = new \Google_Service_AnalyticsReporting_OrderBy();
  91 + $ordering->setFieldName("ga:sessions");
  92 + $ordering->setOrderType("VALUE");
  93 + $ordering->setSortOrder("DESCENDING");
  94 + $request2->setOrderBys($ordering);
88 95  
89 96 $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
90 97 $body->setReportRequests(
... ... @@ -104,6 +111,9 @@
104 111  
105 112 $data = [];
106 113  
  114 + /**
  115 + * Generating data for Sessions and users
  116 + */
107 117 $report = $reports[ 0 ];
108 118 $header = $report->getColumnHeader();
109 119 // $dimensionHeaders = $header->getDimensions();
... ... @@ -138,8 +148,42 @@
138 148 }
139 149  
140 150 /**
141   - * @todo Get data from second query
  151 + * Generating data for
142 152 */
  153 + $data2 = [];
  154 +
  155 + $report = $reports[ 1 ];
  156 + $header = $report->getColumnHeader();
  157 + $dimensionHeaders = $header->getDimensions();
  158 + $metricHeaders = $header->getMetricHeader()
  159 + ->getMetricHeaderEntries();
  160 + $rows = $report->getData()
  161 + ->getRows();
  162 +
  163 + for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  164 + $row = $rows[ $rowIndex ];
  165 +
  166 + $metrics = $row->getMetrics();
  167 + for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {
  168 + $values = $metrics[ $j ];
  169 + for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {
  170 + $value = $values->getValues()[ $valueIndex ];
  171 + $currentValue = (int) $value;
  172 + }
  173 + }
  174 +
  175 + $dimensions = $row->getDimensions();
  176 + foreach ($dimensions as $key => $dimension) {
  177 + if (empty($data2[ $key ][ $dimension ])) {
  178 + $data2[ $key ][ $dimension ] = $currentValue;
  179 + } else {
  180 + $data2[ $key ][ $dimension ] += $currentValue;
  181 + }
  182 + }
  183 + }
  184 +
  185 + $data[ 'table' ] = $data2;
  186 +
143 187 return $data;
144 188 }
145 189 }
146 190 \ No newline at end of file
... ...
backend/views/site/analytics.php
... ... @@ -6,6 +6,8 @@
6 6  
7 7 use artbox\gentelella\widgets\XPanel;
8 8 use dosamigos\highcharts\HighCharts;
  9 + use yii\bootstrap\Html;
  10 + use yii\bootstrap\Tabs;
9 11 use yii\web\View;
10 12  
11 13 ?>
... ... @@ -179,5 +181,20 @@
179 181  
180 182 <?php $panel::end(); ?>
181 183 </div>
  184 +
  185 + <div class="col-lg-7 col-md-7 col-sm-12 col-xs-12">
  186 +
  187 + <?php $panel = XPanel::begin(
  188 + [
  189 + 'title' => 'Analytics',
  190 + 'toolbarLayout' => false,
  191 + ]
  192 + ); ?>
  193 +
  194 + <?php Tabs::widget(); ?>
  195 +
  196 + <?php $panel::end(); ?>
  197 +
  198 + </div>
182 199  
183 200 </div>
184 201 \ No newline at end of file
... ...