yii2-ga
Google analytics component for Yii2, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist atlasmobile/yii2-ga "*"
or add, (*4)
"atlasmobile/yii2-ga": "*"
to the require section of your composer.json
file., (*5)
Application params
To use this extension, add the following code in your application params:, (*6)
return [
...
'googleAnalytics' => [
'developerKey' => '', // Public key
'clientId' => 'xxx.apps.googleusercontent.com', // Client ID
'analyticsId' => 'ga:xxxxxxxxx', //(It is the number at the end of the URL starting with p: https://www.google.com/analytics/web/#home/a33443w112345pXXXXXXXX/)
'serviceAccountName' => 'xxx@xxx.gserviceaccount.com', // Email address
'privateKeyPath' => '', //path to private key in p12 format
],
];
Add the serviceAccountName (xxx@dxxx.gserviceaccount.com) as a new user to your Analyics property., (*7)
Usage
use atlasmobile\analytics\Analytics;
class Test
{
public function example()
{
$analytics = new Analytics();
$analytics->startDate = '';
$analytics->endDate = '';
$sessionsData = $analytics->getSessions();
$visitorsData = $analytics->getUsers();
$pageViewsData = $analytics->getPageViews();
$avgSessionsDurationData = $analytics->getAvgSessionDuration();
$countriesData = $analytics->getCountries();
}
}
Parameters 'startDate' and 'endDate' are set by default for yesterday, (*8)
Useful links
Analytics Core Reporting API
Google API Php Client, (*9)