Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/script/src/runtime/registry/meta-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ export function useScriptMetaPixel<T extends MetaPixelApi>(_options?: MetaPixelI
// @ts-expect-error untyped
fbq.callMethod(...params)
}
else if (params[0] === 'consent') {
// fbevents replays this queue in order and stops at a revoke, so a grant queued
// behind one never runs. Before it loads, consent replaces the pending state
// ahead of every queued command.
for (let i = fbq.queue.length - 1; i >= 0; i--) {
if (fbq.queue[i][0] === 'consent')
fbq.queue.splice(i, 1)
}
fbq.queue.unshift(params)
}
else {
fbq.queue.push(params)
}
Expand Down
31 changes: 28 additions & 3 deletions test/nuxt-runtime/consent-default.nuxt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,44 @@ describe('per-script consent object', () => {
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('ad_storage'))
})

it('meta: consent.grant()/revoke() queue fbq(\'consent\', ...) calls', async () => {
it('meta: consent.grant()/revoke() before load keep only the latest consent at the head of the queue', async () => {
const { useScriptMetaPixel } = await import('../../packages/script/src/runtime/registry/meta-pixel')
const result: any = useScriptMetaPixel({ id: '123' })
result._opts.clientInit()
;(window as any).fbq.queue = []
result.consent.grant()
result.consent.revoke()
expect((window as any).fbq.queue).toEqual([
['consent', 'grant'],
['consent', 'revoke'],
['init', '123'],
['track', 'PageView'],
])
})

it('meta: consent.grant() before load replaces defaultConsent \'denied\' instead of queueing behind it', async () => {
const { useScriptMetaPixel } = await import('../../packages/script/src/runtime/registry/meta-pixel')
const result: any = useScriptMetaPixel({ id: '123', defaultConsent: 'denied' })
result._opts.clientInit()
;(window as any).fbq('track', 'Lead')
result.consent.grant()
expect((window as any).fbq.queue).toEqual([
['consent', 'grant'],
['init', '123'],
['track', 'PageView'],
['track', 'Lead'],
])
})

it('meta: consent calls go straight to fbevents once it has loaded', async () => {
const { useScriptMetaPixel } = await import('../../packages/script/src/runtime/registry/meta-pixel')
const result: any = useScriptMetaPixel({ id: '123', defaultConsent: 'denied' })
result._opts.clientInit()
const callMethod = vi.fn()
;(window as any).fbq.callMethod = callMethod
result.consent.grant()
expect(callMethod).toHaveBeenCalledWith('consent', 'grant')
expect((window as any).fbq.queue[0]).toEqual(['consent', 'revoke'])
})

it('tiktok: consent.grant()/revoke()/hold() queue ttq consent actions', async () => {
const { useScriptTikTokPixel } = await import('../../packages/script/src/runtime/registry/tiktok-pixel')
const result: any = useScriptTikTokPixel({ id: 'CA123' })
Expand Down
Loading