diff --git a/packages/audience/pixel/src/snippet.test.ts b/packages/audience/pixel/src/snippet.test.ts index efef6c254d..b47be5ce38 100644 --- a/packages/audience/pixel/src/snippet.test.ts +++ b/packages/audience/pixel/src/snippet.test.ts @@ -45,4 +45,28 @@ describe('generateSnippet', () => { expect(html).toContain('s.async=1'); expect(html).toContain('document.head.appendChild(s)'); }); + + it('includes environment in init args when not production', () => { + const html = generateSnippet({ key: 'pk_test_123', environment: 'dev' }); + + expect(html).toContain('"environment":"dev"'); + }); + + it('includes sandbox environment in init args', () => { + const html = generateSnippet({ key: 'pk_test_123', environment: 'sandbox' }); + + expect(html).toContain('"environment":"sandbox"'); + }); + + it('omits environment from init args when set to production', () => { + const html = generateSnippet({ key: 'pk_test_123', environment: 'production' }); + + expect(html).not.toContain('environment'); + }); + + it('omits environment from init args when not provided', () => { + const html = generateSnippet({ key: 'pk_test_123' }); + + expect(html).not.toContain('environment'); + }); }); diff --git a/packages/audience/pixel/src/snippet.ts b/packages/audience/pixel/src/snippet.ts index ef5c7d59bb..8da1242b73 100644 --- a/packages/audience/pixel/src/snippet.ts +++ b/packages/audience/pixel/src/snippet.ts @@ -1,15 +1,23 @@ +import type { Environment } from '@imtbl/audience-core'; + const DEFAULT_CDN_URL = 'https://cdn.immutable.com/pixel/v1/imtbl.js'; export interface SnippetOptions { key: string; cdnUrl?: string; consent?: 'none' | 'anonymous' | 'full'; + environment?: Environment; } export function generateSnippet(options: SnippetOptions): string { - const { key, cdnUrl = DEFAULT_CDN_URL, consent } = options; + const { + key, cdnUrl = DEFAULT_CDN_URL, consent, environment, + } = options; const initArgs: Record = { key }; + if (environment && environment !== 'production') { + initArgs.environment = environment; + } if (consent) { initArgs.consent = consent; }