I’m trying to build a web pixel extension in order to track the browser-side events for Meta.
When I try to initialize the Meta Tracking script before the
analytics.subscribe(“page_viewed”, (event) => { … });
:
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', pixelId , customerData);
I get the following error:
[Web Pixels] Error: The creation of Web Pixel id "82411820" failed: {}
at jt (bab9016a5w09126b9fp260b266fmecbb582em.js:1:67568)
at async bab9016a5w09126b9fp260b266fmecbb582em.js:1:71110
at async Promise.all (index 0)
If I move the Meta’s script inside the subscription of the event I get the following error:
worker.modern.js:140 Uncaught (in promise) ReferenceError: window is not defined
I read Window is not available inside the web pixel extensions so this makes sense.
It is worth mentioning that I also tried another approach provided by Meta’s documentation here. But, this didn’t work either.
Here is the code I tested:
var fbtag = document.createElement("img");
var externalId = sha256("123456789");
fbtag.src="https://www.facebook.com/tr/?id=" + "123" + "&ev=" +
"PageView" + "&ud[external_id]=" + externalId + "&dl=" + "https://test.com";
document.body.appendChild(fbtag);
When I tried the code above I got the following error:
Uncaught (in promise) ReferenceError: document is not defined
at executeFbBrowserEvent (worker.modern.js:181:23)
at Object.
It seems like the document is not available either.
So, how are we supposed to track the browser events for the checkout steps once we update our store to the Checkout extensibility?
Can anyone help, please?
Thanks in advance.