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 { SettingsModule } from '../Settings/Settings.module';
import { InventoryAdjustmentsModule } from '../InventoryAdjutments/InventoryAdjustments.module';
import { EventTrackerModule } from '../EventsTracker/EventTracker.module';
import { PostHogModule } from '../EventsTracker/postHog.module';
import { EventTrackerModule } from '../EventsTracker/EventTracker.module';
@Module({
imports: [
@@ -152,8 +152,8 @@ import { PostHogModule } from '../EventsTracker/postHog.module';
TransactionsLockingModule,
SettingsModule,
InventoryAdjustmentsModule,
// PostHogModule,
// EventTrackerModule,
PostHogModule,
EventTrackerModule,
],
controllers: [AppController],
providers: [

View File

@@ -1,23 +1,33 @@
import { Inject, Injectable } from '@nestjs/common';
// import { PostHog, EventMessage } from 'posthog-node';
import { POSTHOG } from './postHog.module';
import { TenancyContext } from '../Tenancy/TenancyContext.service';
import { POSTHOG_PROVIDER } from './PostHog.constants';
interface EventMessage {
distinctId
export interface IdentifyMessage {
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()
export class EventTrackerService {
constructor(
@Inject(POSTHOG) private readonly posthog: any,
private readonly tenancyContext: TenancyContext,
@Inject(POSTHOG_PROVIDER) private readonly posthog: any,
) {}
/**
* Track tenant an event.
* @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.
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 { PostHog } from 'posthog-node';
import { PostHog } from 'posthog-node';
import { EventTrackerService } from './EventTracker.service';
import { ConfigService } from '@nestjs/config';
export const POSTHOG = 'PostHog';
import { POSTHOG_PROVIDER } from './PostHog.constants';
import { TenancyContext } from '../Tenancy/TenancyContext.service';
@Module({
providers: [
EventTrackerService,
TenancyContext,
{
provide: POSTHOG,
provide: POSTHOG_PROVIDER,
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],
},
],
exports: [EventTrackerService],
exports: [EventTrackerService, POSTHOG_PROVIDER],
})
export class PostHogModule {}