├── Analytics.php ├── AnalyticsAsset.php ├── README.md ├── assets ├── css │ └── analytics.css └── js │ ├── analytics.js │ └── main.js ├── composer.json ├── messages ├── en │ └── infoweb │ │ └── analytics.php ├── fr │ └── infoweb │ │ └── analytics.php ├── id │ └── infoweb │ │ └── analytics.php └── nl │ └── infoweb │ └── analytics.php ├── models └── Connect.php └── views ├── average_session_length.php ├── countries.php ├── sessions.php ├── total_page_views.php ├── total_sessions.php ├── total_users.php └── visitors.php /Analytics.php: -------------------------------------------------------------------------------- 1 | getView(); 35 | 36 | $connection = new Connect; 37 | 38 | // Set default values 39 | // @todo Find a better way to do this 40 | if (!isset($this->startDate)) { 41 | $connection->startDate = date('Y-m-d', strtotime('-1 month')); 42 | } else { 43 | $connection->startDate = $this->startDate; 44 | } 45 | 46 | if (!isset($this->endDate)) { 47 | $connection->endDate = date('Y-m-d'); 48 | } else { 49 | $connection->endDate = $this->endDate; 50 | } 51 | 52 | // Get analytics data 53 | switch ($this->dataType) { 54 | 55 | case Analytics::SESSIONS: 56 | $data = $connection->getSessions(); 57 | 58 | // Set javascript variable 59 | // @todo Find a better way to do this 60 | $view->registerJs("var sessionsData = {$data}", \yii\web\View::POS_HEAD); 61 | 62 | break; 63 | 64 | case Analytics::VISITORS: 65 | $data = $connection->getVisitors(); 66 | 67 | // Set javascript variable 68 | // @todo Find a better way to do this 69 | $view->registerJs("var visitorsData = {$data}", \yii\web\View::POS_HEAD); 70 | 71 | break; 72 | 73 | case Analytics::COUNTRIES: 74 | $data = $connection->getCountries(); 75 | 76 | // Set javascript variable 77 | // @todo Find a better way to do this 78 | $view->registerJs("var countriesData = {$data}", \yii\web\View::POS_HEAD); 79 | 80 | break; 81 | 82 | case Analytics::TOTAl_SESSIONS: 83 | $data = $connection->getTotalSessions(); 84 | 85 | // Set javascript variable 86 | // @todo Find a better way to do this 87 | $view->registerJs("var totalSessionsData = {$data}", \yii\web\View::POS_HEAD); 88 | 89 | break; 90 | 91 | case Analytics::TOTAL_USERS: 92 | $data = $connection->getTotalUsers(); 93 | 94 | // Set javascript variable 95 | // @todo Find a better way to do this 96 | $view->registerJs("var totalUsersData = {$data}", \yii\web\View::POS_HEAD); 97 | 98 | break; 99 | 100 | case Analytics::TOTAL_PAGE_VIEWS: 101 | $data = $connection->getTotalPageViews(); 102 | 103 | // Set javascript variable 104 | // @todo Find a better way to do this 105 | $view->registerJs("var totalPageViewsData = {$data}", \yii\web\View::POS_HEAD); 106 | 107 | break; 108 | 109 | case Analytics::AVERAGE_SESSION_LENGTH: 110 | $data = $connection->getAverageSessionLength(); 111 | 112 | // Set javascript variable 113 | // @todo Find a better way to do this 114 | $view->registerJs("var averageSessionLengthData = {$data}", \yii\web\View::POS_HEAD); 115 | 116 | break; 117 | } 118 | 119 | // Register asssets 120 | AnalyticsAsset::register($this->view); 121 | 122 | } 123 | 124 | /** 125 | * Run the widget 126 | * 127 | * @return string 128 | */ 129 | public function run() 130 | { 131 | return $this->render($this->dataType); 132 | 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /AnalyticsAsset.php: -------------------------------------------------------------------------------- 1 | API's' and enable the 'Analytics API' 33 | 34 | Go to 'API & Auth -> Credentials' and under 'OAuth' click 'Create new Client ID' 35 | 36 | Choose 'Service account' and click 'Create client id' 37 | 38 | Save the certificate to 'backend\assets\certificate\certificate.p12' (don't forget to rename the file) 39 | 40 | Write down the 'private key's password' somewhere 41 | 42 | Add the credentials to your backend params 43 | 44 | ```php 45 | return [ 46 | ... 47 | 'analytics' => [ 48 | 'developerKey' => '', // Public key fingerprints 49 | 'serviceAccountName' => 'xxx@developer.gserviceaccount.com', // Email address 50 | 'clientId' => 'xxx.apps.googleusercontent.com', // Client ID 51 | ], 52 | ]; 53 | ``` 54 | 55 | Go to [Google analytics](https://www.google.com/analytics/), open your property and get your 'Profile ID' 56 | 57 | (It is the number at the end of the URL starting with p: https://www.google.com/analytics/web/#home/a11345062w43527078pXXXXXXXX/) 58 | 59 | Add the 'Profile ID' to your params 60 | 61 | ```php 62 | return [ 63 | ... 64 | 'analytics' => [ 65 | ... 66 | 'analyticsId' => 'ga:XXXXXXXX', 67 | ], 68 | ]; 69 | ``` 70 | 71 | 72 | Add the serviceAccountName (xxx@developer.gserviceaccount.com) as a new user to your Analyics property 73 | 74 | 75 | Create the alias '@google/api' in the bootstrap file in common/config like so: 76 | ```php 77 | Yii::setAlias('google/api', dirname(dirname(__DIR__)) . '/vendor/google/apiclient/src'); 78 | ``` 79 | 80 | Import the translations and use category 'infoweb/analytics': 81 | ``` 82 | yii i18n/import @infoweb/analytics/messages 83 | ``` 84 | 85 | If you can't access the `/tmp` folder on your server (shared hosting), change line 94 in `vendor\google\apiclient\src\Google\` 86 | ```php 87 | 'directory' => dirname(Yii::getAlias('@webroot')) . '/runtime/Google_Client' 88 | ``` 89 | 90 | Usage 91 | ----- 92 | 93 | Once the extension is installed, replace the contents of `backend/views/site/index.php` with the following: 94 | 95 | ```php 96 | title = Yii::$app->name; 100 | ?> 101 | ``` 102 | 103 | ```php 104 |