-
Notifications
You must be signed in to change notification settings - Fork 15
fix(fb-pixel): guard global fbq bootstrap so multiple pixel destinations don't conflict #3097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,21 +53,28 @@ class FacebookPixel { | |
| } | ||
|
|
||
| init() { | ||
| window._fbq = function () { | ||
| if (window.fbq.callMethod) { | ||
| window.fbq.callMethod.apply(window.fbq, arguments); | ||
| } else { | ||
| window.fbq.queue.push(arguments); | ||
| } | ||
| }; | ||
| // Bootstrap the global fbq shim only once per page. When multiple Meta Pixel | ||
| // destinations are configured, each one calls init(), and re-running this block | ||
| // mutates the already loaded fbevents.js SDK (resetting fbq.version, fbq.queue, etc.), | ||
| // which makes Meta log "Multiple pixels with conflicting versions were detected" | ||
| // and prevents the second pixel from initializing. Guard it like Meta's own snippet. | ||
| if (!window.fbq) { | ||
| window._fbq = function () { | ||
| if (window.fbq.callMethod) { | ||
| window.fbq.callMethod.apply(window.fbq, arguments); | ||
| } else { | ||
| window.fbq.queue.push(arguments); | ||
| } | ||
| }; | ||
|
|
||
| window.fbq = window.fbq || window._fbq; | ||
| window.fbq.push = window.fbq; | ||
| window.fbq.loaded = true; | ||
| window.fbq.disablePushState = true; // disables automatic pageview tracking | ||
| window.fbq.allowDuplicatePageViews = true; // enables fb | ||
| window.fbq.version = '2.0'; | ||
| window.fbq.queue = []; | ||
| window.fbq = window.fbq || window._fbq; | ||
| window.fbq.push = window.fbq; | ||
| window.fbq.loaded = true; | ||
| window.fbq.disablePushState = true; // disables automatic pageview tracking | ||
| window.fbq.allowDuplicatePageViews = true; // enables fb | ||
| window.fbq.version = '2.0'; | ||
| window.fbq.queue = []; | ||
|
Comment on lines
+61
to
+74
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I moved disablePushState and allowDuplicatePageViews out of the one-time guard so they run on every init(), including when window.fbq was already bootstrapped by the host page or an earlier destination. Only the shim creation and version/queue setup (the parts that cause the version conflict) stay guarded. Added a test covering the pre-existing fbq case. |
||
| } | ||
| window.fbq('set', 'autoConfig', this.autoConfig, this.pixelId); // toggle autoConfig : sends button click and page metadata | ||
| if (this.advancedMapping) { | ||
| if (this.useUpdatedMapping) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. I now capture the previous window.fbq and window._fbq in beforeEach and restore them in an afterEach, so this suite no longer leaks global state into the sibling describes.