[GA4] Menambahkan cuplikan kode ke situs (gtag.js)

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

Petunjuk ini ditujukan bagi administrator dan editor yang menyiapkan pengumpulan data yang disediakan pengguna dan ingin mempelajari cara menambahkan cuplikan kode ke situs mereka menggunakan gtag.js. Memperbarui kode di situs Anda akan membantu memastikan alamat email, nomor telepon, dan/atau nama dan alamat tersedia di halaman saat Analytics mengumpulkan data yang disediakan pengguna.

Ringkasan

Untuk memperbarui kode situs, Anda harus menetapkan nilai user_data bersama dengan setiap kolom data yang disediakan pengguna dalam perintah event, config, atau set menggunakan fungsi gtag( ).

Jika Anda meneruskan data yang sama dalam dua atau beberapa jenis perintah (misalnya, event dan config), maka perintah event akan menggantikan perintah config, dan perintah config akan menggantikan perintah set. Pelajari prioritas parameter tag Google.

Anda harus memperbarui kode di setiap halaman tempat Anda mengumpulkan data yang disediakan pengguna, seperti halaman pendaftaran, halaman perolehan prospek, halaman checkout, atau halaman yang serupa. Anda dapat mengirimkan data lebih dari sekali dalam satu sesi untuk memastikan data tersebut dikirim ke Analytics.

Sebelum memulai

Petunjuk ini mengasumsikan bahwa Anda telah:

  • Memiliki akses ke kode sumber situs Anda
  • Mengaktifkan pengumpulan data yang disediakan pengguna dan mengizinkan penyertaan data yang disediakan pengguna

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.

Menerapkan kolom dalam kode Anda

Di setiap halaman tempat Anda mengumpulkan data pelanggan, konfigurasi parameter user_data dengan setiap kolom yang ingin Anda tentukan.

Lihat contoh

Contoh berikut menyertakan nama variabel untuk data pelanggan. Pastikan nama variabel dalam kode cocok dengan atribut di situs Anda. Misalnya, jika Anda menyimpan email dalam variabel bernama “email_address”, Anda harus memperbarui cuplikan dengan nama tersebut (misalnya, yourEmailVariable).

Anda juga dapat melakukan hardcode pada kolom dengan string atau menggunakan fungsi, bukan menggunakan variabel.

// Implement

<script>
gtag('set', 'user_data', {
"email": yourEmailVariable, ***Ubah yourEmailVariable menjadi nama variabel JavaScript yang sebenarnya tempat Anda menyimpan data email pengguna. Lakukan hal yang sama untuk variabel lainnya di bawah. Pastikan nilainya tidak di-hash.
"phone_number": yourPhoneVariable,
"address": {
"first_name": yourFirstNameVariable,
"last_name": yourLastNameVariable,
"street": yourStreetAddressVariable,
"city":yourCityVariable,
"region": yourRegionVariable,
"postal_code": yourPostalCodeVariable,
"country": yourCountryVariable
}
});
</script>

Nomor telepon harus dalam format E.164, yang berarti harus terdiri dari 11 sampai 15 digit, termasuk awalan tanda plus (+) dan kode negara tanpa tanda hubung, tanda kurung, atau spasi.

Menghapus kolom kosong

Jika Anda tidak mengumpulkan kolom tertentu dari kode contoh sebelumnya, hapus kolom tersebut seluruhnya dari kode, jangan dikosongkan. Misalnya, gambar berikut menunjukkan tampilan situs yang hanya mengumpulkan alamat email dan nomor telepon:

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

Menambahkan beberapa nilai

Anda dapat memberikan beberapa nilai (maksimal 3 nomor telepon dan alamat email, serta 2 alamat) menggunakan array. Jika Anda mengambil lebih dari satu nilai, memberikan nilai ini akan meningkatkan kemungkinan kecocokan.

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

Apakah ini membantu?

Bagaimana cara meningkatkannya?
Telusuri
Hapus penelusuran
Tutup penelusuran
Menu utama
12847410816883862671
true
Pusat Bantuan Penelusuran
true
true
true
true
true
69256
false
false