pixel: fix $state usage, fix load ordering

This commit is contained in:
Elijah Duffy
2025-12-18 21:22:07 -08:00
parent b26f6160f8
commit 3561012fb9
2 changed files with 12 additions and 29 deletions

View File

@@ -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);
}
});
});