本指南逐步介绍了如何迁移现有 analytics.js
Universal Analytics 实现以使用 gtag.js
。
概览
analytics.js 使用两种主要机制来向 Google Analytics(分析)发送数据:
在 gtag.js
中,媒体资源通过 config
命令来指定或作为命令的参数来指定。
与 analytics.js 不同,gtag.js 不使用跟踪器向 Google Analytics(分析)发送数据。它将数据发送到由 config
命令设置的 ID 所标识的 Google Analytics(分析)媒体资源。提供给 gtag.js 的事件名称指定发送到 Google Analytics(分析)的数据类型。
要从 analytics.js 迁移到 gtag.js,请针对您网站的每个网页执行以下操作:
将 analytics.js 代码替换为等效的 gtag.js 代码,以便:
将 analytics.js 代码段替换为 gtag.js 代码段
将网页中的 analytics.js 代码段:
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'TAG_ID', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
替换为以下 gtag.js 代码段:
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'TAG_ID');
</script>
衡量网页浏览
analytics.js 使用跟踪器向 Google Analytics(分析)发送网页浏览数据。跟踪器具有 Google Analytics(分析)媒体资源的衡量 ID。gtag.js 将网页浏览数据发送到由 config
命令中指定的 TAG_ID
所标识的 Google Analytics(分析)媒体资源。
使用默认跟踪器衡量网页浏览
移除以下使用默认跟踪器向 Google Analytics(分析)send
网页浏览数据的 analytics.js 代码:
// Creates the default tracker.
ga('create', 'TAG_ID', 'auto');
// Uses the default tracker to send a pageview to the
// Google Analytics property with tag ID of 'TAG_ID'.
ga('send', 'pageview');
gtag.js 代码段中的以下代码自动向代码 ID 为 TAG_ID
的 Google Analytics(分析)媒体资源发送网页浏览数据:
gtag('config', 'TAG_ID');
使用指定跟踪器衡量网页浏览
将以下使用指定跟踪器向 Google Analytics(分析)发送网页浏览数据的 analytics.js 代码:
ga('create', 'TAG_ID', 'auto', 'trackerName');
ga('trackerName.send', 'pageview');
替换为以下 gtag.js event
命令:
gtag('event', 'page_view', { 'send_to': 'TAG_ID' });
衡量事件
如前所述,analytics.js 使用跟踪器将事件发送到 Google Analytics(分析)。跟踪器具有 Google Analytics(分析)媒体资源的跟踪 ID。与之不同的是,gtag.js 将事件发送到由 config
命令中指定的 TAG_ID
所标识的 Google Analytics(分析)媒体资源。
使用默认跟踪器衡量事件
将以下使用默认跟踪器向 Google Analytics(分析)send
事件的 analytics.js 代码:
ga('create', 'TAG_ID', 'auto');
ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
替换为以下 gtag.js event
命令:
gtag('event', eventName, eventParameters);
其中 eventName
是要记录的事件的名称。
示例:
analytics.js:
// Creates the default tracker.
ga('create', 'TAG_ID', 'auto');
// Uses the default tracker to send the event to the
// Google Analytics property with a tag ID of `TAG_ID`.
ga('send', 'event', 'Videos', 'play', 'Fall Campaign');
gtag.js:
// Sends the event to the Google Analytics property with a
// tag ID of `TAG_ID` set by the config command in
// the gtag.js snippet.
gtag('event', 'play', {
'event_category': 'Videos',
'event_label': 'Fall Campaign'
});
使用指定跟踪器衡量事件
将以下使用指定跟踪器向 Google Analytics(分析)发送事件的 analytics.js 代码:
ga('create', 'TAG_ID', 'auto', 'trackerName');
ga('trackerName.send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
替换为以下 gtag.js event
命令:
gtag('event', eventName, {
'send_to': 'TAG_ID',
'parameter1': 'value1',
'parameter2': 'value2',
// ...
});
示例:
analytics.js:
// Creates a tracker named <b>clientTracker</b>.
ga('create', 'TAG_ID', 'auto', 'clientTracker');
// Uses tracker clientTracker to send the event to the
// Google Analytics property with a tag ID of TAG_ID.
ga('clientTracker.send', 'event', 'Videos', 'play', 'Fall Campaign');
gtag.js:
// Send the event to the Google Analytics property
// with a tag ID of 'TAG_ID'.
gtag('event', 'play', {
'send_to': 'TAG_ID',
'event_category': 'Videos',
'event_label': 'Fall Campaign'
});
发送自定义维度和指标
将网页中向 Google Analytics(分析)发送自定义维度的任何 analytics.js send
命令:
ga('send', 'hitType', { 'dimension<Index>': 'dimension_value'});
替换为以下 gtag.js 代码:
gtag('config', 'TAG_ID', {
'custom_map': {'dimension<Index>': 'dimension_name'}
});
gtag('event', 'any_event_name', {'dimension_name': 'dimension_value'});
将 TAG_ID
替换为您自己的 Google Analytics(分析)ID。
将网页中向 Google Analytics(分析)发送自定义指标的任何 analytics.js send
命令:
ga('send', 'hitType', { 'metric<Index>': 'metric_value'});
替换为以下 gtag.js 代码:
gtag('config', 'TAG_ID', {
'custom_map': {'metric<Index>': 'metric_name'}
});
gtag('event', 'any_event_name', {'metric_name': 'metric_value'});
将 TAG_ID
替换为您的代码 ID。
衡量用户计时
将网页中跟踪用户计时的任何 analytics.js send
命令:
ga('send', 'timing', 'timingCategory', 'timingVar', timingValue, 'timingLabel');
替换为以下 gtag.js event
命令:
gtag('event', 'timing_complete', {
'name': 'timingVar',
'value': timingValue,
'event_category': 'timingCategory',
'event_label': 'timingLabel'
});
衡量异常
将网页中跟踪异常的任何 analytics.js send
命令:
ga('send', 'exception', {
'exDescription': 'error_message',
'exFatal': false // set to true if the exception is fatal
});
替换为以下 gtag.js event
命令:
gtag('event', 'exception', {
'description': 'error_message',
'fatal': false // set to true if the exception is fatal
});
将 analytics.js 字段映射到 gtag.js 参数
以下表格将 analytics.js 字段映射到相应的 gtag.js 参数。
事件
analytics.js 字段 | gtag.js 参数 |
---|---|
eventAction |
event_action |
eventCategory |
event_category |
eventLabel |
event_label |
eventValue |
value |
自定义维度和指标
analytics.js 字段 | gtag.js 参数 |
---|---|
dimension<Index> |
dimension<Index> |
metric<Index> |
metric<Index> |
其中 <Index>
是表示自定义维度或指标索引的非负整数。
用户计时
analytics.js 字段 | gtag.js 参数 |
---|---|
timingCategory |
event_category |
timingLabel |
event_label |
timingValue |
value |
timingVar |
name |
异常衡量
analytics.js 字段 | gtag.js 参数 |
---|---|
exDescription |
description |
exFatal |
fatal |
增强型电子商务操作数据
analytics.js 字段 | gtag.js 参数 |
---|---|
id |
transaction_id |
affiliation |
affiliation |
revenue |
value |
tax |
tax |
shipping |
shipping |
coupon |
coupon |
list |
list_name |
step |
checkout_step |
option |
checkout_option |
促销数据
analytics.js 字段 | gtag.js 参数 |
---|---|
creative |
creative_name |
position (展示、商品) |
list_position |
position (促销) |
creative_slot |
商品和促销操作
analytics.js 字段 | gtag.js 事件 |
---|---|
add |
add_to_cart |
checkout (第一步) |
begin_checkout |
checkout (任何后续步骤) |
checkout_progress |
checkout_option |
set_checkout_option |
click |
select_content (不含促销) |
detail |
view_item |
promo_click |
select_content (含促销) |
purchase |
purchase |
refund |
refund |
remove |
remove_from_cart |
客户端 ID 和用户 ID
analytics.js 字段 | gtag.js 参数 |
---|---|
clientId |
client_id |
userId |
user_id |