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
24 changes: 24 additions & 0 deletions packages/audience/pixel/src/snippet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
10 changes: 9 additions & 1 deletion packages/audience/pixel/src/snippet.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = { key };
if (environment && environment !== 'production') {
initArgs.environment = environment;
}
if (consent) {
initArgs.consent = consent;
}
Expand Down
Loading