如何使用初始化后将填充的数据初始化 Facebook Pixel?

How to initialize Facebook Pixel with data that will be populated after the initialization?

我有一个单页广告活动网站,该网站应收集 PageViewLEAD 事件数据,以便通过 "advanced matching" 进行 Facebook 定位。

对于 PageView,我需要在页面加载时初始化 Pixel。对于高级匹配,我需要为初始化脚本提供用户数据——但是,我只有在用户提交数据后才能获得用户数据,也就是应该发送 LEAD 事件的时候。

是否可以为发送 LEAD 事件时使用的 javascript 变量提供带有 "pointer" 的初始化脚本?帮助页面对此有提示,但所有示例都有例如以纯文本或 sha256 散列形式提供的电子邮件参数;既不是 JS 变量,也不是指向一个的文本指针。

来自"Advanced Matching with the Pixel" help page

To enable this feature, modify the default FB pixel code to pass data into the pixel init call.

fbq('init', '<FB_PIXEL_ID>', { 
    em: '{{_email_}}', 
    // Data will be hashed automatically via a dedicated function in FB pixel
    ph: '{{_phone_number_}}',
    fn: '{{_first_name_}}'
    ....
})

In the example above, you will need to replace email, phone_number, first_name with the names of the variables on your website that capture this data.

假设我有 user_email 变量在与 fbq-init:

相同的范围内
var user_email = '';

稍后,在提交表单后,这个变量将被填充为

user_email = "john@example.com";

我应该如何引用fbq中的变量? (1) 像这样?

fbq('init', 123456, { em: '{{_user_email_}}' });

(2) 还是这样?

fbq('init', 123456, { em: 'user_email' });

(3) 或者我应该简单地用变量提供它:

fbq('init', 123456, { em: user_email }); // nb: user_email === false when this is run

(4) 或者我应该在没有任何匹配数据的情况下初始化像素然后丰富它? (如何?)

一家信誉良好的广告代理商向我发送了说明,要求我按照示例 2 中的方式提交变量名称,但帮助页面提示为 1,但实际上并没有提供任何实际示例。


我尝试了 1-3 的所有选项,似乎变量不会在初始化后的后续 fbq('track', …); 调用中重新计算。如果 user_email 开头为空白,则 none 的事件将包含经过哈希处理的用户数据。

此外,即使我从有效的电子邮件 user_email = "john@example.com"; 开始(也尝试使用真实域而不是 example.com),只有方法 3 会起作用——但是,正如预期的那样,这不是动态的,即。如果 user_email 更改哈希值则不会。

好像没有开始的字符串变量替换。

更恰当的问题是:如何在像素初始化后提交用户数据以进行高级匹配 而不是尝试进行初始化 lazy/dynamic.

作为解决方法,可以通过 javascript 加载像素(图像)来生成带有电子邮件用户数据的 Lead 事件:

// here the user email is unknown, so PageView is generated without email
fbq('init', 123456, {});
fbq('track', 'PageView');

// Later the form is submitted via ajax, so the user email is known
$.ajax(…).done(function(data) {
    // I decided to sha256 hash the email address in the backend and return it for the javascript handler
    var pixel = document.createElement('img');
    pixel.src = 'https://www.facebook.com/tr/?id=123456&ev=Lead&ud[em]=' + data;
    document.body.appendChild(pixel);
    // confirmed by the Pixel helper Chrome extension, this does generate a valid tracking event
    // (ironically, we're loading the noscript version via javascript)
});

当然,纯fbq版本会更好。另外,我还没有意识到可能存在的缺点,如果有的话。