xcartman/yii2-google-analitics

Yii框架的Google Analytics扩展

v1.0.2 2018-09-05 18:07 UTC

This package is auto-updated.

Last update: 2024-09-11 14:48:35 UTC


README

此扩展为Yii框架提供了google-analitics集成。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

$ composer require xcartman/yii2-google-analitics

或者将以下内容添加到您的 composer.json 文件的 require 部分。

"xcartman/yii2-google-analitics": "*"

配置

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;
}

错误使用方法

!重要 每次请求后,您需要清除数据,这可以通过getBegin方法或克隆ga对象来完成

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);