topshelfcraft/tracker

为 CraftCMS 提供便捷的服务端 Google Analytics 跟踪助手

安装: 712

依赖: 0

建议者: 0

安全: 0

星标: 19

关注者: 3

分支: 2

开放问题: 2

类型:craft-plugin

3.0.0 2020-06-22 18:50 UTC

This package is auto-updated.

Last update: 2024-09-23 04:28:08 UTC


README

为 CraftCMS 提供服务端 Google Analytics 跟踪助手

Top Shelf Craft 创建
Michael Rog,业主

Tracker 提供了一个助手方法,可以从 CraftCMS 模板或服务程序化地发送 Google Analtyics 跟踪点击。

安装

  1. 从您的项目目录,使用 Composer 需求插件包

    composer require topshelfcraft/tracker
    
  2. 在控制面板中,转到设置 → 插件,并为 Tracker 点击“安装”按钮。

  3. 没有第 3 步。

(Tracker 还可以通过 Craft CMS 插件商店安装。)

使用方法

跟踪页面浏览

{% set params = {
	trackingId: 'UA-12345-67',
	location: 'http://myawesomesite.com/foo.html',
	title: 'My Page Title'
} %}

{% do craft.tracker.track(params) %}

跟踪事件

{% set params = {
	trackingId: 'UA-12345-67',
	location: 'http://myawesomesite.com/foo.html',
	type: 'event',
	eventCategory: 'Music Player',
	eventAction: 'Play button',
	eventLabel: 'The Beatles - Yesterday',
	eventValue: '1461603558'
} %}

{% do craft.tracker.track(params) %}

从 PHP 使用 Helper 类

use topshelfcraft\tracker\Tracker;

$params = [
    'location' => Craft::$app->request->getUrl(),
    'clientId' => $currentUserId,
    'type' => 'pageview',
    'trackingId' => $trackingId,
    'version' => '1',
];

Tracker::getInstance()->track($params);

参数列表

以下是一个便捷的速查表,列出了 Google Analytics 可能接受的跟踪点击参数

参数名称不是很友好,因此 Tracker 插件提供了一套更友好的处理程序,这些处理程序将在请求实例化时映射到官方 API 参数处理程序。

完整的映射在 Settings.php 中的 GoogleParamNames 常量中定义。

/*
 * Main Parameters
 */

'adSenseNumber' => 'a', // Random number used to link Google Analytics to Adsense (currently not working)
'clientId' => 'cid', // Client ID number
'encodingType' => 'de', // Document Encoding type
'location'  => 'dl', // URL of the page being viewed.
'title' => 'dt', // Page title of the page being viewed.
'flashVersion' => 'fl', // Flash version on the site.
'nonInteraction' => 'ni', // Non-interaction hit type (1 = yes, 0 = no)
'javaEnabled' => 'je' , // Whether Java is enabled on the site, (1 = yes, 2 = no)
'screenDepth' => 'sd', // The view screen's depth.
'screenResolution' => 'sr', // The view screen's resolution.
'type' => 't', // The Type of tracking call that triggers the analytics request (e.g. pageview, event).
'trackingId' => 'tid', // Google Analytics user account number (UA-XXXXXX-X)
'userLanguage' => 'ul', // Language displayed on the site.
'version' => 'v', // Protocol version
'sdkVersion' => '_v', // SDK version number.
'hitIncrement' => '_s', // Hit Sequence, increments each time an event happens.
'verificationCode' => '_u', // Verification code generated by GA analytics.js
'cacheBuster' => 'z', // Functions as a cachebuster.

/*
 * Override Parameter
 */

'documentHostNameOverride' => 'dh', // Document host name override
'documentPathOverride' => 'dp', // Document Path override, used when overriding the standard page name.
'userAgentOverride' => 'ua', // User agent override.
'userIpOverride' => 'uip', // User Ip override.
'screenName' => 'cd', // Screen name, mainly used in app tracking.
'linkId' => 'linkid', // Link ID of a clicked DOM element.

/*
 * Events Parameters
 *
 * You will only see any of these when t (type) = event
 */

'eventAction' => 'ea', // Event Action.
'eventCategory' => 'ec', // Event Category.
'eventLabel' => 'el', // Event Label.
'eventValue' => 'ev', // Event value.

/*
 * Timing Parameters
 */

'userTimingCategory' => 'utc', // User timing category, not universal coordinated time.
'userTimingVarName' => 'utv', // User timing variable name.
'userTimingTime' => 'utt', // User timing time.
'userTimingLabel'=> 'utl', // User timing label.
'pageLoadTime' => 'plt', // Page load time.
'dnsTime' => 'dns', // DNS time.
'pageDownloadTime' => 'pdt', // Page download time.
'redirectResponseTime' => 'rrt', // Redirect response time.
'tcpConnectTime' => 'tcp', // TCP connect time.
'serverResponseTime' => 'srt', // Server response time.
'exceptionDescription' => 'exd', // Exception Description.
'isExceptionFatal' => 'exf', // Whether exception fatal or not.

/*
 * Campaign Variable Parameters
 *
 * To register any campaign variables (c*) you MUST populate Campaign Source AND Campaign Medium as a minimum.
 */

'campaignName' => 'cn', // Campaign name.
'campaignSource' => 'cs', // Campaign source.
'campaignMedium' => 'cm', // Campaign medium.
'campaignKeyword' => 'ck', // Campaign keyword.
'campaignContent' => 'cc', // Campaign content.
'campaignId' => 'ci', // Campaign Id.
'adwordsId' => 'glcid', // Google adwords id.
'displayAdsId' => 'dclid', // Google display ads id.

/*
 * eCommerce Parameters
 *
 * You will only see these when t (Type) = transaction or item.
 */

'currency' => 'cu', // Currency the transaction takes place in.
'itemName' => 'in', // The item name.
'itemCode' => 'ic', // The item's sku.
'itemPrice' => 'ip', // The item's price (per unit).
'itemQuantity' => 'iq', // Item quantity.
'itemVariation' => 'iv', // The item's category or variety.
'transactionAffiliation' => 'ta', // The transaction affiliation.
'transactionIdentification' => 'ti', // Transaction identification number.
'transactionRevenueValue' => 'tr', // Transactions' revenue value.
'transactionShippingValue' => 'ts', // Transactions' shipping value.
'transactionTaxValue' => 'tv', // Transaction tax value.

/*
 * App Tracking Parameters
 */

'applicationId' => 'aid', // Application Id.
'applicationInstallerId' => 'aiid', // Application installer Id.
'applicationName' => 'an', // Application Name.
'applicationVersion' => 'av', // Application version.
'hitSequence' => 'ht', // Hit sequence.

/*
 * Other Parameters
 *
 * When using 'anonymizeIp' the key just needs to be there, you can pass it any value, or pass it no value and
 * and it will anonymize the IP.
 */

'anonymizeIp' => 'aip', // Anonymize IP address.
'queueTime' => 'qt', // Queue time ( for collecting offline data).
'sessionControl' => 'sc', // Session Control.
'userId' => 'uid', // User ID (known uid as opposed to cid).

/*
 * Google Experiments Parameters
 */

'experimentId' => 'xid', // The experiment id.
'experimentVariant' => 'xvar', // The experiment variant.

/*
 * Social Tracking Parameters
 */

'socialNetwork' => 'sn', // The social network.
'socialAction' => 'sa', // Social action.
'socialActionTarget' => 'st', // The social action target, typically a URL.

);

配置

Tracker 插件配置文件允许您设置默认跟踪 ID,以及为网站和/或环境提供默认参数。

// (in tracker.php config file)

return [
	'trackingId' => 'UA-XXXXXX-XX',
	'defaultParams' => [],
];

您提供的任何默认参数都将添加到每个请求中,在从方法调用添加用户提供的参数之前。

系统要求是什么?

Craft 3.0+

我发现了一个错误。

请打开 GitHub 问题,或者如果您很慷慨,请向 3.x.dev 分支提交 PR。

贡献者