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: 2 additions & 2 deletions packages/audience/pixel/src/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ describe('bootstrap', () => {
(window as Record<string, unknown>).__imtbl = [
['init', { key: 'pk_test' }],
['consent', 'full'],
['identify', 'user-1', { email: 'a@b.com' }],
['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', { email: 'a@b.com' });
expect(mockIdentify).toHaveBeenCalledWith('user-1', 'passport', { email: 'a@b.com' });
});

it('installs loader and handles new commands after load', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/audience/pixel/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function handleCommand(command: Command): void {
pixel.identify(
args[0] as string,
args[1] as Parameters<Pixel['identify']>[1],
args[2] as Parameters<Pixel['identify']>[2],
);
break;
case 'consent':
Expand Down
26 changes: 23 additions & 3 deletions packages/audience/pixel/src/pixel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,21 @@ describe('Pixel', () => {
});

describe('identify', () => {
it('enqueues identify message at full consent', () => {
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', { email: 'test@example.com' });
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',
Expand All @@ -242,12 +243,31 @@ describe('Pixel', () => {
);
});

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');
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();
Expand Down
4 changes: 3 additions & 1 deletion packages/audience/pixel/src/pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IdentifyMessage,
UserTraits,
ConsentManager,
IdentityType,
} from '@imtbl/audience-core';
import {
MessageQueue,
Expand Down Expand Up @@ -147,7 +148,7 @@ export class Pixel {
this.queue!.enqueue(message);
}

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

this.userId = userId;
Expand All @@ -158,6 +159,7 @@ export class Pixel {
...this.buildBase(),
type: 'identify',
userId,
identityType,
traits: {
...traits,
sessionId,
Expand Down
Loading