pixel: add ensure fbc fallback

This commit is contained in:
Elijah Duffy
2025-12-18 19:22:54 -08:00
parent f2d389ee64
commit 0cd3f10da6
5 changed files with 114 additions and 7 deletions

View File

@@ -68,7 +68,8 @@ export class PixelControl {
private _trackingManager: MaybeGetter<TrackingManager | undefined>;
private _conversionClient?: ConversionClient = undefined;
private static _baseLoaded: boolean = false;
private static _baseLoaded: boolean = $state(false);
private static _baseFailed: boolean = $state(false);
private static _registeredPixels: Record<string, PixelControl> = {};
/** Indicates whether the Meta Pixel base script has been loaded. */
@@ -76,6 +77,11 @@ export class PixelControl {
return this._baseLoaded;
}
/** Indicates whether the Meta Pixel base script has failed to load. */
static get baseFailed(): boolean {
return this._baseFailed;
}
/**
* Ensures that the Meta Pixel base has been loaded before
* allowing further operations.
@@ -111,15 +117,18 @@ export class PixelControl {
/** Loads the Meta Pixel base script. */
static async load() {
if (this._baseLoaded && !!window.fbq) return;
if (this._baseLoaded && !this.baseFailed && !!window.fbq) return;
if (!window.fbq) {
try {
await loadMetaPixel(); // Load the Meta Pixel script
} catch (e) {
log.warn('[PixelControl] Failed to load Meta Pixel script, all events will be queued.', e);
this._baseFailed = true;
return;
} finally {
this._baseLoaded = true;
}
}
this._baseLoaded = true;
log.debug('[PixelControl] Meta Pixel base script loaded.');
}