diff --git a/src/lib/MetaPixel.svelte b/src/lib/MetaPixel.svelte index 4326498..f78fbe8 100644 --- a/src/lib/MetaPixel.svelte +++ b/src/lib/MetaPixel.svelte @@ -57,17 +57,22 @@ * Returns a PixelControl instance for the given Meta Pixel ID. If * the base Meta Pixel script has not been loaded yet, it will be * loaded automatically. Optionally sets a test event code for the Pixel. + * Does NOT initialize the Pixel; call `fireInit()` on the returned instance + * before tracking events. + * @param trackingManager Tracking manager to handle user consent for tracking * @param pixelID Meta Pixel ID - * @param testEventCode Optional test event code + * @param options Optional settings * @returns PixelControl instance */ static for( trackingManager: MaybeGetter, pixelID: string, options?: { + /** + * if provided, events fired will always have this code attached + * to prevent them from polluting real analytics data. + */ testEventCode?: string; - advancedMatching?: AdvancedMatching; - initOptions?: InitOptions; } ): PixelControl { PixelControl.load(); @@ -75,6 +80,19 @@ return new PixelControl(trackingManager, pixelID, options?.testEventCode); } + /** + * Initializes this pixel with the Meta Pixel API including any advanced + * matching data and options. + * @param advancedMatching Advanced matching data + * @param initOptions Initialization options + * @returns this PixelControl instance + */ + fireInit(advancedMatching?: AdvancedMatching, initOptions?: InitOptions): PixelControl { + PixelControl.loadGuard(); + window.fbq('init', this._pixelID, advancedMatching, initOptions); + return this; + } + /** * Checks if the Meta Pixel has consent to track user data * and if the Pixel has been loaded. @@ -171,7 +189,7 @@ throw new Error('MetaPixel component requires a TrackingManager to manage consent.'); } PixelControl.load(); - pixel = PixelControl.for(trackingManager, pixelID, { testEventCode }); + pixel = PixelControl.for(trackingManager, pixelID, { testEventCode }).fireInit(); trackingManager.runWithConsent(() => { if (autoPageView && pixel) {