此功能目前处于公开 Beta 版阶段,随时可能发生变化。
这些说明适用于以下管理员和编辑者:想要了解如何使用 Google 跟踪代码管理器,在网站上设置代码段来收集用户提供的数据。
与其他配置选项相比,更新代码有助于确保在 Google Analytics 收集用户提供的数据时,网页上会显示电子邮件地址、电话号码和/或姓名与地址。
概览
如需使用 Google 跟踪代码管理器设置代码段,请完成以下步骤:
- 在网站代码中识别并指定哪些字段包含用户提供的数据
- 将自定义 JavaScript 变量中包含用户提供的数据的每个字段传递给 Google Analytics
注意:您可以将经过哈希处理或未经哈希处理的客户数据发送到 Google Analytics。当您发送未经哈希处理的数据时,Google 会先对数据进行哈希处理,再将数据存储在 Google 的服务器中。不过,您也可以在将数据发送给 Google 之前,先自行对数据进行哈希处理。
准备工作
这些说明假定您满足以下条件:
- 拥有您网站源代码的访问权限
- 已启用“收集用户提供的数据”功能和“允许包括用户提供的数据”选项
- 在 Google 跟踪代码管理器容器中设置了“Google Analytics:GA4 事件”代码
- 拥有 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:
- Use hex SHA256.
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. |
使用自定义 JavaScript 变量更新事件代码
在更新跟踪代码管理器容器之前,请了解如何更新要添加到容器的自定义 JavaScript 代码。
- 登录 Google 跟踪代码管理器。
- 在工作区标签页中,点击左侧的代码。
- 打开您用于收集客户数据的 Google Analytics:GA4 事件代码。确保事件代码使用的衡量 ID 对应于用来收集用户数据的网站数据流。
- 展开事件参数部分,然后点击添加参数。
- 在“事件参数”字段中,输入
user_data。 - 在“值”字段中,选择或创建“用户提供的数据”变量。
- 如果您要创建新变量,请在“用户提供的数据”变量中选择代码。
- 在数据源下拉菜单中,点击新建变量…。
- 依次点击变量配置 > 自定义 JavaScript。
- 在页面顶部输入自定义 JavaScript 变量的名称。
- 在自定义 JavaScript 文本框中,输入代码段。了解操作方法。
- 先保存新变量,然后保存事件代码。
- 点击右上角的提交即可发布所做更改。
创建代码段以收集用户提供的数据
在第 10 步中设置自定义 JavaScript 变量时,您可以使用以下代码:
function () {
return {
"email": yourEmailVariable , // 将 yourEmailVariable 替换为用于捕获用户电子邮件地址的变量名
"phone_number": yourPhoneVariable , // 针对 yourPhoneVariable 和以下变量名重复上述操作
"address": {
"first_name": yourFirstNameVariable ,
"last_name": yourLastNameVariable ,
"street": yourStreetAddressVariable ,
"city": yourCityVariable ,
"region": yourRegionVariable ,
"postal_code": yourPostalCodeVariable ,
"country": yourCountryVariable
}
}
}
或者,您也可以使用字符串对字段进行硬编码,或者使用函数(而不是变量)。
// 实现
<script>
gtag('set', 'user_data', {"sha256_email_address": yourNormalizedandHashedEmailVariable,"sha256_phone_number": yourNormalizedandHashedPhoneVariable,"address": {"address.sha256_first_name": yourNormalizedandHashedFirstNameVariable,"address.sha256_last_name": yourNormalizedandHashedLastNameVariable,"city": yourCityVariable,"region": yourRegionVariable,"postal_code": yourPostalCodeVariable,"country": yourCountryVariable}
});
</script>如果您想提供经过哈希处理的用户数据值,可以使用以下示例代码段:
async function processUserData() {
const userData = {'sha256_email_address': await hashEmail(email.trim()),'sha256_phone_number': await hashPhoneNumber(phoneNumber),'address': {'address.sha256_first_name': await hashName(firstName),'address.sha256_last_name': await hashName(lastName),'city': city,'region': region,'postal_code': postalCode,'country': country,},
};
return userData;电话号码必须采用 E.164 格式,这意味着它必须由 11 到 15 位数组成,且包括加号 (+) 前缀和国家/地区代码(不得包含短划线、括号或空格)。
Next steps
Validate that user-provided data collection is set up correctly. Show me how.