jalle19/yii-ga

为Yii框架提供的Google Analytics扩展

1.0.2 2014-06-03 12:17 UTC

This package is auto-updated.

Last update: 2024-09-13 00:32:37 UTC


README

Yii框架的Google Analytics扩展。它支持常规跟踪以及电子商务交易的跟踪。

有关Google Analytics集成的详细信息,请参阅https://developers.google.com/analytics/devguides/

安装

使用Composer安装应用程序,并确保您已经在您的引导文件中包含了Composer的自动加载器。之后,您需要在应用程序配置中添加以下内容

// change this path if necessary
Yii::setPathOfAlias('yiiga', realpath(__DIR__.'/../../vendor/jalle19/yii-ga/src/yiiga'));
...
return array(
	...
	'components'=>array(
		...
		'ga'=>array(
			'class'=>'yiiga\components\GoogleAnalytics',
			'accountId'=>'UA-XXXXXXX-X',
			'cookieDomain'=>'www.example.com', // optional
			'currency'=>'euro', // only needed if you're going to use e-commerce transactions
		),
	),
),

用法

要注册基本跟踪支持,将Yii::app()->ga->registerTracking();添加到您的布局文件中(假设您想要跟踪整个站点)。

要注册电子商务交易,根据您的需要调整以下片段(请参阅类文件以了解哪些属性可用于交易和交易项)

<?php

/* @var $order Order */

// Create a new transaction
$transaction = new yiiga\models\Transaction();
$transaction->orderId = $order->id;
$transaction->city = $order->city;
$transaction->country = $order->country;
$transaction->total = $order->total;
$transaction->tax = $order->tax;

// Add the order items to the transaction
foreach ($order->items as $orderItem)
{
	$item = new yiiga\models\TransactionItem();
	$item->sku = $orderItem->sku;
	$item->name = $orderItem->name;
	$item->price = ($orderItem->price + $orderItem->tax);
	$item->quantity = $orderItem->quantity;

	$transaction->addItem($item);
}

// Register the transaction
Yii::app()->ga->addTransaction($transaction);

许可证

本应用程序受新BSD许可证许可。

鸣谢

感谢@crisu83提供了一些指导!