[GA4] เพิ่มข้อมูลโค้ดลงในเว็บไซต์ (gtag.js)

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

คำสั่งเหล่านี้มีไว้สำหรับผู้ดูแลระบบและผู้แก้ไขที่กำลังตั้งค่าการเก็บรวบรวมข้อมูลที่ได้จากผู้ใช้ และต้องการดูวิธีเพิ่มข้อมูลโค้ดลงในเว็บไซต์โดยใช้ gtag.js การอัปเดตโค้ดในเว็บไซต์ช่วยให้มั่นใจได้ว่าอีเมล หมายเลขโทรศัพท์ และ/หรือชื่อและที่อยู่พร้อมใช้งานบนหน้าเว็บเมื่อ Analytics รวบรวมข้อมูลที่ได้จากผู้ใช้

ภาพรวม

หากต้องการอัปเดตโค้ดของเว็บไซต์ คุณต้องตั้งค่า user_data พร้อมกับช่องข้อมูลที่ได้จากผู้ใช้แต่ละช่องภายในคำสั่ง event, config หรือ set โดยใช้ฟังก์ชัน gtag( )

หากคุณส่งข้อมูลเดียวกันภายในประเภทคำสั่ง 2 ประเภทขึ้นไป (เช่น event และ config) คำสั่ง event จะลบล้างคำสั่ง config และคำสั่ง config จะลบล้างคำสั่ง set ดูข้อมูลเกี่ยวกับลำดับความสำคัญของพารามิเตอร์ของแท็ก Google

คุณควรอัปเดตโค้ดในหน้าที่รวบรวมข้อมูลที่ได้จากผู้ใช้ เช่น หน้าลงชื่อสมัครใช้ หน้าการสร้างผู้มีโอกาสเป็นลูกค้า หน้าชำระเงิน หรือหน้าอื่นๆ ที่คล้ายกัน คุณสามารถส่งข้อมูลได้มากกว่า 1 ครั้งภายในเซสชันเพื่อให้ระบบส่งข้อมูลไปยัง 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)

คุณยังฮาร์ดโค้ดช่องด้วยสตริงหรือใช้ฟังก์ชันแทนตัวแปรได้ด้วย

// Implement

<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 หลัก โดยมีเครื่องหมายบวก (+) นำหน้าและรหัสประเทศที่ไม่มีขีดกลางยาว วงเล็บ หรือเว้นวรรค

นำช่องที่ว่างออก

หากคุณไม่รวบรวมบางช่องจากโค้ดตัวอย่างก่อนหน้า ให้นำช่องทั้งหมดออกจากโค้ดแทนที่จะปล่อยให้ว่างไว้ ตัวอย่างต่อไปนี้แสดงลักษณะของเว็บไซต์ที่รวบรวมเฉพาะอีเมลและหมายเลขโทรศัพท์

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

เพิ่มหลายค่า

คุณระบุค่าได้หลายค่า (หมายเลขโทรศัพท์และอีเมลสูงสุด 3 รายการ และที่อยู่ 2 รายการ) โดยใช้อาร์เรย์ หากบันทึกมากกว่า 1 ค่า จะเพิ่มโอกาสในการจับคู่มากยิ่งขึ้น

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

ข้อมูลนี้มีประโยชน์ไหม

เราจะปรับปรุงได้อย่างไร
ค้นหา
ล้างการค้นหา
ปิดการค้นหา
เมนูหลัก
14723668147812523639
true
ค้นหาศูนย์ช่วยเหลือ
true
true
true
true
true
69256
false
false