Skip to content

Commit

Permalink
feat: ignore onboarding events for existing customers (#8064)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Sep 3, 2024
1 parent e70b09a commit 3d63089
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
15 changes: 15 additions & 0 deletions src/lib/features/onboarding/onboarding-service.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ test('Default project should take first user created instead of project created
]);
});

test('Ignore events for existing customers', async () => {
jest.useFakeTimers();
jest.setSystemTime(new Date(2024, 8, 2)); // day before we added metrics
const { userStore } = stores;
await userStore.insert({});

jest.setSystemTime(new Date());
await onboardingService.insert({ type: 'first-user-login' });

const { rows: instanceEvents } = await db.rawDatabase.raw(
'SELECT * FROM onboarding_events_instance',
);
expect(instanceEvents).toMatchObject([]);
});

test('Ignore system user in onboarding events', async () => {
// system users are not counted towards onboarding metrics
await db.rawDatabase.raw('INSERT INTO users (is_system) VALUES (true)');
Expand Down
41 changes: 28 additions & 13 deletions src/lib/features/onboarding/onboarding-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import type {
IOnboardingStore,
ProjectEvent,
} from './onboarding-store-type';
import { millisecondsToSeconds } from 'date-fns';
import { isBefore, millisecondsToSeconds } from 'date-fns';

const START_ONBOARDING_TRACKING_DATE = new Date(2024, 8, 3);

export class OnboardingService {
private flagResolver: IFlagResolver;
Expand Down Expand Up @@ -92,18 +94,28 @@ export class OnboardingService {
| { flag: string; type: ProjectEvent['type'] }
| { type: 'first-user-login' | 'second-user-login' },
): Promise<void> {
await this.insertInstanceEvent(event);
const firstInstanceUserDate = await this.userStore.getFirstUserDate();
// the time we introduced onboarding tracking
if (
firstInstanceUserDate &&
isBefore(firstInstanceUserDate, START_ONBOARDING_TRACKING_DATE)
)
return;

await this.insertInstanceEvent(event, firstInstanceUserDate);
if ('flag' in event) {
await this.insertProjectEvent(event);
await this.insertProjectEvent(event, firstInstanceUserDate);
}
this.eventBus.emit('onboarding-event');
}

private async insertInstanceEvent(event: {
flag?: string;
type: InstanceEvent['type'];
}): Promise<void> {
const firstInstanceUserDate = await this.userStore.getFirstUserDate();
private async insertInstanceEvent(
event: {
flag?: string;
type: InstanceEvent['type'];
},
firstInstanceUserDate: Date | null,
): Promise<void> {
if (!firstInstanceUserDate) return;

const timeToEvent = millisecondsToSeconds(
Expand All @@ -115,18 +127,21 @@ export class OnboardingService {
});
}

private async insertProjectEvent(event: {
flag: string;
type: ProjectEvent['type'];
}): Promise<void> {
private async insertProjectEvent(
event: {
flag: string;
type: ProjectEvent['type'];
},
firstInstanceUserDate: Date | null,
): Promise<void> {
const project = await this.projectReadModel.getFeatureProject(
event.flag,
);
if (!project) return;

const startDate =
project.project === 'default'
? await this.userStore.getFirstUserDate()
? firstInstanceUserDate
: project.createdAt || null;

if (!startDate) return;
Expand Down

0 comments on commit 3d63089

Please sign in to comment.