本指南將逐步引導您將現有的analytics.js
通用 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 程式碼片段中的程式碼會自動將網頁瀏覽傳送至 Google Analytics (分析) 資源 (代碼 ID 為 TAG_ID
):
gtag('config', 'TAG_ID');
使用指定的追蹤程式評估網頁瀏覽量
取代以下 analytics.js 程式碼,使用指定追蹤程式將網頁瀏覽傳送至 Google Analytics (分析):
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 (分析) 資源。
使用預設追蹤程式評估事件
將以下使用預設追蹤程式的 analytics.js 程式碼替換為 send
事件:
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
替換成自己的 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 |