pixel: allow disabling conversion API forwarding

This commit is contained in:
Elijah Duffy
2025-12-18 21:27:44 -08:00
parent 3561012fb9
commit 6692338b83

View File

@@ -197,14 +197,15 @@ export class PixelControl {
/** /**
* Sends a PageView event * Sends a PageView event
* @param disableCAPI If true, disables sending this event to the Conversion API
* @throws Error if the Meta Pixel is not initialized. * @throws Error if the Meta Pixel is not initialized.
*/ */
pageView() { pageView(disableCAPI: boolean = false) {
if (!this.consentGuard()) return; if (!this.consentGuard()) return;
let eventID: string | undefined = undefined; let eventID: string | undefined = undefined;
// Optionally, send to conversion API endpoint if configured // Optionally, send to conversion API endpoint if configured
if (this._conversionClient) { if (this._conversionClient && !disableCAPI) {
eventID = crypto.randomUUID(); eventID = crypto.randomUUID();
this._conversionClient this._conversionClient
.trackEvent('PageView', { eventID }) .trackEvent('PageView', { eventID })
@@ -241,13 +242,22 @@ export class PixelControl {
/** /**
* Tracks a standard event for this pixel (uses `trackSingle` under the hood) * Tracks a standard event for this pixel (uses `trackSingle` under the hood)
* @param event Standard event name
* @param params Event parameters
* @param eventID Optional event ID for deduplication with Conversion API
* @param disableCAPI If true, disables sending this event to the Conversion API
* @throws Error if the Meta Pixel is not initialized. * @throws Error if the Meta Pixel is not initialized.
*/ */
track<K extends StandardEventName>(event: K, params?: EventParamsByName[K], eventID?: string) { track<K extends StandardEventName>(
event: K,
params?: EventParamsByName[K],
eventID?: string,
disableCAPI: boolean = false
) {
if (!this.consentGuard()) return; if (!this.consentGuard()) return;
// Optionally, send to conversion API endpoint if configured // Optionally, send to conversion API endpoint if configured
if (this._conversionClient) { if (this._conversionClient && !disableCAPI) {
eventID = eventID ?? crypto.randomUUID(); eventID = eventID ?? crypto.randomUUID();
this._conversionClient this._conversionClient
.trackEvent(event, { eventID: eventID, customData: pixelParamsToCustomData(params ?? {}) }) .trackEvent(event, { eventID: eventID, customData: pixelParamsToCustomData(params ?? {}) })