與使用 gtag 類似,Google Analytics (分析) 4 專用的 Google Analytics (分析) Measurement Protocol 可用來傳送「使用者提供的資料」和「User-ID」,進而改善行為和轉換評估作業。
如要與 Measurement Protocol 要求一併傳送使用者提供的資料,請在 JSON 酬載中加入 user_data
參數。每次提供 user_data
時,都必須存在 user_id
參數。
Measurement Protocol 使用的正規化和雜湊演算法與 Google Ads API 加強型評估功能相同。基於隱私權考量,電子郵件地址、電話號碼、名字、姓氏和街道地址都必須使用 SHA-256 演算法進行雜湊處理,才能上傳。雜湊值應以十六進位字串格式編碼 (字串物件僅包含十六進位數字),例如 88d7ecb5c5b21d7b1
。
為了將雜湊結果標準化,在對其中一個值進行雜湊處理之前,您必須:
- 移除開頭和結尾的空白字元。
- 將文字轉換為小寫。
- 根據 E164 標準設定電話號碼格式。
- 移除
gmail.com
和googlemail.com
電子郵件地址中網域名稱前的所有半形句號 (.)。
JSON 貼文內文
鍵 | 類型 | 說明 |
---|---|---|
user_id | 字串 | 使用者的專屬 ID。如要進一步瞭解這個 ID,請參閱跨平台分析的 User-ID。 |
user_data | 物件 | 用來識別使用者的進階使用者資料欄位。 |
user_data.sha256_email_address[] | 字串陣列 | 經過雜湊處理及編碼的使用者電子郵件地址。
已正規化,例如:
|
user_data.sha256_phone_number[] | 字串陣列 | 使用者的電話號碼進行雜湊處理並編碼。
已正規化,例如:
|
user_data.address[] | 陣列 | 根據實際位置識別使用者。 |
user_data.address[].sha256_first_name | 字串 | 使用者的名字經過雜湊處理並編碼。
已正規化,例如:
|
user_data.address[].sha256_last_name | 字串 | 使用者的姓氏經過雜湊處理並編碼。
已正規化,例如:
|
user_data.address[].sha256_street | 字串 | 雜湊及編碼的街道和使用者號碼。
已正規化,例如:
|
user_data.address[].city | 字串 | 使用者地址的城市。
已正規化,例如:
|
user_data.address[].region | 字串 | 使用者地址的州/省或地域。
已正規化,例如:
|
user_data.address[].postal_code | 字串 | 使用者地址的郵遞區號。
已正規化,例如:
|
user_data.address[].country | 字串 | 使用者地址的國家/地區代碼。採用 ISO 3166-1 alpha-2 標準格式。 |
如要進一步瞭解傳輸和酬載的格式,請參閱 Measurement Protocol 參考說明文件。
傳送使用者提供的資料
有別於 gtag 會自動對使用者提供的機密資料進行雜湊處理,Measurement Protocol 要求開發人員使用 SHA256 這種安全的單向雜湊演算法,雜湊處理使用者提供的資料,然後在呼叫 API 前使用十六進位字串格式進行編碼。
名稱中以 sha256
前置字串開頭的所有使用者資料欄位,都只能填入雜湊和十六進位編碼值。
下列程式碼範例會執行必要的加密和編碼步驟:
Node.js
const { subtle } = require('crypto').webcrypto;
async function populateSensitiveUserData(value) {
const encoder = new TextEncoder();
// Convert a string value to UTF-8 encoded text.
const value_utf8 = encoder.encode(value);
// Compute the hash (digest) using the SHA-256 algorithm.
const hash_sha256 = await subtle.digest('SHA-256', value_utf8);
// Convert buffer to byte array.
const hash_array = Array.from(new Uint8Array(hash_sha256));
// Return a hex-encoded string.
return hash_array.map(b => b.toString(16).padStart(2, "0")).join('');
};
// Test the encryption function by calling it.
async function main() {
return await populateSensitiveUserData('<value>');
}
main()
.then(v => console.log(v))
.catch(err => console.error(err));
為方便起見,user_data
物件中的所有重複欄位 (例如 address
、sha256_email_address
、sha256_phone_number
) 都可傳遞單一值,而非陣列。
下列程式碼範例會呼叫 Measurement Protocol,並將使用者資料和 User-ID 一併傳遞。
Node.js
const measurement_id = 'G-XXXXXXXXXX';
const api_secret = '<secret_value>';
// Populate mock User Data using the `populateSensitiveUserData` function defined
// above.
const yourEmailSha256Variable = await populateSensitiveUserData('test@yourdomain.com');
const yourPhoneSha256Variable = await populateSensitiveUserData('+15555555555');
const yourFirstNameSha256Variable = await populateSensitiveUserData('john');
const yourLastNameSha256Variable = await populateSensitiveUserData('doe');
const yourStreetAddressSha256Variable = await populateSensitiveUserData('123 main street');
// Populate mock unencrypted user data.
const yourCityVariable = 'san francisco';
const yourRegionVariable = 'california';
const yourPostalCodeVariable = '94000';
const yourCountryVariable = 'US';
fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}`, {
method: "POST",
body: JSON.stringify({
client_id: 'XXXXXXXXXX.YYYYYYYYYY',
user_id: "XXX",
events: [{
name: 'purchase'
}],
user_data: {
sha256_email_address: yourEmailSha256Variable,
sha256_phone_number: yourPhoneSha256Variable,
address: {
sha256_first_name: yourFirstNameSha256Variable,
sha256_last_name: yourLastNameSha256Variable,
sha256_street: yourStreetAddressSha256Variable,
city: yourCityVariable,
region: yourRegionVariable,
postal_code: yourPostalCodeVariable,
country: yourCountryVariable
}
}
})
});
多個值
開發人員可以選擇使用陣列值 (而非字串) 提供多個值 (最多 3 個電話號碼和電子郵件,最多 2 個地址)。擷取多個值時,可以提高比對成功的機率。
Node.js
const measurement_id = 'G-XXXXXXXXXX';
const api_secret = '<secret_value>';
fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}`, {
method: "POST",
body: JSON.stringify({
client_id: 'XXXXXXXXXX.YYYYYYYYYY',
user_id: "XXX",
events: [{
name: 'purchase'
}],
user_data: {
sha256_email_address: [yourEmailSha256Variable1, yourEmailSha256Variable2],
sha256_phone_number: [yourPhoneSha256Variable1, yourPhoneSha256Variable2],
address: [{
sha256_first_name: yourFirstNameSha256Variable1,
sha256_last_name: yourLastNameSha256Variable1,
sha256_street: yourStreetAddressSha256Variable1,
city: yourCityVariable1,
region: yourRegionVariable1,
postal_code: yourPostalCodeVariable1,
country: yourCountryVariable1
},{
sha256_first_name: yourFirstNameSha256Variable2,
sha256_last_name: yourLastNameSha256Variable2,
sha256_street: yourStreetAddressSha256Variable2,
city: yourCityVariable2,
region: yourRegionVariable2,
postal_code: yourPostalCodeVariable2,
country: yourCountryVariable2
}]
}
})
});