Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split userInfo/context stamping from raw event data #876

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@ coverage/

# Typescript
tsconfig.tsbuildinfo

# Library Info Auto Generated file
packages/core/src/info.ts
117 changes: 117 additions & 0 deletions packages/core/src/__tests__/methods/process.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { SegmentClient } from '../../analytics';
import { EventType, SegmentEvent } from '../../types';

import { getMockLogger } from '../__helpers__/mockLogger';
import { MockSegmentStore } from '../__helpers__/mockSegmentStore';

jest.mock('uuid');

jest
.spyOn(Date.prototype, 'toISOString')
.mockReturnValue('2010-01-01T00:00:00.000Z');

describe('process', () => {
const store = new MockSegmentStore({
userInfo: {
userId: 'current-user-id',
anonymousId: 'very-anonymous',
},
context: {
library: {
name: 'test',
version: '1.0',
},
},
});

const clientArgs = {
config: {
writeKey: 'mock-write-key',
flushInterval: 0,
},
logger: getMockLogger(),
store: store,
};

beforeEach(() => {
jest.clearAllMocks();
});

it('stamps basic data: timestamp and messageId for events when not ready', async () => {
const client = new SegmentClient(clientArgs);
jest.spyOn(client.isReady, 'value', 'get').mockReturnValue(false);
// @ts-ignore
const timeline = client.timeline;
jest.spyOn(timeline, 'process');

await client.track('Some Event', { id: 1 });

let expectedEvent: Record<string, unknown> = {
event: 'Some Event',
properties: {
id: 1,
},
type: EventType.TrackEvent,
};

// While not ready only timestamp and messageId should be defined
// @ts-ignore
const pendingEvents = client.pendingEvents;
expect(pendingEvents.length).toBe(1);
const pendingEvent = pendingEvents[0];
expect(pendingEvent).toMatchObject(expectedEvent);
expect(pendingEvent.messageId).not.toBeUndefined();
expect(pendingEvent.timestamp).not.toBeUndefined();

// Not yet processed
expect(timeline.process).not.toHaveBeenCalled();

// When ready it replays events
jest.spyOn(client.isReady, 'value', 'get').mockReturnValue(true);
// @ts-ignore
await client.onReady();
expectedEvent = {
...expectedEvent,
context: { ...store.context.get() },
userId: store.userInfo.get().userId,
anonymousId: store.userInfo.get().anonymousId,
};

// @ts-ignore
expect(client.pendingEvents.length).toBe(0);

expect(timeline.process).toHaveBeenCalledWith(
expect.objectContaining(expectedEvent)
);
});

it('stamps all context and userInfo data for events when ready', async () => {
const client = new SegmentClient(clientArgs);
jest.spyOn(client.isReady, 'value', 'get').mockReturnValue(true);

// @ts-ignore
const timeline = client.timeline;
jest.spyOn(timeline, 'process');

await client.track('Some Event', { id: 1 });

const expectedEvent = {
event: 'Some Event',
properties: {
id: 1,
},
type: EventType.TrackEvent,
context: { ...store.context.get() },
userId: store.userInfo.get().userId,
anonymousId: store.userInfo.get().anonymousId,
} as SegmentEvent;

// @ts-ignore
const pendingEvents = client.pendingEvents;
expect(pendingEvents.length).toBe(0);

expect(timeline.process).toHaveBeenCalledWith(
expect.objectContaining(expectedEvent)
);
});
});
46 changes: 35 additions & 11 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class SegmentClient {
this.trackDeepLinks(),
]);

this.onReady();
await this.onReady();
this.isReady.value = true;

// flush any stored events
Expand Down Expand Up @@ -413,17 +413,29 @@ export class SegmentClient {
}

async process(incomingEvent: SegmentEvent) {
const event = await this.applyRawEventData(incomingEvent);
const event = this.applyRawEventData(incomingEvent);

if (this.isReady.value) {
this.flushPolicyExecuter.notify(event);
return this.timeline.process(event);
return this.startProcess(event);
} else {
this.pendingEvents.push(event);
return event;
}
}

/**
* Starts timeline processing
* @param incomingEvent Segment Event
* @returns Segment Event
*/
private async startProcess(
incomingEvent: SegmentEvent
): Promise<SegmentEvent | undefined> {
const event = await this.applyContextData(incomingEvent);
this.flushPolicyExecuter.notify(event);
return this.timeline.process(event);
}

private async trackDeepLinks() {
if (this.getConfig().trackDeepLinks === true) {
const deepLinkProperties = await this.store.deepLinkData.get(true);
Expand Down Expand Up @@ -453,7 +465,7 @@ export class SegmentClient {
* Executes when everything in the client is ready for sending events
* @param isReady
*/
private onReady() {
private async onReady() {
// Add all plugins awaiting store
if (this.pluginsToAdd.length > 0 && !this.isAddingPlugins) {
this.isAddingPlugins = true;
Expand All @@ -473,7 +485,7 @@ export class SegmentClient {

// Send all events in the queue
for (const e of this.pendingEvents) {
void this.timeline.process(e);
await this.startProcess(e);
}
this.pendingEvents = [];
}
Expand Down Expand Up @@ -775,12 +787,27 @@ export class SegmentClient {
}

/**
* Injects context and userInfo data into the event, sets the messageId and timestamp
* Sets the messageId and timestamp
* @param event Segment Event
* @returns event with data injected
*/
private applyRawEventData = (event: SegmentEvent): SegmentEvent => {
return {
...event,
messageId: getUUID(),
timestamp: new Date().toISOString(),
integrations: event.integrations ?? {},
} as SegmentEvent;
};

/**
* Injects context and userInfo data into the event
* This is handled outside of the timeline to prevent concurrency issues between plugins
* This is only added after the client is ready to let the client restore values from storage
* @param event Segment Event
* @returns event with data injected
*/
private applyRawEventData = async (
private applyContextData = async (
event: SegmentEvent
): Promise<SegmentEvent> => {
const userInfo = await this.processUserInfo(event);
Expand All @@ -793,9 +820,6 @@ export class SegmentClient {
...event.context,
...context,
},
messageId: getUUID(),
timestamp: new Date().toISOString(),
integrations: event.integrations ?? {},
} as SegmentEvent;
};

Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/info.ts

This file was deleted.