Commit 3cae1b75d497c3e5f2a07e469b6e75cb7eabadf6

Authored by Alexey Boroda
1 parent 05a089dd

-Analytics almost ready

backend/controllers/SettingsController.php deleted
1 -<?php  
2 - namespace backend\controllers;  
3 -  
4 - use common\models\Settings;  
5 - use yii\base\InvalidConfigException;  
6 - use yii\filters\AccessControl;  
7 - use yii\web\Controller;  
8 - use Yii;  
9 -  
10 - /**  
11 - * Class SettingsController  
12 - *  
13 - * @package artbox\core\controllers  
14 - */  
15 - class SettingsController extends Controller  
16 - {  
17 - /**  
18 - * @inheritdoc  
19 - */  
20 - public function behaviors()  
21 - {  
22 - return [  
23 - 'access' => [  
24 - 'class' => AccessControl::className(),  
25 - 'rules' => [  
26 - [  
27 - 'actions' => [  
28 - 'login',  
29 - 'error',  
30 - ],  
31 - 'allow' => true,  
32 - ],  
33 - [  
34 - 'actions' => [  
35 - 'logout',  
36 - 'index',  
37 - ],  
38 - 'allow' => true,  
39 - 'roles' => [ '@' ],  
40 - ],  
41 - ],  
42 - ],  
43 - ];  
44 - }  
45 - /**  
46 - * @inheritdoc  
47 - */  
48 - public function getViewPath()  
49 - {  
50 - return \Yii::getAlias('@artbox/core/views/settings');  
51 - }  
52 -  
53 - /**  
54 - * Display site settings page  
55 - *  
56 - * @return string|\yii\web\Response  
57 - */  
58 - public function actionIndex()  
59 - {  
60 - $model = $this->findSettings();  
61 -  
62 - if ($model->load(Yii::$app->request->post()) && $model->save()) {  
63 - Yii::$app->session->setFlash('success', 'Settings saved');  
64 -  
65 - return $this->goHome();  
66 - }  
67 -  
68 - return $this->render(  
69 - 'settings',  
70 - [  
71 - 'model' => $model,  
72 - ]  
73 - );  
74 - }  
75 -  
76 - /**  
77 - * Find site settings  
78 - *  
79 - * @return \yii2tech\filedb\ActiveRecord  
80 - * @throws \yii\base\InvalidConfigException  
81 - */  
82 - public function findSettings()  
83 - {  
84 - if ($model = Settings::find()  
85 - ->one()  
86 - ) {  
87 - return $model;  
88 - } else {  
89 - throw new InvalidConfigException('Settings file not found');  
90 - }  
91 - }  
92 - }  
93 -  
94 \ No newline at end of file 0 \ No newline at end of file
backend/controllers/SiteController.php
1 <?php 1 <?php
2 namespace backend\controllers; 2 namespace backend\controllers;
3 - 3 +
  4 + use backend\models\Analytics;
  5 + use common\models\Settings;
4 use Yii; 6 use Yii;
5 use yii\web\Controller; 7 use yii\web\Controller;
6 use yii\filters\VerbFilter; 8 use yii\filters\VerbFilter;
7 use yii\filters\AccessControl; 9 use yii\filters\AccessControl;
8 use common\models\LoginForm; 10 use common\models\LoginForm;
9 - 11 +
10 /** 12 /**
11 * Site controller 13 * Site controller
12 */ 14 */
@@ -47,7 +49,7 @@ @@ -47,7 +49,7 @@
47 ], 49 ],
48 ]; 50 ];
49 } 51 }
50 - 52 +
51 /** 53 /**
52 * @inheritdoc 54 * @inheritdoc
53 */ 55 */
@@ -59,7 +61,7 @@ @@ -59,7 +61,7 @@
59 ], 61 ],
60 ]; 62 ];
61 } 63 }
62 - 64 +
63 /** 65 /**
64 * Displays homepage. 66 * Displays homepage.
65 * 67 *
@@ -67,9 +69,22 @@ @@ -67,9 +69,22 @@
67 */ 69 */
68 public function actionIndex() 70 public function actionIndex()
69 { 71 {
70 - return $this->render('index');  
71 - } 72 + $settings = Settings::getInstance();
  73 +
  74 + $analytics = new Analytics(
  75 + [
  76 + 'viewId' => $settings->analytics_key,
  77 + ]
  78 + );
72 79
  80 + return $this->render(
  81 + 'analytics',
  82 + [
  83 + 'data' => $analytics->generateData(),
  84 + ]
  85 + );
  86 + }
  87 +
73 /** 88 /**
74 * Login action. 89 * Login action.
75 * 90 *
@@ -80,7 +95,7 @@ @@ -80,7 +95,7 @@
80 if (!Yii::$app->user->isGuest) { 95 if (!Yii::$app->user->isGuest) {
81 return $this->goHome(); 96 return $this->goHome();
82 } 97 }
83 - 98 +
84 $model = new LoginForm(); 99 $model = new LoginForm();
85 if ($model->load(Yii::$app->request->post()) && $model->login()) { 100 if ($model->load(Yii::$app->request->post()) && $model->login()) {
86 return $this->goBack(); 101 return $this->goBack();
@@ -93,7 +108,7 @@ @@ -93,7 +108,7 @@
93 ); 108 );
94 } 109 }
95 } 110 }
96 - 111 +
97 /** 112 /**
98 * Logout action. 113 * Logout action.
99 * 114 *
@@ -102,10 +117,10 @@ @@ -102,10 +117,10 @@
102 public function actionLogout() 117 public function actionLogout()
103 { 118 {
104 Yii::$app->user->logout(); 119 Yii::$app->user->logout();
105 - 120 +
106 return $this->goHome(); 121 return $this->goHome();
107 } 122 }
108 - 123 +
109 public function actionAnalytic() 124 public function actionAnalytic()
110 { 125 {
111 return $this->render('analytic'); 126 return $this->render('analytic');
backend/models/Analytics.php 0 โ†’ 100644
  1 +<?php
  2 + namespace backend\models;
  3 +
  4 + use yii\base\Model;
  5 +
  6 + class Analytics extends Model
  7 + {
  8 + public $viewId;
  9 +
  10 + private function executeQuery()
  11 + {
  12 + $client = new \Google_Client();
  13 +
  14 + $client->setAuthConfig(\Yii::getAlias('@common/config/Artbox-85b8559147bc.json'));
  15 + $client->addScope(\Google_Service_Analytics::ANALYTICS_READONLY);
  16 +
  17 + $analytics = new \Google_Service_AnalyticsReporting($client);
  18 +
  19 + $profile_id = $this->viewId;
  20 +
  21 + $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  22 + $dateRange->setStartDate("30daysAgo");
  23 + $dateRange->setEndDate("today");
  24 +
  25 + $sessions = new \Google_Service_AnalyticsReporting_Metric();
  26 + $sessions->setExpression('ga:sessions');
  27 + $sessions->setAlias('ะกะตะฐะฝัั‹');
  28 +
  29 + $users = new \Google_Service_AnalyticsReporting_Metric();
  30 + $users->setExpression('ga:users');
  31 + $users->setAlias('ะŸะพะปัŒะทะพะฒะฐั‚ะตะปะธ');
  32 +
  33 + $views = new \Google_Service_AnalyticsReporting_Metric();
  34 + $views->setExpression('ga:pageviews');
  35 + $views->setAlias('ะŸั€ะพัะผะพั‚ั€ั‹');
  36 +
  37 + $new_sessions = new \Google_Service_AnalyticsReporting_Metric();
  38 + $new_sessions->setExpression('ga:percentNewSessions');
  39 + $new_sessions->setAlias('ะะพะฒั‹ะต ัะตััะธะธ');
  40 +
  41 + $dimensions = new \Google_Service_AnalyticsReporting_Dimension();
  42 + $dimensions->setName('ga:date');
  43 +
  44 + $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  45 + $request->setViewId($profile_id);
  46 + $request->setDateRanges($dateRange);
  47 + $request->setMetrics(
  48 + [
  49 + $sessions,
  50 + $users,
  51 + $views,
  52 + $new_sessions,
  53 + ]
  54 + );
  55 + $request->setDimensions($dimensions);
  56 +
  57 + $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  58 + $body->setReportRequests([ $request ]);
  59 + $response = $analytics->reports->batchGet($body);
  60 +
  61 + return $response;
  62 + }
  63 +
  64 + public function generateData()
  65 + {
  66 + $reports = $this->executeQuery();
  67 +
  68 + $data = [];
  69 + for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {
  70 + $report = $reports[ $reportIndex ];
  71 + $header = $report->getColumnHeader();
  72 + // $dimensionHeaders = $header->getDimensions();
  73 + $metricHeaders = $header->getMetricHeader()
  74 + ->getMetricHeaderEntries();
  75 + $rows = $report->getData()
  76 + ->getRows();
  77 + $totals = $report->getData()
  78 + ->getTotals();
  79 + $total_values = $totals[ 0 ]->getValues();
  80 +
  81 + $data[ 'sessions' ] = $total_values[ 0 ];
  82 + $data[ 'users' ] = $total_values[ 1 ];
  83 + $data[ 'views' ] = $total_values[ 2 ];
  84 + $data[ 'new' ] = $total_values[ 3 ];
  85 +
  86 + for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  87 + // $dimensions = $row->getDimensions();
  88 + // for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  89 + // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
  90 + // }
  91 +
  92 + $row = $rows[ $rowIndex ];
  93 + $metrics = $row->getMetrics();
  94 + for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {
  95 + $values = $metrics[ $j ];
  96 + for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {
  97 + $value = $values->getValues()[ $valueIndex ];
  98 + $data[ $valueIndex ][] = (int) $value;
  99 + }
  100 + }
  101 + }
  102 + }
  103 +
  104 + return $data;
  105 + }
  106 + }
0 \ No newline at end of file 107 \ No newline at end of file
backend/views/site/analytic.php renamed to backend/views/site/analytics.php
1 <?php 1 <?php
  2 + /**
  3 + * @var View $this
  4 + * @var array $data
  5 + */
  6 +
2 use artbox\gentelella\widgets\XPanel; 7 use artbox\gentelella\widgets\XPanel;
3 use dosamigos\highcharts\HighCharts; 8 use dosamigos\highcharts\HighCharts;
4 - use yii\helpers\VarDumper;  
5 -  
6 - $client = new Google_Client();  
7 -  
8 - $client->setAuthConfig(\Yii::getAlias('@common/config/Artbox-85b8559147bc.json'));  
9 - $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);  
10 -  
11 - $analytics = new Google_Service_AnalyticsReporting($client);  
12 -  
13 - $profile_id = "119240817";  
14 -  
15 - $dateRange = new Google_Service_AnalyticsReporting_DateRange();  
16 - $dateRange->setStartDate("30daysAgo");  
17 - $dateRange->setEndDate("today");  
18 -  
19 - $sessions = new Google_Service_AnalyticsReporting_Metric();  
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');  
37 -  
38 - $request = new Google_Service_AnalyticsReporting_ReportRequest();  
39 - $request->setViewId($profile_id);  
40 - $request->setDateRanges($dateRange);  
41 - $request->setMetrics(  
42 - [  
43 - $sessions,  
44 - $users,  
45 - $views,  
46 - $new_sessions,  
47 - ]  
48 - );  
49 - $request->setDimensions($dimensions);  
50 -  
51 - $body = new Google_Service_AnalyticsReporting_GetReportsRequest();  
52 - $body->setReportRequests([ $request ]);  
53 - $response = $analytics->reports->batchGet($body);  
54 -  
55 - // VarDumper::dump($response, 10, true);die();  
56 -  
57 - function printResults($reports)  
58 - {  
59 - $data = [];  
60 - for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) {  
61 - $report = $reports[ $reportIndex ];  
62 - $header = $report->getColumnHeader();  
63 - $dimensionHeaders = $header->getDimensions();  
64 - $metricHeaders = $header->getMetricHeader()  
65 - ->getMetricHeaderEntries();  
66 - $rows = $report->getData()  
67 - ->getRows();  
68 - $totals = $report->getData()  
69 - ->getTotals();  
70 - $total_values = $totals[ 0 ]->getValues();  
71 -  
72 - $data[ 'sessions' ] = $total_values[ 0 ];  
73 - $data[ 'users' ] = $total_values[ 1 ];  
74 - $data[ 'views' ] = $total_values[ 2 ];  
75 - $data[ 'new' ] = $total_values[ 3 ];  
76 -  
77 - for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {  
78 - $row = $rows[ $rowIndex ];  
79 - $dimensions = $row->getDimensions();  
80 - $metrics = $row->getMetrics();  
81 - for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {  
82 - // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");  
83 - }  
84 -  
85 - for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) {  
86 - $values = $metrics[ $j ];  
87 - for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) {  
88 - $value = $values->getValues()[ $valueIndex ];  
89 - $data[ $valueIndex ][] = (int) $value;  
90 - }  
91 - }  
92 - }  
93 - }  
94 -  
95 - return $data;  
96 - }  
97 -  
98 - $data = printResults($response->getReports()); 9 + use yii\web\View;
99 10
100 ?> 11 ?>
101 12
102 <div class="row"> 13 <div class="row">
103 - <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">  
104 - <div class="tile-stats">  
105 - <div class="icon"><i class="fa fa-clock-o"></i>  
106 - </div>  
107 - <div class="count"><?= $data[ 'sessions' ] ?></div>  
108 -  
109 - <h3>Sessions</h3>  
110 - <p>Lorem ipsum psdea itgum rixt.</p>  
111 - </div> 14 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  15 + <div class="tile-stats">
  16 + <div class="icon"><i class="fa fa-clock-o"></i>
  17 + </div>
  18 + <div class="count"><?= $data[ 'sessions' ] ?></div>
  19 +
  20 + <h3>Sessions</h3>
  21 + <p>Lorem ipsum psdea itgum rixt.</p>
112 </div> 22 </div>
113 - <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">  
114 - <div class="tile-stats">  
115 - <div class="icon"><i class="fa fa-user"></i>  
116 - </div>  
117 - <div class="count"><?= $data[ 'users' ] ?></div>  
118 -  
119 - <h3>Users</h3>  
120 - <p>Lorem ipsum psdea itgum rixt.</p>  
121 - </div> 23 + </div>
  24 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  25 + <div class="tile-stats">
  26 + <div class="icon"><i class="fa fa-user"></i>
  27 + </div>
  28 + <div class="count"><?= $data[ 'users' ] ?></div>
  29 +
  30 + <h3>Users</h3>
  31 + <p>Lorem ipsum psdea itgum rixt.</p>
122 </div> 32 </div>
123 - <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">  
124 - <div class="tile-stats">  
125 - <div class="icon"><i class="fa fa-eye"></i>  
126 - </div>  
127 - <div class="count"><?= $data[ 'views' ] ?></div>  
128 -  
129 - <h3>Page views</h3>  
130 - <p>Lorem ipsum psdea itgum rixt.</p>  
131 - </div> 33 + </div>
  34 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  35 + <div class="tile-stats">
  36 + <div class="icon"><i class="fa fa-eye"></i>
  37 + </div>
  38 + <div class="count"><?= $data[ 'views' ] ?></div>
  39 +
  40 + <h3>Page views</h3>
  41 + <p>Lorem ipsum psdea itgum rixt.</p>
132 </div> 42 </div>
133 - <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">  
134 - <div class="tile-stats">  
135 - <div class="icon"><i class="fa fa-plus"></i>  
136 - </div>  
137 - <div class="count"><?= round(intval($data[ 'new' ]), 2) ?> %</div>  
138 -  
139 - <h3>New sessions</h3>  
140 - <p>Lorem ipsum psdea itgum rixt.</p>  
141 - </div> 43 + </div>
  44 + <div class="animated flipInY col-lg-3 col-md-3 col-sm-6 col-xs-12">
  45 + <div class="tile-stats">
  46 + <div class="icon"><i class="fa fa-plus"></i>
  47 + </div>
  48 + <div class="count"><?= round(intval($data[ 'new' ]), 2) ?> %</div>
  49 +
  50 + <h3>New sessions</h3>
  51 + <p>Lorem ipsum psdea itgum rixt.</p>
142 </div> 52 </div>
  53 + </div>
143 </div> 54 </div>
144 55
145 <div class="row"> 56 <div class="row">
146 - <div class="col-md-12">  
147 - <?php $panel = XPanel::begin(  
148 - [  
149 - 'title' => 'Analytics',  
150 - 'toolbarLayout' => '{collapse}',  
151 - ]  
152 - ); ?>  
153 -  
154 - <?php  
155 - echo HighCharts::widget(  
156 - [  
157 - 'clientOptions' => [  
158 - 'colors' => [  
159 - '#9ABCC3',  
160 - '#A8E3D6',  
161 - ],  
162 - 'chart' => [  
163 - 'type' => 'area',  
164 - 'zoomType' => 'x',  
165 - ],  
166 - 'title' => [  
167 - 'text' => 'Analytics',  
168 - ],  
169 - 'yAxis' => [  
170 - 'title' => [  
171 - 'text' => 'Sessions count',  
172 - ],  
173 - ],  
174 - 'series' => [  
175 - [  
176 - 'name' => 'Sessions',  
177 - 'data' => $data[ 0 ],  
178 - ],  
179 - [  
180 - 'name' => 'Users',  
181 - 'data' => $data[ 1 ],  
182 - ],  
183 - ],  
184 - 'credits' => [  
185 - 'enabled' => false,  
186 - ],  
187 - 'plotOptions' => [  
188 - 'area' => [  
189 - 'marker' => [  
190 - 'enabled' => false,  
191 - 'symbol' => 'circle',  
192 - 'radius' => 2,  
193 - 'states' => [  
194 - 'hover' => [  
195 - 'enabled' => true,  
196 - ],  
197 - ],  
198 - ],  
199 - ],  
200 - ],  
201 - ],  
202 - ]  
203 - );  
204 - ?>  
205 -  
206 - <?php $panel::end(); ?>  
207 -  
208 - </div> 57 + <div class="col-md-12">
  58 + <?php $panel = XPanel::begin(
  59 + [
  60 + 'title' => 'Analytics',
  61 + 'toolbarLayout' => '{collapse}',
  62 + ]
  63 + ); ?>
  64 +
  65 + <?php
  66 + echo HighCharts::widget(
  67 + [
  68 + 'clientOptions' => [
  69 + 'colors' => [
  70 + '#9ABCC3',
  71 + '#A8E3D6',
  72 + ],
  73 + 'chart' => [
  74 + 'type' => 'area',
  75 + 'zoomType' => 'x',
  76 + ],
  77 + 'title' => [
  78 + 'text' => 'Analytics',
  79 + ],
  80 + 'yAxis' => [
  81 + 'title' => [
  82 + 'text' => 'Sessions count',
  83 + ],
  84 + ],
  85 + 'series' => [
  86 + [
  87 + 'name' => 'Sessions',
  88 + 'data' => $data[ 0 ],
  89 + ],
  90 + [
  91 + 'name' => 'Users',
  92 + 'data' => $data[ 1 ],
  93 + ],
  94 + ],
  95 + 'credits' => [
  96 + 'enabled' => false,
  97 + ],
  98 + 'plotOptions' => [
  99 + 'area' => [
  100 + 'marker' => [
  101 + 'enabled' => false,
  102 + 'symbol' => 'circle',
  103 + 'radius' => 2,
  104 + 'states' => [
  105 + 'hover' => [
  106 + 'enabled' => true,
  107 + ],
  108 + ],
  109 + ],
  110 + ],
  111 + ],
  112 + ],
  113 + ]
  114 + );
  115 + ?>
  116 +
  117 + <?php $panel::end(); ?>
  118 +
  119 + </div>
209 </div> 120 </div>
210 121
211 <div class="row"> 122 <div class="row">
212 - <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">  
213 - <?php $panel = XPanel::begin(  
214 - [  
215 - 'title' => 'Analytics',  
216 - 'toolbarLayout' => '{collapse}',  
217 - ]  
218 - ); ?>  
219 -  
220 - <?php  
221 - echo HighCharts::widget(  
222 - [  
223 - 'clientOptions' => [  
224 - 'exporting' => [  
225 - 'enabled' => false,  
226 - ],  
227 - 'colors' => [  
228 - '#9ABCC3',  
229 - '#A8E3D6',  
230 - ],  
231 - 'chart' => [  
232 - 'plotBackgroundColor' => null,  
233 - 'plotBorderWidth' => null,  
234 - 'plotShadow' => false,  
235 - 'type' => 'pie',  
236 - ],  
237 - 'title' => [  
238 - 'text' => 'Analytics',  
239 - ],  
240 - 'series' => [  
241 - [  
242 - 'name' => 'Sessions',  
243 - 'data' => [  
244 - [  
245 - 'name' => 'New visitor',  
246 - 'y' => round(intval($data[ 'new' ]), 2),  
247 - ],  
248 - [  
249 - 'name' => 'Returning Visitor',  
250 - 'y' => 100 - round(intval($data[ 'new' ]), 2),  
251 - ],  
252 - ],  
253 - ],  
254 - ],  
255 - 'credits' => [  
256 - 'enabled' => false,  
257 - ],  
258 - 'plotOptions' => [  
259 - 'pie' => [  
260 - 'allowPointSelect' => true,  
261 - 'cursor' => 'pointer',  
262 - ],  
263 - ],  
264 - ],  
265 - ]  
266 - );  
267 - ?>  
268 -  
269 - <?php $panel::end(); ?>  
270 - </div> 123 + <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12">
  124 + <?php $panel = XPanel::begin(
  125 + [
  126 + 'title' => 'Analytics',
  127 + 'toolbarLayout' => '{collapse}',
  128 + ]
  129 + ); ?>
  130 +
  131 + <?php
  132 + echo HighCharts::widget(
  133 + [
  134 + 'clientOptions' => [
  135 + 'exporting' => [
  136 + 'enabled' => false,
  137 + ],
  138 + 'colors' => [
  139 + '#9ABCC3',
  140 + '#A8E3D6',
  141 + ],
  142 + 'chart' => [
  143 + 'plotBackgroundColor' => null,
  144 + 'plotBorderWidth' => null,
  145 + 'plotShadow' => false,
  146 + 'type' => 'pie',
  147 + ],
  148 + 'title' => [
  149 + 'text' => 'Analytics',
  150 + ],
  151 + 'series' => [
  152 + [
  153 + 'name' => 'Sessions',
  154 + 'data' => [
  155 + [
  156 + 'name' => 'New visitor',
  157 + 'y' => round(intval($data[ 'new' ]), 2),
  158 + ],
  159 + [
  160 + 'name' => 'Returning Visitor',
  161 + 'y' => 100 - round(intval($data[ 'new' ]), 2),
  162 + ],
  163 + ],
  164 + ],
  165 + ],
  166 + 'credits' => [
  167 + 'enabled' => false,
  168 + ],
  169 + 'plotOptions' => [
  170 + 'pie' => [
  171 + 'allowPointSelect' => true,
  172 + 'cursor' => 'pointer',
  173 + ],
  174 + ],
  175 + ],
  176 + ]
  177 + );
  178 + ?>
  179 +
  180 + <?php $panel::end(); ?>
  181 + </div>
271 182
272 </div> 183 </div>
273 \ No newline at end of file 184 \ No newline at end of file
backend/views/site/index.php
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 /* @var $this yii\web\View */ 3 /* @var $this yii\web\View */
4 4
  5 + use backend\models\Analytics;
  6 +
5 $this->title = 'My Yii Application'; 7 $this->title = 'My Yii Application';
6 8
7 9
8 \ No newline at end of file 10 \ No newline at end of file
common/config/.gitignore
@@ -2,4 +2,4 @@ main-local.php @@ -2,4 +2,4 @@ main-local.php
2 db* 2 db*
3 params-local.php 3 params-local.php
4 test-local.php 4 test-local.php
5 -settings.php  
6 \ No newline at end of file 5 \ No newline at end of file
  6 +#settings.php
7 \ No newline at end of file 7 \ No newline at end of file
common/config/settings.php
@@ -5,7 +5,7 @@ return [ @@ -5,7 +5,7 @@ return [
5 'id' => '1', 5 'id' => '1',
6 'name' => 'Admin321', 6 'name' => 'Admin321',
7 'description' => 'Site administrator', 7 'description' => 'Site administrator',
8 - 'analytics' => '<!-- Google Analytics --> 8 + 'analytics' => '<!-- Google Analytics -->
9 <script> 9 <script>
10 (function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){ 10 (function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
11 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 11 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
@@ -16,5 +16,6 @@ return [ @@ -16,5 +16,6 @@ return [
16 ga(\'send\', \'pageview\'); 16 ga(\'send\', \'pageview\');
17 </script> 17 </script>
18 <!-- End Google Analytics -->', 18 <!-- End Google Analytics -->',
  19 + 'analytics_key' => '',
19 ], 20 ],
20 ]; 21 ];
21 \ No newline at end of file 22 \ No newline at end of file
common/models/Settings.php
@@ -6,15 +6,19 @@ @@ -6,15 +6,19 @@
6 6
7 /** 7 /**
8 * Class Settings 8 * Class Settings
  9 +
9 * 10 *
10 - * @package artbox\core\models 11 +*@package artbox\core\models
11 * @property string $name 12 * @property string $name
12 * @property string $id 13 * @property string $id
13 * @property string $description 14 * @property string $description
14 * @property string $analytics 15 * @property string $analytics
  16 + * @property string $analytics_key
15 */ 17 */
16 class Settings extends ActiveRecord 18 class Settings extends ActiveRecord
17 { 19 {
  20 + private static $instance;
  21 +
18 /** 22 /**
19 * @inheritdoc 23 * @inheritdoc
20 */ 24 */
@@ -27,6 +31,7 @@ @@ -27,6 +31,7 @@
27 'description', 31 'description',
28 'id', 32 'id',
29 'analytics', 33 'analytics',
  34 + 'analytics_key',
30 ], 35 ],
31 'string', 36 'string',
32 ], 37 ],
@@ -51,6 +56,7 @@ @@ -51,6 +56,7 @@
51 'name' => Yii::t('core', 'Name'), 56 'name' => Yii::t('core', 'Name'),
52 'description' => Yii::t('core', 'Description'), 57 'description' => Yii::t('core', 'Description'),
53 'analytics' => Yii::t('core', 'Google Analytics Code'), 58 'analytics' => Yii::t('core', 'Google Analytics Code'),
  59 + 'analytics_key' => Yii::t('core', 'Google Analytics Key'),
54 ]; 60 ];
55 } 61 }
56 62
@@ -61,7 +67,12 @@ @@ -61,7 +67,12 @@
61 */ 67 */
62 public static function getInstance() 68 public static function getInstance()
63 { 69 {
64 - return self::findOne([ 'id' => 1 ]); 70 + if (empty(self::$instance)) {
  71 + self::$instance = self::findOne([ 'id' => 1 ]);
  72 + return self::$instance;
  73 + }
  74 +
  75 + return self::$instance;
65 } 76 }
66 } 77 }
67 78
68 \ No newline at end of file 79 \ No newline at end of file