baibaratsky/yii2-ga-measurement-protocol

Yii2 的 Google Analytics Measurement Protocol

v2.0.1 2019-03-02 12:14 UTC

This package is auto-updated.

Last update: 2024-09-16 14:38:15 UTC


README

Packagist Dependency Status Packagist Packagist

直接与 Google Analytics 交互。无需任何 JS 代码。纯服务器端。

提供对 Google Analytics Measurement Protocol 所有方法的完全支持。

安装

注意: 1.* 范围的版本与 PHP 7.2 不兼容,请使用 2.* 版本与 Yii 2.0.13+ 一起使用。

  1. 通过 composer 安装此扩展是首选方式。

    要安装,请运行

    $ php composer.phar require baibaratsky/yii2-ga-measurement-protocol:2.0.*
    

    或添加

    "baibaratsky/yii2-ga-measurement-protocol": "2.0.*"
    

    到您的 composer.json 文件的 require 部分。

    对于低于 7.2 的 PHP 版本,请使用 1.* 范围的版本

    "baibaratsky/yii2-ga-measurement-protocol": "1.2.*"
    
  2. 在您的 main.php 配置文件中添加组件配置

    'components' => [
        'ga' => [
            'class' => 'baibaratsky\yii\google\analytics\MeasurementProtocol',
            'trackingId' => 'UA-XXXX-Y', // Put your real tracking ID here
    
            // These parameters are optional:
            'useSsl' => true, // If you’d like to use a secure connection to Google servers
            'overrideIp' => false, // By default, IP is overridden by the user’s one, but you can disable this
            'anonymizeIp' => true, // If you want to anonymize the sender’s IP address
            'asyncMode' => true, // Enables the asynchronous mode (see below)
            'autoSetClientId' => true, // Try to set ClientId automatically from the “_ga” cookie (disabled by default)
        ],
    ],

使用方法

此扩展只是围绕 PHP 的 Google Analytics Measurement Protocol 库 的包装。 request() 返回一个 TheIconic\Tracking\GoogleAnalytics\Analytics 对象,因此所有方法都将无缝工作。

基本使用方法

\Yii::$app->ga->request()
    ->setClientId('12345678')
    ->setDocumentPath('/mypage')
    ->sendPageview();

使用增强型电子商务进行订单跟踪

$request = \Yii::$app->ga->request();

// Build the order data programmatically, each product of the order included in the payload
// First, general and required hit data
$request->setClientId('12345678');
$request->setUserId('123');

// Then, include the transaction data
$request->setTransactionId('7778922')
    ->setAffiliation('THE ICONIC')
    ->setRevenue(250.0)
    ->setTax(25.0)
    ->setShipping(15.0)
    ->setCouponCode('MY_COUPON');

// Include a product, the only required fields are SKU and Name
$productData1 = [
    'sku' => 'AAAA-6666',
    'name' => 'Test Product 2',
    'brand' => 'Test Brand 2',
    'category' => 'Test Category 3/Test Category 4',
    'variant' => 'yellow',
    'price' => 50.00,
    'quantity' => 1,
    'coupon_code' => 'TEST 2',
    'position' => 2
];

$request->addProduct($productData1);

// You can include as many products as you need, this way
$productData2 = [
    'sku' => 'AAAA-5555',
    'name' => 'Test Product',
    'brand' => 'Test Brand',
    'category' => 'Test Category 1/Test Category 2',
    'variant' => 'blue',
    'price' => 85.00,
    'quantity' => 2,
    'coupon_code' => 'TEST',
    'position' => 4
];

$request->addProduct($productData2);

// Don't forget to set the product action, which is PURCHASE in the example below
$request->setProductActionToPurchase();

// Finally, you need to send a hit; in this example, we are sending an Event
$request->setEventCategory('Checkout')
    ->setEventAction('Purchase')
    ->sendEvent();

异步模式

默认情况下,向 Google Analytics 发送 hit 将是同步请求,并且它将阻塞脚本的执行,直到后者从服务器获得响应或超时(100 秒后终止),抛出 Guzzle 异常。但是,如果您在组件配置中将异步模式打开,则将使用异步非阻塞请求。

'asyncMode' => true,

这意味着我们正在发送请求而不等待响应。您将得到的 TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse 对象的 HTTP 状态代码为 null

您还可以在未在配置中打开的情况下发送异步请求。只需在发送 hit 之前调用 setAsyncRequest(true)

\Yii::$app->ga->request()
    ->setClientId('12345678')
    ->setDocumentPath('/mypage')
    ->setAsyncRequest(true)
    ->sendPageview();

自动从 cookie 设置 clientId

如果您在组件配置中将 autoSetClientId 设置为 true,则必须禁用 enableCookieValidation。您可以通过配置 request 组件来完成此操作。否则,自动设置 clientId 将不会工作。

'components' => [
    'request' => [
        'enableCookieValidation' => false,
    ],
]