First-party data

向您的网站添加代码段 (gtag.js)

此功能目前处于公开 Beta 版阶段,随时可能发生变化。

这些说明适用于以下管理员和编辑者:正在设置“收集用户提供的数据”功能,并希望了解如何使用 gtag.js 向网站添加代码段。更新您网站上的代码有助于确保在 Google Analytics 收集用户提供的数据时,网页上会显示邮箱、电话号码和/或姓名与地址。

下载“用户提供的数据”实现指南,详细了解 Google Analytics 中的用户提供的数据。

概览

若要更新网站代码,您必须使用 gtag( ) 函数,在 event、config 或 set 命令中设置 user_data 值以及每个包含用户提供的数据的字段。

如果您在两种或更多类型的命令(例如 event 和 config)中传递相同的数据,则 event 命令会替换 config 命令,而 config 命令会替换 set 命令。了解 Google 代码的参数优先级。

我们建议您对收集用户提供的数据的所有页面更新代码,例如注册页面、潜在客户发掘页面、结账页或类似页面。您可以在一次会话中多次发送数据,以确保数据发送到 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”的变量中,则应使用该名称更新代码段(例如,更新代码段中出现 yourEmailVariable 的地方)。

注意:您也可以使用字符串对字段进行硬编码,或者使用函数(而不是变量)。

// 实现

<script>
gtag('set', 'user_data', {
"email": yourEmailVariable, ***将 yourEmailVariable 更改为您存储用户电子邮件地址数据的实际 JavaScript 变量名称。对以下其他变量执行相同的操作。请确保未对这些值进行哈希处理。
"phone_number": yourPhoneVariable,
"address": {
"first_name": yourFirstNameVariable,
"last_name": yourLastNameVariable,
"street": yourStreetAddressVariable,
"city":yourCityVariable,
"region": yourRegionVariable,
"postal_code": yourPostalCodeVariable,
"country": yourCountryVariable
}
});
</script>

电话号码必须采用 E.164 格式,这意味着它必须由 11 到 15 位数组成,且包括加号 (+) 前缀和国家/地区代码(不得包含短划线、括号或空格)。

移除空白字段

如果您没有收集上一个示例代码中的某些字段,请从代码中完全移除这些字段,而不是将其留空。例如,如果某网站仅收集电子邮件地址和电话号码,则代码将如下所示:

// 实现
<script>
gtag('set', 'user_data', {
"email": {{ yourEmailVariable }},
"phone_number": {{ yourPhoneVariable }}
});
</script>

添加多个值

您可以使用数组提供多个值(最多 3 个电话号码和电子邮件地址,以及 2 个实际地址)。如果您获取的值不止一个,提供数组值将提高成功匹配的几率。

<script>
gtag('set', 'user_data', {
"email": [yourEmailVariable1, yourEmailVariable2],
"phone_number": [yourPhoneVariable1, yourPhoneVariable2],
"address": [
{
  first_name: yourFirstNameVariable,
  last_name: yourLastNameVariable,
  street: yourStreetAddressVariable,
  city: yourCityVariable,
  region: yourRegionVariable,
  postal_code: yourPostalCodeVariable
},
{
  first_name: yourFirstNameVariable,
  last_name: yourLastNameVariable,
  street: yourStreetAddressVariable,
  city: yourCityVariable2,
  region: yourRegionVariable2,
  postal_code: yourPostalCodeVariable2
}
]
});
</script>
本页面可能包含使用 AI 技术翻译的内容。AI 翻译未必准确无误。

该内容对您有帮助吗?

您有什么改进建议?
搜索
清除搜索内容
关闭搜索框
主菜单
15200464254770975946
true
搜索支持中心
false
true
true
true
true
true
69256
false
false
false
false
false
⌂ 换网址