Compare commits
2 Commits
1bb202ffa5
...
3561012fb9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3561012fb9 | ||
|
|
b26f6160f8 |
@@ -13,4 +13,6 @@ export * from './conversion/index.ts';
|
||||
// set log level to debug if we're in dev mode
|
||||
if (dev) {
|
||||
log.setLevel('debug');
|
||||
} else {
|
||||
log.setLevel('warn');
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ PixelControl interface.
|
||||
|
||||
import type { TrackingManager } from '../tracking.svelte.ts';
|
||||
import { onNavigate } from '$app/navigation';
|
||||
import { PixelControl, type PixelControlOptions } from './pixel-control.svelte.ts';
|
||||
import { PixelControl, type PixelControlOptions } from './pixel-control.ts';
|
||||
import { ensureFbc, type EnsureFbcOptions } from './fbc.ts';
|
||||
|
||||
interface Props {
|
||||
@@ -38,22 +38,20 @@ PixelControl interface.
|
||||
|
||||
let pixel = $state<PixelControl | null>(null);
|
||||
|
||||
const ensureFbcOptions: EnsureFbcOptions = $derived({
|
||||
sameSite: 'Lax',
|
||||
pixelLoaded: PixelControl.baseLoaded && !PixelControl.baseFailed
|
||||
});
|
||||
const fbcOptions: EnsureFbcOptions = {
|
||||
sameSite: 'Lax'
|
||||
};
|
||||
|
||||
onMount(() => {
|
||||
if (!trackingManager) {
|
||||
throw new Error('MetaPixel component requires a TrackingManager to manage consent.');
|
||||
}
|
||||
PixelControl.load();
|
||||
pixel = PixelControl.initialize(trackingManager, pixelID, pixelOptions);
|
||||
|
||||
trackingManager.runWithConsent(() => {
|
||||
if (autoPageView && pixel) {
|
||||
pixel.pageView();
|
||||
ensureFbc(ensureFbcOptions);
|
||||
ensureFbc(fbcOptions);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -62,7 +60,7 @@ PixelControl interface.
|
||||
trackingManager?.runWithConsent(() => {
|
||||
if (autoPageView && pixel) {
|
||||
pixel.pageView();
|
||||
ensureFbc(ensureFbcOptions);
|
||||
ensureFbc(fbcOptions);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -68,8 +68,7 @@ export class PixelControl {
|
||||
private _trackingManager: MaybeGetter<TrackingManager | undefined>;
|
||||
private _conversionClient?: ConversionClient = undefined;
|
||||
|
||||
private static _baseLoaded: boolean = $state(false);
|
||||
private static _baseFailed: boolean = $state(false);
|
||||
private static _baseLoaded: boolean = false;
|
||||
private static _registeredPixels: Record<string, PixelControl> = {};
|
||||
|
||||
/** Indicates whether the Meta Pixel base script has been loaded. */
|
||||
@@ -77,11 +76,6 @@ 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.
|
||||
@@ -116,20 +110,11 @@ export class PixelControl {
|
||||
}
|
||||
|
||||
/** Loads the Meta Pixel base script. */
|
||||
static async load() {
|
||||
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 {
|
||||
static load() {
|
||||
if (this._baseLoaded && !!window.fbq) return;
|
||||
loadMetaPixel(); // Load the Meta Pixel script
|
||||
this._baseLoaded = true;
|
||||
}
|
||||
}
|
||||
log.debug('[PixelControl] Meta Pixel base script loaded.');
|
||||
log.debug('[PixelControl] Meta Pixel base script loaded.', this._baseLoaded);
|
||||
}
|
||||
|
||||
/** Tells the Meta pixel that the user has given consent for tracking. */
|
||||
Reference in New Issue
Block a user