承接 kitbrennan90/php-ga-measurement-protocol 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kitbrennan90/php-ga-measurement-protocol

最新稳定版本:v1.0.3

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

统计信息

  • 总下载量: 2.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固