diff --git a/backend/controllers/SettingsController.php b/backend/controllers/SettingsController.php deleted file mode 100755 index f5a7bbd..0000000 --- a/backend/controllers/SettingsController.php +++ /dev/null @@ -1,93 +0,0 @@ - [ - 'class' => AccessControl::className(), - 'rules' => [ - [ - 'actions' => [ - 'login', - 'error', - ], - 'allow' => true, - ], - [ - 'actions' => [ - 'logout', - 'index', - ], - 'allow' => true, - 'roles' => [ '@' ], - ], - ], - ], - ]; - } - /** - * @inheritdoc - */ - public function getViewPath() - { - return \Yii::getAlias('@artbox/core/views/settings'); - } - - /** - * Display site settings page - * - * @return string|\yii\web\Response - */ - public function actionIndex() - { - $model = $this->findSettings(); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { - Yii::$app->session->setFlash('success', 'Settings saved'); - - return $this->goHome(); - } - - return $this->render( - 'settings', - [ - 'model' => $model, - ] - ); - } - - /** - * Find site settings - * - * @return \yii2tech\filedb\ActiveRecord - * @throws \yii\base\InvalidConfigException - */ - public function findSettings() - { - if ($model = Settings::find() - ->one() - ) { - return $model; - } else { - throw new InvalidConfigException('Settings file not found'); - } - } - } - \ No newline at end of file diff --git a/backend/controllers/SiteController.php b/backend/controllers/SiteController.php index 6b1ff44..7b274cb 100755 --- a/backend/controllers/SiteController.php +++ b/backend/controllers/SiteController.php @@ -1,12 +1,14 @@ render('index'); - } + $settings = Settings::getInstance(); + + $analytics = new Analytics( + [ + 'viewId' => $settings->analytics_key, + ] + ); + return $this->render( + 'analytics', + [ + 'data' => $analytics->generateData(), + ] + ); + } + /** * Login action. * @@ -80,7 +95,7 @@ if (!Yii::$app->user->isGuest) { return $this->goHome(); } - + $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); @@ -93,7 +108,7 @@ ); } } - + /** * Logout action. * @@ -102,10 +117,10 @@ public function actionLogout() { Yii::$app->user->logout(); - + return $this->goHome(); } - + public function actionAnalytic() { return $this->render('analytic'); diff --git a/backend/models/Analytics.php b/backend/models/Analytics.php new file mode 100644 index 0000000..d16a7a4 --- /dev/null +++ b/backend/models/Analytics.php @@ -0,0 +1,106 @@ +setAuthConfig(\Yii::getAlias('@common/config/Artbox-85b8559147bc.json')); + $client->addScope(\Google_Service_Analytics::ANALYTICS_READONLY); + + $analytics = new \Google_Service_AnalyticsReporting($client); + + $profile_id = $this->viewId; + + $dateRange = new \Google_Service_AnalyticsReporting_DateRange(); + $dateRange->setStartDate("30daysAgo"); + $dateRange->setEndDate("today"); + + $sessions = new \Google_Service_AnalyticsReporting_Metric(); + $sessions->setExpression('ga:sessions'); + $sessions->setAlias('Сеансы'); + + $users = new \Google_Service_AnalyticsReporting_Metric(); + $users->setExpression('ga:users'); + $users->setAlias('Пользователи'); + + $views = new \Google_Service_AnalyticsReporting_Metric(); + $views->setExpression('ga:pageviews'); + $views->setAlias('Просмотры'); + + $new_sessions = new \Google_Service_AnalyticsReporting_Metric(); + $new_sessions->setExpression('ga:percentNewSessions'); + $new_sessions->setAlias('Новые сессии'); + + $dimensions = new \Google_Service_AnalyticsReporting_Dimension(); + $dimensions->setName('ga:date'); + + $request = new \Google_Service_AnalyticsReporting_ReportRequest(); + $request->setViewId($profile_id); + $request->setDateRanges($dateRange); + $request->setMetrics( + [ + $sessions, + $users, + $views, + $new_sessions, + ] + ); + $request->setDimensions($dimensions); + + $body = new \Google_Service_AnalyticsReporting_GetReportsRequest(); + $body->setReportRequests([ $request ]); + $response = $analytics->reports->batchGet($body); + + return $response; + } + + public function generateData() + { + $reports = $this->executeQuery(); + + $data = []; + for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) { + $report = $reports[ $reportIndex ]; + $header = $report->getColumnHeader(); + // $dimensionHeaders = $header->getDimensions(); + $metricHeaders = $header->getMetricHeader() + ->getMetricHeaderEntries(); + $rows = $report->getData() + ->getRows(); + $totals = $report->getData() + ->getTotals(); + $total_values = $totals[ 0 ]->getValues(); + + $data[ 'sessions' ] = $total_values[ 0 ]; + $data[ 'users' ] = $total_values[ 1 ]; + $data[ 'views' ] = $total_values[ 2 ]; + $data[ 'new' ] = $total_values[ 3 ]; + + for ($rowIndex = 0; $rowIndex < count($rows); $rowIndex++) { + // $dimensions = $row->getDimensions(); + // for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) { + // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n"); + // } + + $row = $rows[ $rowIndex ]; + $metrics = $row->getMetrics(); + for ($j = 0; $j < count($metricHeaders) && $j < count($metrics); $j++) { + $values = $metrics[ $j ]; + for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) { + $value = $values->getValues()[ $valueIndex ]; + $data[ $valueIndex ][] = (int) $value; + } + } + } + } + + return $data; + } + } \ No newline at end of file diff --git a/backend/views/site/analytic.php b/backend/views/site/analytic.php deleted file mode 100644 index 69ff4c8..0000000 --- a/backend/views/site/analytic.php +++ /dev/null @@ -1,272 +0,0 @@ -setAuthConfig(\Yii::getAlias('@common/config/Artbox-85b8559147bc.json')); - $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY); - - $analytics = new Google_Service_AnalyticsReporting($client); - - $profile_id = "119240817"; - - $dateRange = new Google_Service_AnalyticsReporting_DateRange(); - $dateRange->setStartDate("30daysAgo"); - $dateRange->setEndDate("today"); - - $sessions = new Google_Service_AnalyticsReporting_Metric(); - $sessions->setExpression('ga:sessions'); - $sessions->setAlias('Сеансы'); - - $users = new Google_Service_AnalyticsReporting_Metric(); - $users->setExpression('ga:users'); - $users->setAlias('Пользователи'); - - $views = new Google_Service_AnalyticsReporting_Metric(); - $views->setExpression('ga:pageviews'); - $views->setAlias('Просмотры'); - - $new_sessions = new Google_Service_AnalyticsReporting_Metric(); - $new_sessions->setExpression('ga:percentNewSessions'); - $new_sessions->setAlias('Новые сессии'); - - $dimensions = new Google_Service_AnalyticsReporting_Dimension(); - $dimensions->setName('ga:date'); - - $request = new Google_Service_AnalyticsReporting_ReportRequest(); - $request->setViewId($profile_id); - $request->setDateRanges($dateRange); - $request->setMetrics( - [ - $sessions, - $users, - $views, - $new_sessions, - ] - ); - $request->setDimensions($dimensions); - - $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); - $body->setReportRequests([ $request ]); - $response = $analytics->reports->batchGet($body); - - // VarDumper::dump($response, 10, true);die(); - - function printResults($reports) - { - $data = []; - for ($reportIndex = 0; $reportIndex < count($reports); $reportIndex++) { - $report = $reports[ $reportIndex ]; - $header = $report->getColumnHeader(); - $dimensionHeaders = $header->getDimensions(); - $metricHeaders = $header->getMetricHeader() - ->getMetricHeaderEntries(); - $rows = $report->getData() - ->getRows(); - $totals = $report->getData() - ->getTotals(); - $total_values = $totals[ 0 ]->getValues(); - - $data[ 'sessions' ] = $total_values[ 0 ]; - $data[ 'users' ] = $total_values[ 1 ]; - $data[ 'views' ] = $total_values[ 2 ]; - $data[ 'new' ] = $total_values[ 3 ]; - - 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++) { - $values = $metrics[ $j ]; - for ($valueIndex = 0; $valueIndex < count($values->getValues()); $valueIndex++) { - $value = $values->getValues()[ $valueIndex ]; - $data[ $valueIndex ][] = (int) $value; - } - } - } - } - - return $data; - } - - $data = printResults($response->getReports()); - -?> - -
-
-
-
-
-
- -

Sessions

-

Lorem ipsum psdea itgum rixt.

-
-
-
-
-
-
-
- -

Users

-

Lorem ipsum psdea itgum rixt.

-
-
-
-
-
-
-
- -

Page views

-

Lorem ipsum psdea itgum rixt.

-
-
-
-
-
-
-
%
- -

New sessions

-

Lorem ipsum psdea itgum rixt.

-
-
-
- -
-
- 'Analytics', - 'toolbarLayout' => '{collapse}', - ] - ); ?> - - [ - 'colors' => [ - '#9ABCC3', - '#A8E3D6', - ], - 'chart' => [ - 'type' => 'area', - 'zoomType' => 'x', - ], - 'title' => [ - 'text' => 'Analytics', - ], - 'yAxis' => [ - 'title' => [ - 'text' => 'Sessions count', - ], - ], - 'series' => [ - [ - 'name' => 'Sessions', - 'data' => $data[ 0 ], - ], - [ - 'name' => 'Users', - 'data' => $data[ 1 ], - ], - ], - 'credits' => [ - 'enabled' => false, - ], - 'plotOptions' => [ - 'area' => [ - 'marker' => [ - 'enabled' => false, - 'symbol' => 'circle', - 'radius' => 2, - 'states' => [ - 'hover' => [ - 'enabled' => true, - ], - ], - ], - ], - ], - ], - ] - ); - ?> - - - -
-
- -
-
- 'Analytics', - 'toolbarLayout' => '{collapse}', - ] - ); ?> - - [ - 'exporting' => [ - 'enabled' => false, - ], - 'colors' => [ - '#9ABCC3', - '#A8E3D6', - ], - 'chart' => [ - 'plotBackgroundColor' => null, - 'plotBorderWidth' => null, - 'plotShadow' => false, - 'type' => 'pie', - ], - 'title' => [ - 'text' => 'Analytics', - ], - 'series' => [ - [ - 'name' => 'Sessions', - 'data' => [ - [ - 'name' => 'New visitor', - 'y' => round(intval($data[ 'new' ]), 2), - ], - [ - 'name' => 'Returning Visitor', - 'y' => 100 - round(intval($data[ 'new' ]), 2), - ], - ], - ], - ], - 'credits' => [ - 'enabled' => false, - ], - 'plotOptions' => [ - 'pie' => [ - 'allowPointSelect' => true, - 'cursor' => 'pointer', - ], - ], - ], - ] - ); - ?> - - -
- -
\ No newline at end of file diff --git a/backend/views/site/analytics.php b/backend/views/site/analytics.php new file mode 100644 index 0000000..f1d64e1 --- /dev/null +++ b/backend/views/site/analytics.php @@ -0,0 +1,183 @@ + + +
+
+
+
+
+
+ +

Sessions

+

Lorem ipsum psdea itgum rixt.

+
+
+
+
+
+
+
+ +

Users

+

Lorem ipsum psdea itgum rixt.

+
+
+
+
+
+
+
+ +

Page views

+

Lorem ipsum psdea itgum rixt.

+
+
+
+
+
+
+
%
+ +

New sessions

+

Lorem ipsum psdea itgum rixt.

+
+
+
+ +
+
+ 'Analytics', + 'toolbarLayout' => '{collapse}', + ] + ); ?> + + [ + 'colors' => [ + '#9ABCC3', + '#A8E3D6', + ], + 'chart' => [ + 'type' => 'area', + 'zoomType' => 'x', + ], + 'title' => [ + 'text' => 'Analytics', + ], + 'yAxis' => [ + 'title' => [ + 'text' => 'Sessions count', + ], + ], + 'series' => [ + [ + 'name' => 'Sessions', + 'data' => $data[ 0 ], + ], + [ + 'name' => 'Users', + 'data' => $data[ 1 ], + ], + ], + 'credits' => [ + 'enabled' => false, + ], + 'plotOptions' => [ + 'area' => [ + 'marker' => [ + 'enabled' => false, + 'symbol' => 'circle', + 'radius' => 2, + 'states' => [ + 'hover' => [ + 'enabled' => true, + ], + ], + ], + ], + ], + ], + ] + ); + ?> + + + +
+
+ +
+
+ 'Analytics', + 'toolbarLayout' => '{collapse}', + ] + ); ?> + + [ + 'exporting' => [ + 'enabled' => false, + ], + 'colors' => [ + '#9ABCC3', + '#A8E3D6', + ], + 'chart' => [ + 'plotBackgroundColor' => null, + 'plotBorderWidth' => null, + 'plotShadow' => false, + 'type' => 'pie', + ], + 'title' => [ + 'text' => 'Analytics', + ], + 'series' => [ + [ + 'name' => 'Sessions', + 'data' => [ + [ + 'name' => 'New visitor', + 'y' => round(intval($data[ 'new' ]), 2), + ], + [ + 'name' => 'Returning Visitor', + 'y' => 100 - round(intval($data[ 'new' ]), 2), + ], + ], + ], + ], + 'credits' => [ + 'enabled' => false, + ], + 'plotOptions' => [ + 'pie' => [ + 'allowPointSelect' => true, + 'cursor' => 'pointer', + ], + ], + ], + ] + ); + ?> + + +
+ +
\ No newline at end of file diff --git a/backend/views/site/index.php b/backend/views/site/index.php index 599051a..6719567 100755 --- a/backend/views/site/index.php +++ b/backend/views/site/index.php @@ -2,6 +2,8 @@ /* @var $this yii\web\View */ + use backend\models\Analytics; + $this->title = 'My Yii Application'; \ No newline at end of file diff --git a/common/config/.gitignore b/common/config/.gitignore index b2e5776..0be4f93 100644 --- a/common/config/.gitignore +++ b/common/config/.gitignore @@ -2,4 +2,4 @@ main-local.php db* params-local.php test-local.php -settings.php \ No newline at end of file +#settings.php \ No newline at end of file diff --git a/common/config/settings.php b/common/config/settings.php index 068e635..e35c03a 100644 --- a/common/config/settings.php +++ b/common/config/settings.php @@ -5,7 +5,7 @@ return [ 'id' => '1', 'name' => 'Admin321', 'description' => 'Site administrator', - 'analytics' => ' + 'analytics' => ' ', + 'analytics_key' => '', ], ]; \ No newline at end of file diff --git a/common/models/Settings.php b/common/models/Settings.php index ae2b93d..b1772ac 100755 --- a/common/models/Settings.php +++ b/common/models/Settings.php @@ -6,15 +6,19 @@ /** * Class Settings + * - * @package artbox\core\models +*@package artbox\core\models * @property string $name * @property string $id * @property string $description * @property string $analytics + * @property string $analytics_key */ class Settings extends ActiveRecord { + private static $instance; + /** * @inheritdoc */ @@ -27,6 +31,7 @@ 'description', 'id', 'analytics', + 'analytics_key', ], 'string', ], @@ -51,6 +56,7 @@ 'name' => Yii::t('core', 'Name'), 'description' => Yii::t('core', 'Description'), 'analytics' => Yii::t('core', 'Google Analytics Code'), + 'analytics_key' => Yii::t('core', 'Google Analytics Key'), ]; } @@ -61,7 +67,12 @@ */ public static function getInstance() { - return self::findOne([ 'id' => 1 ]); + if (empty(self::$instance)) { + self::$instance = self::findOne([ 'id' => 1 ]); + return self::$instance; + } + + return self::$instance; } } \ No newline at end of file -- libgit2 0.21.4