[GA4] Thiết lập đoạn mã (Trình quản lý thẻ)

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

Đây là hướng dẫn dành cho những quản trị viên và người chỉnh sửa muốn tìm hiểu cách thiết lập đoạn mã cho tính năng thu thập dữ liệu do người dùng cung cấp trên trang web của họ bằng Trình quản lý thẻ của Google.

So với các lựa chọn khác về cấu hình, việc cập nhật mã sẽ giúp đảm bảo rằng địa chỉ email, số điện thoại và/hoặc tên và địa chỉ sẽ có trên trang khi Analytics thu thập dữ liệu do người dùng cung cấp.

Tổng quan

Để thiết lập đoạn mã bằng Trình quản lý thẻ của Google, hãy hoàn tất các bước sau:

  1. Nhận diện và xác định các trường dữ liệu do người dùng cung cấp trong mã của trang web của bạn
  2. Truyền từng trường dữ liệu do người dùng cung cấp trong một biến JavaScript tuỳ chỉnh đến Google Analytics

Lưu ý: Bạn có thể gửi dữ liệu khách hàng đã băm hoặc chưa băm đến Google Analytics. Khi bạn gửi dữ liệu chưa băm, Google sẽ băm dữ liệu đó cho bạn trước khi lưu trữ dữ liệu trong máy chủ của Google. Tuy nhiên, bạn cũng có thể tự băm dữ liệu trước khi gửi dữ liệu đó đến Google.

Trước khi bắt đầu

Hướng dẫn này giả định rằng bạn đáp ứng các điều kiện sau:

  • Có quyền truy cập vào mã nguồn của trang web
  • Đã kích hoạt tính năng thu thập dữ liệu do người dùng cung cấp và được phép thu thập dữ liệu do người dùng cung cấp
  • Đã thiết lập thẻ Google Analytics: Sự kiện GA4 trong vùng chứa Trình quản lý thẻ của Google
  • Có vai trò Quản trị viên đối với tài sản 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.

Cập nhật thẻ sự kiện bằng một biến JavaScript tuỳ chỉnh

Trước khi cập nhật vùng chứa Trình quản lý thẻ, hãy đọc về cách cập nhật mã JavaScript tuỳ chỉnh mà bạn sẽ thêm vào vùng chứa của mình.

  1. Đăng nhập vào Trình quản lý thẻ của Google.
  2. Trong thẻ Không gian làm việc, hãy nhấp vào Thẻ ở bên trái.
  3. Mở thẻ Google Analytics: Sự kiện GA4 mà bạn dùng để thu thập dữ liệu khách hàng. Đảm bảo thẻ sự kiện sử dụng mã đo lường cho luồng dữ liệu web là nơi dữ liệu người dùng được thu thập.
  4. Chọn Bao gồm dữ liệu do người dùng cung cấp từ trang web của bạn.
  5. Trong trình đơn thả xuống Dữ liệu người dùng, hãy chọn hoặc tạo một biến Dữ liệu do người dùng cung cấp.
  6. Nếu bạn đang tạo một biến mới, hãy chọn trong biến Dữ liệu do người dùng cung cấp.
  7. Trong trình đơn thả xuống Nguồn dữ liệu, hãy nhấp vào Biến mới....
  8. Nhấp vào Cấu hình biến > JavaScript tuỳ chỉnh.
  9. Nhập tên cho biến JavaScript tuỳ chỉnh ở đầu trang.
  10. Nhập đoạn mã vào hộp văn bản JavaScript tuỳ chỉnh. Tìm hiểu cách làm.
  11. Lưu các biến mới, rồi lưu thẻ sự kiện.
  12. Ở trên cùng bên phải, hãy nhấp vào Gửi để xuất bản các thay đổi.

Tạo đoạn mã để thu thập dữ liệu do người dùng cung cấp

Bạn có thể sử dụng mã sau đây khi thiết lập biến JavaScript tuỳ chỉnh ở bước 10:

function () {

return {

"email": yourEmailVariable , // replace yourEmailVariable with variable name that captures your user’s email

"phone_number": yourPhoneVariable , // repeat for yourPhoneVariable and following variable names below

"address": {

"first_name": yourFirstNameVariable ,

"last_name": yourLastNameVariable ,

"street": yourStreetAddressVariable ,

"city": yourCityVariable ,

"region": yourRegionVariable ,

"postal_code": yourPostalCodeVariable ,

"country": yourCountryVariable

}

}

}

Đối với mỗi loại dữ liệu người dùng trong mã mẫu, hãy thay thế các biến phần giữ chỗ (ví dụ: yourEmailVariable) bằng tên của biến JavaScript chung chứa phần dữ liệu đó. Nếu trang web của bạn không thu thập một trong các trường đó, hãy xoá toàn bộ trường thay vì để trống.

Ngoài ra, bạn có thể cố định giá trị trong mã của trường này bằng một chuỗi hoặc sử dụng một hàm thay vì các biến.

// Implement
<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>

Nếu muốn cung cấp giá trị dữ liệu người dùng dạng băm, bạn có thể sử dụng đoạn mã mẫu sau đây:

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;

Số điện thoại phải ở định dạng E.164, tức là phải có từ 11 đến 15 chữ số, bắt đầu bằng dấu cộng (+), tiếp đến là mã quốc gia, không có dấu gạch ngang, dấu ngoặc vuông hoặc dấu cách.

Next steps

Validate that user-provided data collection is set up correctly. Show me how.

Thông tin này có hữu ích không?

Chúng tôi có thể cải thiện trang này bằng cách nào?
Tìm kiếm
Xóa nội dung tìm kiếm
Đóng tìm kiếm
Trình đơn chính
15014690977048286755
true
Tìm kiếm trong Trung tâm trợ giúp
true
true
true
true
true
69256
false
false