PHP code example of xcartman / yii2-google-analitics

1. Go to this page and download the library: Download xcartman/yii2-google-analitics library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xcartman / yii2-google-analitics example snippets


return [
    //...
    'components' => [
        //...
        'ga' => [
            'class' => 'xcartman\ga\GoogleAnalitics',
			//https://ga-dev-tools.appspot.com/account-explorer/
            'viewId' => '',
			//https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php#1_enable_the_api			
			'privateKey' => '' 
        ],
    ],
];

Yii::$app->ga->begin
	->dateRange('7daysAgo', 'today')
	->metric('ga:sessions')
	->dimension('ga:browser')
	->request()
	->printReports();

for($i=0;$i<5;$i++){
    Yii::$app->ga->begin <-- use begin 
}

for($i=0;$i<5;$i++){
    $googleAnalitics = clone Yii::$app->ga; <-- without begin 

    do something

    //free memory
    $googleAnalitics = null;
}

for($i=0;$i<5;$i++){
	Yii::$app->ga
		->dateRange('7daysAgo', 'today')
		->metric('ga:sessions')
		->dimension('ga:browser')
		->request()
		->printReports();
}

  $report = Yii::$app->ga->begin->dateRange('1daysAgo', 'today')
            ->metric('ga:uniqueEvents')
            ->dimension('ga:eventCategory')
            ->dimension('ga:eventAction')
            ->dimension('ga:eventLabel')
            ->request()->one($index = 0);

  print_r($report);

	$values = Yii::$app->ga->begin->dateRange('70daysAgo', 'today')
        ->metric('ga:uniqueEvents')
        ->dimension('ga:segment')
        ->segment('ga:eventLabel', 'What are you loking for ?')
        ->request()->one($index = 0);