Skip to content

Commit fc9f900

Browse files
bkboothclaude
andauthored
fix(pixel): support environment in snippet generator for non-prod endpoints (#2849)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 008b6ea commit fc9f900

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/audience/pixel/src/snippet.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,28 @@ describe('generateSnippet', () => {
4545
expect(html).toContain('s.async=1');
4646
expect(html).toContain('document.head.appendChild(s)');
4747
});
48+
49+
it('includes environment in init args when not production', () => {
50+
const html = generateSnippet({ key: 'pk_test_123', environment: 'dev' });
51+
52+
expect(html).toContain('"environment":"dev"');
53+
});
54+
55+
it('includes sandbox environment in init args', () => {
56+
const html = generateSnippet({ key: 'pk_test_123', environment: 'sandbox' });
57+
58+
expect(html).toContain('"environment":"sandbox"');
59+
});
60+
61+
it('omits environment from init args when set to production', () => {
62+
const html = generateSnippet({ key: 'pk_test_123', environment: 'production' });
63+
64+
expect(html).not.toContain('environment');
65+
});
66+
67+
it('omits environment from init args when not provided', () => {
68+
const html = generateSnippet({ key: 'pk_test_123' });
69+
70+
expect(html).not.toContain('environment');
71+
});
4872
});

packages/audience/pixel/src/snippet.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
import type { Environment } from '@imtbl/audience-core';
2+
13
const DEFAULT_CDN_URL = 'https://cdn.immutable.com/pixel/v1/imtbl.js';
24

35
export interface SnippetOptions {
46
key: string;
57
cdnUrl?: string;
68
consent?: 'none' | 'anonymous' | 'full';
9+
environment?: Environment;
710
}
811

912
export function generateSnippet(options: SnippetOptions): string {
10-
const { key, cdnUrl = DEFAULT_CDN_URL, consent } = options;
13+
const {
14+
key, cdnUrl = DEFAULT_CDN_URL, consent, environment,
15+
} = options;
1116

1217
const initArgs: Record<string, string> = { key };
18+
if (environment && environment !== 'production') {
19+
initArgs.environment = environment;
20+
}
1321
if (consent) {
1422
initArgs.consent = consent;
1523
}

0 commit comments

Comments
 (0)