kitbrennan90/php-ga-measurement-protocol
Composer 安装命令:
composer require kitbrennan90/php-ga-measurement-protocol
包简介
PHP package for interacting with Google Analytics measurement protocol (eg. send page views and events to Google Analytics using your server side).
README 文档
README
This simple library lets you send server side track events to Google Analytics.
Installation
This library is best installed via composer. Via the command line: composer require kitbrennan90/php-ga-measurement-protocol
If you are using PSR-4 (or a modern framework), simply import the client at the top of your script:
use GaMeasurementProtocol\Client;
If you are not using PSR-4, you should require the composer autoloader: require_once('vendor/autoload.php');
Using the library
Initialise the Client
$trackingId = 'GA-123456-1'; // Take the tracking/property ID from your Google Analytics account $client = new Client($trackingId);
Sending a pageview
The library defaults to firing pageviews.
$trackingId = 'GA-123456-1'; // Take the tracking/property ID from your Google Analytics account $client = new Client($trackingId); $params = [ 'dl' => 'http://foo.com/home?a=b' // Document location, required for a 'pageview' type ]; $client->request() ->setParameters($params) ->send();
Not working? The Measurement Protocol will not raise any errors by default, please explore the debug section below if your results are not showing in Google Analytics the issue.
Sending other hit types (eg. event, screenview)
The library supports every tracking type (available as constants):
- GaMeasurementProtocol\Enums\HitType::PAGEVIEW
- GaMeasurementProtocol\Enums\HitType::SCREENVIEW
- GaMeasurementProtocol\Enums\HitType::EVENT
- GaMeasurementProtocol\Enums\HitType::TRANSACTION
- GaMeasurementProtocol\Enums\HitType::ITEM
- GaMeasurementProtocol\Enums\HitType::SOCIAL
- GaMeasurementProtocol\Enums\HitType::EXCEPTION
- GaMeasurementProtocol\Enums\HitType::TIMING
Just call setHitType($type) on the request:
$trackingId = 'GA-123456-1'; // Take the tracking/property ID from your Google Analytics account $client = new Client($trackingId); $params = [ 'dl' => 'http://foo.com/home?a=b' // Document location, required for a 'pageview' type ]; $client->request() ->setParameters($params) ->setHitType(GaMeasurementProtocol\Enums\HitType::EVENT) ->send();
Sending additional parameters
The full supported parameter list can be found here: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
Add all your optional parameters to an array, and call setParameters($params) on the request object:
$trackingId = 'GA-123456-1'; // Take the tracking/property ID from your Google Analytics account $client = new Client($trackingId); $params = [ 'dl' => 'http://foo.com/home?a=b', // Document location, required for a 'pageview' type 'ul' => 'en-us' // User language ]; $client->request() ->setParameters($params) ->send();
Debugging
The Measurement Protocol always returns 200, even if parameters are invalid. You instead need to enable debug mode, which will var_dump the debug results to your view.
Note If debug mode is turned on, the results will NOT appear in your Google Analytics.
Once debug mode says your hit is valid, you should turn debug mode off and send your request again.
$trackingId = 'GA-123456-1'; // Take the tracking/property ID from your Google Analytics account $client = new Client($trackingId); $params = [ 'dl' => 'http://foo.com/home?a=b' // Document location, required for a 'pageview' type ]; $client->setDebug(true) ->request() ->setParameters($params) ->send();
kitbrennan90/php-ga-measurement-protocol 适用场景与选型建议
kitbrennan90/php-ga-measurement-protocol 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.45k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kitbrennan90/php-ga-measurement-protocol 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kitbrennan90/php-ga-measurement-protocol 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-11