[GA4] 將程式碼片段加入網站 (gtag.js)

This feature is part of an open beta. Keep in mind that the feature is subject to change.

以下操作說明適用於想要設定「收集使用者提供的資料」功能的管理員和編輯者,可協助他們瞭解如何使用 gtag.js 在網站中加入程式碼片段。成功更新網站程式碼後,當 Analytics (分析) 收集使用者提供的資料時,網頁上就能顯示電子郵件地址、電話號碼和/或姓名及地址。

總覽

如要更新網站程式碼,請務必使用 gtag( ) 函式,在 event、config 或 set 指令內設定 user_data 值,以及每個使用者提供資料欄位。

如果您在兩種以上的指令 (例如 event 和 set) 內傳遞相同資料,event 指令就會覆寫 config 指令,config 指令則會覆寫 set 指令。瞭解 Google 代碼的參數優先順序

我們建議您對收集使用者提供資料的所有頁面更新程式碼,例如註冊、待開發客戶產生、結帳頁面或類似頁面。您可以在一個工作階段中多次傳送資料,確保資料能傳送至 Analytics (分析)。

事前準備

這些操作說明假設您已具備下列條件:

  • 可存取網站原始碼
  • 已啟用「收集使用者提供的資料」功能,並允許納入使用者提供的資料

Hashing the data

You can send hashed or unhashed data from a website. If you send unhashed data, Google normalizes and hashes the data before the data reaches Google's servers. To ensure Analytics interprets the data correctly and to comply with the User-provided data policy, you must use the correct key name (in the next section).

To hash the data:

To normalize the data:

  • Remove leading or trailing whitespaces.
  • Convert the text to lowercase.
  • Format phone numbers according to the E.164 standard.

Defining the fields in your code

You must provide at least one of these fields:

  • Email (preferred).
  • Address (first name, last name, postal code, and country are required). You can optionally provide street address, city, and region as additional match keys.
  • A phone number can also be provided along with an email or full name and address.

Make sure the data is available when the Google tag fires. If the data is collected on a previous page, make sure it’s available in your code on the current page so you can configure the field.

Fields to set

The following table describes each field you can set. The key name shows how you must reference the field in your code. You must pass the data as a String type. When defining name and address, make sure to define each component as an individual variable (for example, first name, last name, and so on).

Note: If you want to send unhashed data and let Google normalize and hash the data, use the first key name for each data field. For example, email in the Email address data field below. If you want to send hashed data, use the key name that begins with sha256_.

Data Field Key Name Description
Email address email

User email.

Example: ‘jdoe@example.com’

sha256_email_address

Hashed user email.

Example:

‘a8af8341993604f29cd4e0e5a5a4b5d48c575436c38b28abbfd7d481f345d5db’

Phone number phone_number

User phone number. Must be in E.164 format, which means it must be 11 to 15 digits including a plus sign (+) prefix and country code with no dashes, parentheses, or spaces.

Example: ‘+11231234567’

sha256_phone_number

Hashed user phone number.

Example:

‘e9d3eef677f9a3b19820f92696be53d646ac4cea500e5f8fd08b00bc6ac773b1’

First name address.first_name

User first name.

Example: 'John'

address.sha256_first_name

Hashed user first name.

Example:

‘96d9632f363564cc3032521409cf22a852f2032eec099ed5967c0d000cec607a’

Surname address.last_name

User last name.

Example: 'Doe'

address.sha256_last_name

Hashed user last name.

Example:

‘799ef92a11af918e3fb741df42934f3b568ed2d93ac1df74f1b8d41a27932a6f’

Street address address.street User street address. Example: '123 New Rd'
City address.city User city name. Example: `Southampton’
Region address.region User province, state, or region. Example: `Hampshire’
Postal code address.postal_code User post code. Example: 'SO99 9XX'
Country address.country User country code. Example: 'UK'. Use 2-letter country codes, per the ISO 3166-1 alpha-2 standard.

在程式碼中導入欄位

請在收集顧客數位資料的每個網頁上,設定 user_data 參數,以及您要定義的每個欄位。

查看範例

以下範例包含顧客數位資料的變數名稱。請確認程式碼中的變數名稱與網站上的屬性相符。舉例來說,如果您將電子郵件地址儲存在名為「email_address」的變數中,就必須使用該名稱更新程式碼片段 (例如修改您的電子郵件變數部分)。

您也可以使用字串對欄位進行硬式編碼,或是使用函式 (而不是變數)。

// Implement

<script>
gtag('set', 'user_data', {
"email": 您的電子郵件變數, ***將您的電子郵件變數改成實際用來儲存使用者電子郵件資料的 JavaScript 變數名稱。針對下方其他變數進行同樣的操作,並確認值未經雜湊處理。
"phone_number": 您的電話號碼變數,
"address": {
"first_name": 您的名字變數,
"last_name": 您的姓氏變數,
"street": 您的街道地址變數,
"city": 您的城市變數,
"region": 您的區域變數,
"postal_code": 您的郵遞區號變數,
"country": 您的國家/地區變數
}
});
</script>

電話號碼必須採用 E.164 格式,也就是 11 到 15 位數,內含加號 (+) 前置字元和國家/地區代碼,但不含破折號、括號或空格。

移除空白欄位

如果您並未收集上述程式碼範例中的特定欄位,請從程式碼中完全移除這些欄位,不要留空。舉例來說,如果網站只收集電子郵件地址和電話號碼,看起來就會像這樣:

// Implement
<script>
gtag('set', 'user_data', {
"email": {{ 您的電子郵件變數 }},
"phone_number": {{ 您的電話號碼變數 }}
});
</script>

加入多個值

您可以使用陣列提供多個值 (最多 3 個電話號碼和電子郵件地址,以及 2 個地址)。擷取多個值時,可以提高比對成功的機率。

<script>
gtag('set', 'user_data', {
"email": [您的電子郵件變數1, 您的電子郵件變數2],
"phone_number": [您的電話號碼變數1, 您的電話號碼變數2],
"address": [
{
  first_name: 您的名字變數,
  last_name: 您的姓氏變數,
  street: 您的街道地址變數,
  city: 您的城市變數,
  region: 您的區域變數,
  postal_code: 您的郵遞區號變數
},
{
  first_name: 您的名字變數,
  last_name: 您的姓氏變數,
  street: 您的街道地址變數,
  city: 您的城市變數2,
  region: 您的區域變數2,
  postal_code: 您的郵遞區號變數2
}
]
});
</script>

這對您有幫助嗎?

我們應如何改進呢?
搜尋
清除搜尋內容
關閉搜尋
主選單
16287932959973310304
true
搜尋說明中心
true
true
true
true
true
69256
false
false