您可以設定 Google 代碼安裝作業,將資料傳送至特定帳戶或產品群組。您可以在同一個程式碼區塊中,為 Google 評估產品設定完整的成效評估解決方案。本指南說明如何使用 send_to 和 groups 參數設定 Google 代碼,將資料傳送至特定產品、帳戶和設定。
預設路徑
Google 代碼包含用於處理路徑的 config 指令。舉例來說,如果您使用代碼 ID 安裝 Google 代碼,下列程式碼片段會將資料傳送至 Google Analytics 和/或 Google Ads 帳戶:
gtag('config', 'TAG_ID');
如要覆寫 Google 代碼中指定的路徑 (或網頁上任何先前的路徑指示),請在 event 指令中加入 send_to 參數。
舉例來說,無論網頁上先前設定了哪些目標,下列 sign_in 事件只會傳送至具有目的地 ID 'G-XXXXXX-2' 的 Google Analytics 資源。
gtag('event', 'sign_in', { 'send_to': 'G-XXXXXX-2' });
群組
有時您可能需要將特定資訊傳送至一組帳戶或產品,並將其他資訊傳送至另一組帳戶或產品。舉例來說,您可能想將特定行銷廣告活動的資訊傳送給廣告代理商,同時為貴機構保留更完整的資料。您可以使用 groups 整理這項功能。
您可以建立一組目標 (例如產品、帳戶和資源),然後將事件轉送至該群組。
在下列範例中,兩個 Google Analytics 資源會新增至名為 group1 的群組。接著,系統會將 sign_in 事件轉送至該群組中的兩項資源。
gtag('config', 'G-XXXXXX-1', { 'groups': 'group1' });
gtag('config', 'G-XXXXXX-2', { 'groups': 'group1' });
// Routes to 'G-XXXXXX-1' and 'G-XXXXXX-2'
gtag('event', 'sign_in', { 'send_to': 'group1' });
預設群組
如果未設定 send_to 參數,系統會將事件傳送至 default 目標群組。default 群組包含網頁上在事件之前執行的所有產品和帳戶 config 指令。即使 config 指令中未指定 groups 參數,目標也會指派給 default 群組。
// The following two lines are equivalent:
gtag('config', 'G-XXXXXX-1');
gtag('config', 'G-XXXXXX-1', { 'groups': 'default' });
下一個範例說明事件會傳送至 default 群組,無論是否指定 {'send_to : 'default'} 皆是如此。
// Configure a target
gtag('config', 'G-XXXXXX-1');
// Since send_to is not specified, this routes to the 'default' group which
// includes 'G-XXXXXX-1', as defined in config, above.
gtag('event', 'sign_in');
// By default, routes to the 'default' groups which includes
// 'G-XXXXXX-1', as defined in config, above.
gtag('event', 'generate_lead', { 'send_to': 'default' });
前往自訂 groups 的路線
您可以使用 groups 識別應路由至特定 ID 集的特定資料。以下程式碼範例說明如何將 sign_in 事件資料傳送至名為 agency 的自訂群組。
// Configure a target
gtag('config', 'G-XXXXXX-1');
gtag('config', 'G-XXXXXX-3', { 'groups': 'agency' });
gtag('config', 'G-XXXXXX-9', { 'groups': 'agency' });
// Routes only to 'G-XXXXXX-3' and 'G-XXXXXX-9' since they
// are both in the 'agency' group
gtag('event', 'sign_in', { 'send_to': 'agency' });
範例:一併設定 Google Ads、Analytics 和 Floodlight
您可以在同一個 Google 代碼中,為 Google Ads、Google Analytics 和 Floodlight 建立完整設定。這個範例示範組合標記的可能樣貌。這個範例:
- 將網頁瀏覽資料傳送至 Google Analytics
- 評估 Google Ads 和 Floodlight 轉換
- 將加入購物車的商品相關資訊傳送至 Analytics 和 Google Ads
<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());
// Global configs
gtag('config', 'TAG_ID');
gtag('config', 'DC-ZZZZZZ');
// Measure Google Ads conversions
gtag('event', 'conversion', {
'send_to': 'AW-YYYYYY/AbC-D_efG-h12_34-567',
'value': 1.0,
'currency': 'USD'
});
// Measure Floodlight conversions
gtag('event', 'conversion', {
'allow_custom_scripts': true,
'send_to': 'DC-ZZZZZZ/actions/locat304+standard'
});
// Route ecommerce add_to_cart event to Google Ads and Analytics
gtag('event', 'add_to_cart', {
'send_to': [
'G-XXXXXX-1',
'AW-YYYYYY'
],
'items': [
'id': 'U1234',
'ecomm_prodid': 'U1234',
'name': 'Argyle Funky Winklepickers',
'list': 'Search Results',
'category': 'Footwear',
'quantity': 1,
'ecomm_totalvalue': 123.45,
'price': 123.45
]
});
</script>