baibaratsky/yii2-ga-measurement-protocol
Composer 安装命令:
composer require baibaratsky/yii2-ga-measurement-protocol
包简介
Google Analytics Measurement Protocol for Yii2
README 文档
README
Interact with Google Analytics directly. No need of any JS code. Pure server-side.
Full support for all methods of the Google Analytics Measurement Protocol is provided.
Installation
Note: Versions in 1.* range are incompatible with PHP 7.2, use 2.* with Yii 2.0.13+ instead.
-
The preferred way to install this extension is through composer.
To install, either run
$ php composer.phar require baibaratsky/yii2-ga-measurement-protocol:2.0.*or add
"baibaratsky/yii2-ga-measurement-protocol": "2.0.*"to the
requiresection of yourcomposer.jsonfile.For PHP versions prior to 7.2, use a release from 1.* range:
"baibaratsky/yii2-ga-measurement-protocol": "1.2.*" -
Add the component configuration in your
main.phpconfig file:'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) ], ],
Usage
This extension is just a wrapper around the
Google Analytics Measurement Protocol library for PHP.
request() returns a TheIconic\Tracking\GoogleAnalytics\Analytics object, so all its methods will work seamlessly.
Basic Usage
\Yii::$app->ga->request() ->setClientId('12345678') ->setDocumentPath('/mypage') ->sendPageview();
Order Tracking with Enhanced E-commerce
$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();
Asynchronous Mode
By default, sending a hit to Google Analytics will be a synchronous request, and it will block the execution of the script until the latter gets a response from the server or terminates by timeout after 100 seconds (throwing a Guzzle exception). However, if you turn the asynchronous mode on in the component config, asynchronous non-blocking requests will be used.
'asyncMode' => true,
This means that we are sending the request and not waiting for response.
The TheIconic\Tracking\GoogleAnalytics\AnalyticsResponse object that you will get back has null for HTTP status code.
You can also send an asynchronous request even if you haven’t turned it on in the config. Just call setAsyncRequest(true)
before sending the hit:
\Yii::$app->ga->request() ->setClientId('12345678') ->setDocumentPath('/mypage') ->setAsyncRequest(true) ->sendPageview();
Auto set clientId from cookie
If you set autoSetClientId to true durning component configuration you must disable enableCookieValidation.
You can do this by configuring request component. Otherwise, the auto set clientId will not work.
'components' => [ 'request' => [ 'enableCookieValidation' => false, ], ]
baibaratsky/yii2-ga-measurement-protocol 适用场景与选型建议
baibaratsky/yii2-ga-measurement-protocol 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 105.37k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2015 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「google analytics」 「yii」 「analytics.js」 「ga」 「yii2」 「measurement protocol」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 baibaratsky/yii2-ga-measurement-protocol 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 baibaratsky/yii2-ga-measurement-protocol 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 baibaratsky/yii2-ga-measurement-protocol 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Analytics chooser extensions for site settings.
Priveate for SkeekS CMS
A Laravel Nova Card to show Fathom Analytics stats.
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
C3JS Chart for Yii Framework 2.0
统计信息
- 总下载量: 105.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-04-17