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
4 changes: 0 additions & 4 deletions packages/audience/pixel/src/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

const mockInit = jest.fn();
const mockPage = jest.fn();
const mockIdentify = jest.fn();
const mockSetConsent = jest.fn();
const mockDestroy = jest.fn();

jest.mock('./pixel', () => ({
Pixel: jest.fn().mockImplementation(() => ({
init: mockInit,
page: mockPage,
identify: mockIdentify,
setConsent: mockSetConsent,
destroy: mockDestroy,
})),
Expand Down Expand Up @@ -65,14 +63,12 @@ describe('bootstrap', () => {
(window as Record<string, unknown>).__imtbl = [
['init', { key: 'pk_test' }],
['consent', 'full'],
['identify', 'user-1', 'passport', { email: 'a@b.com' }],
];

require('./bootstrap');

expect(mockInit).toHaveBeenCalledWith({ key: 'pk_test' });
expect(mockSetConsent).toHaveBeenCalledWith('full');
expect(mockIdentify).toHaveBeenCalledWith('user-1', 'passport', { email: 'a@b.com' });
});

it('installs loader and handles new commands after load', () => {
Expand Down
7 changes: 0 additions & 7 deletions packages/audience/pixel/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ function handleCommand(command: Command): void {
case 'page':
pixel.page(args[0] as Parameters<Pixel['page']>[0]);
break;
case 'identify':
pixel.identify(
args[0] as string,
args[1] as Parameters<Pixel['identify']>[1],
args[2] as Parameters<Pixel['identify']>[2],
);
break;
case 'consent':
pixel.setConsent(args[0] as Parameters<Pixel['setConsent']>[0]);
break;
Expand Down
8 changes: 4 additions & 4 deletions packages/audience/pixel/src/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ describe('createLoader', () => {
const loader = createLoader(handler);

loader.push(['init', { key: 'pk_123' }]);
loader.push(['identify', 'user-1']);
loader.push(['page', { url: '/home' }]);

expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenCalledWith(['init', { key: 'pk_123' }]);
expect(handler).toHaveBeenCalledWith(['identify', 'user-1']);
expect(handler).toHaveBeenCalledWith(['page', { url: '/home' }]);
});

it('replays queued commands from the stub array', () => {
Expand Down Expand Up @@ -51,9 +51,9 @@ describe('createLoader', () => {
expect(handler).toHaveBeenCalledTimes(1);

// New command via push
loader.push(['identify', 'user-2']);
loader.push(['page', { url: '/about' }]);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(['identify', 'user-2']);
expect(handler).toHaveBeenLastCalledWith(['page', { url: '/about' }]);
});

it('handles empty window.__imtbl gracefully', () => {
Expand Down
56 changes: 0 additions & 56 deletions packages/audience/pixel/src/pixel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,62 +230,6 @@ describe('Pixel', () => {
});
});

describe('identify', () => {
it('enqueues identify message with identityType at full consent', () => {
mockGetOrCreateSession.mockReturnValue({ sessionId: 'session-abc', isNew: false });

const pixel = new Pixel();
activePixel = pixel;
pixel.init({ key: 'pk_test', environment: 'dev', consent: 'full' });
mockEnqueue.mockClear();

pixel.identify('user-1', 'passport', { email: 'test@example.com' });

expect(mockEnqueue).toHaveBeenCalledWith(
expect.objectContaining({
type: 'identify',
userId: 'user-1',
identityType: 'passport',
surface: 'pixel',
traits: expect.objectContaining({
email: 'test@example.com',
sessionId: 'session-abc',
}),
}),
);
});

it('enqueues identify message without traits', () => {
mockGetOrCreateSession.mockReturnValue({ sessionId: 'session-abc', isNew: false });

const pixel = new Pixel();
activePixel = pixel;
pixel.init({ key: 'pk_test', environment: 'dev', consent: 'full' });
mockEnqueue.mockClear();

pixel.identify('steam-id-123', 'steam');

expect(mockEnqueue).toHaveBeenCalledWith(
expect.objectContaining({
type: 'identify',
userId: 'steam-id-123',
identityType: 'steam',
}),
);
});

it('does not enqueue identify at anonymous consent', () => {
const pixel = new Pixel();
activePixel = pixel;
pixel.init({ key: 'pk_test', environment: 'dev', consent: 'anonymous' });

pixel.identify('user-1', 'passport');
// Only the auto page view + session_start, no identify
const calls = mockEnqueue.mock.calls.map((c: unknown[]) => (c[0] as Record<string, unknown>));
expect(calls.find((c) => c.type === 'identify')).toBeUndefined();
});
});

describe('session_end', () => {
it('fires session_end on pagehide when session is active', () => {
const pixel = new Pixel();
Expand Down
34 changes: 4 additions & 30 deletions packages/audience/pixel/src/pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import type {
ConsentLevel,
PageMessage,
TrackMessage,
IdentifyMessage,
UserTraits,
ConsentManager,
CmpDetector,
IdentityType,
} from '@imtbl/audience-core';
import {
MessageQueue,
Expand Down Expand Up @@ -53,8 +50,6 @@ export class Pixel {

private anonymousId = '';

private userId: string | undefined;

private sessionId: string | undefined;

private sessionStartTime: number | undefined;
Expand Down Expand Up @@ -160,28 +155,7 @@ export class Pixel {
sessionId,
...properties,
},
userId: this.consent!.level === 'full' ? this.userId : undefined,
};

this.queue!.enqueue(message);
}

identify(userId: string, identityType: IdentityType, traits?: UserTraits): void {
if (!this.isReady() || this.consent!.level !== 'full') return;

this.userId = userId;
const { sessionId, isNew } = getOrCreateSession(this.domain);
this.refreshSession(sessionId, isNew);

const message: IdentifyMessage = {
...this.buildBase(),
type: 'identify',
userId,
identityType,
traits: {
...traits,
sessionId,
} as UserTraits,
userId: undefined,
};

this.queue!.enqueue(message);
Expand Down Expand Up @@ -256,7 +230,7 @@ export class Pixel {
type: 'track',
eventName,
properties: { ...properties, sessionId },
userId: this.consent!.level === 'full' ? this.userId : undefined,
userId: undefined,
};

this.queue!.enqueue(message);
Expand All @@ -280,7 +254,7 @@ export class Pixel {
type: 'track',
eventName: 'session_start',
properties: { sessionId },
userId: this.consent!.level === 'full' ? this.userId : undefined,
userId: undefined,
};

this.queue!.enqueue(message);
Expand All @@ -301,7 +275,7 @@ export class Pixel {
sessionId: this.sessionId,
duration,
},
userId: this.consent!.level === 'full' ? this.userId : undefined,
userId: undefined,
};

this.queue!.enqueue(message);
Expand Down
Loading