refactor: events tracker to nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-01-08 17:26:11 +02:00
parent 6f870ea1e1
commit 3bf5f4be86
4 changed files with 32 additions and 18 deletions

View File

@@ -63,8 +63,8 @@ import { BankingTransactionsModule } from '../BankingTransactions/BankingTransac
import { TransactionsLockingModule } from '../TransactionsLocking/TransactionsLocking.module'; import { TransactionsLockingModule } from '../TransactionsLocking/TransactionsLocking.module';
import { SettingsModule } from '../Settings/Settings.module'; import { SettingsModule } from '../Settings/Settings.module';
import { InventoryAdjustmentsModule } from '../InventoryAdjutments/InventoryAdjustments.module'; import { InventoryAdjustmentsModule } from '../InventoryAdjutments/InventoryAdjustments.module';
import { EventTrackerModule } from '../EventsTracker/EventTracker.module';
import { PostHogModule } from '../EventsTracker/postHog.module'; import { PostHogModule } from '../EventsTracker/postHog.module';
import { EventTrackerModule } from '../EventsTracker/EventTracker.module';
@Module({ @Module({
imports: [ imports: [
@@ -152,8 +152,8 @@ import { PostHogModule } from '../EventsTracker/postHog.module';
TransactionsLockingModule, TransactionsLockingModule,
SettingsModule, SettingsModule,
InventoryAdjustmentsModule, InventoryAdjustmentsModule,
// PostHogModule, PostHogModule,
// EventTrackerModule, EventTrackerModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [ providers: [

View File

@@ -1,23 +1,33 @@
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
// import { PostHog, EventMessage } from 'posthog-node';
import { POSTHOG } from './postHog.module';
import { TenancyContext } from '../Tenancy/TenancyContext.service'; import { TenancyContext } from '../Tenancy/TenancyContext.service';
import { POSTHOG_PROVIDER } from './PostHog.constants';
interface EventMessage { export interface IdentifyMessage {
distinctId distinctId: string;
properties?: Record<string | number, any>;
disableGeoip?: boolean;
} }
export interface EventMessage extends IdentifyMessage {
event: string;
groups?: Record<string, string | number>; // Mapping of group type to group id
sendFeatureFlags?: boolean;
timestamp?: Date;
uuid?: string;
}
@Injectable() @Injectable()
export class EventTrackerService { export class EventTrackerService {
constructor( constructor(
@Inject(POSTHOG) private readonly posthog: any,
private readonly tenancyContext: TenancyContext, private readonly tenancyContext: TenancyContext,
@Inject(POSTHOG_PROVIDER) private readonly posthog: any,
) {} ) {}
/** /**
* Track tenant an event. * Track tenant an event.
* @param event - The event to track. * @param event - The event to track.
*/ */
public async trackEvent(event: any) { public async trackEvent(event: Omit<EventMessage, 'distinctId'>) {
// Cannot continue if the Posthog not configured. // Cannot continue if the Posthog not configured.
if (!this.posthog) return; if (!this.posthog) return;

View File

@@ -0,0 +1 @@
export const POSTHOG_PROVIDER = 'POSTHOG_PROVIDER';

View File

@@ -1,24 +1,27 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
// import { PostHog } from 'posthog-node'; import { PostHog } from 'posthog-node';
import { EventTrackerService } from './EventTracker.service'; import { EventTrackerService } from './EventTracker.service';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { POSTHOG_PROVIDER } from './PostHog.constants';
export const POSTHOG = 'PostHog'; import { TenancyContext } from '../Tenancy/TenancyContext.service';
@Module({ @Module({
providers: [ providers: [
EventTrackerService, EventTrackerService,
TenancyContext,
{ {
provide: POSTHOG, provide: POSTHOG_PROVIDER,
useFactory: (configService: ConfigService) => { useFactory: (configService: ConfigService) => {
if (configService.get('posthog.apiKey')) {
return new PostHog(configService.get('posthog.apiKey'), {
host: configService.get('posthog.host'),
});
}
return null;
}, },
// new PostHog(configService.get('posthog.apiKey'), {
// host: configService.get('posthog.host'),
// }),
inject: [ConfigService], inject: [ConfigService],
}, },
], ],
exports: [EventTrackerService], exports: [EventTrackerService, POSTHOG_PROVIDER],
}) })
export class PostHogModule {} export class PostHogModule {}