Skip to content

Commit

Permalink
feat: add backend proxy for segment (#5474)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsoderberg authored May 8, 2024
1 parent ac194ef commit 8de8c20
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ApiRateLimitInterceptor } from './app/rate-limiting/guards';
import { RateLimitingModule } from './app/rate-limiting/rate-limiting.module';
import { ProductFeatureInterceptor } from './app/shared/interceptors/product-feature.interceptor';
import { ResourceThrottlerInterceptor } from './app/resource-limiting/guards';
import { AnalyticsModule } from './app/analytics/analytics.module';

const enterpriseImports = (): Array<Type | DynamicModule | Promise<DynamicModule> | ForwardReference> => {
const modules: Array<Type | DynamicModule | Promise<DynamicModule> | ForwardReference> = [];
Expand Down Expand Up @@ -132,6 +133,10 @@ if (process.env.SENTRY_DSN) {
});
}

if (process.env.SEGMENT_TOKEN) {
modules.push(AnalyticsModule);
}

if (process.env.NODE_ENV === 'test') {
modules.push(TestingModule);
}
Expand Down
22 changes: 22 additions & 0 deletions apps/api/src/app/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Body, Controller, Logger, Post, UseGuards } from '@nestjs/common';
import { SkipThrottle } from '@nestjs/throttler';
import { AnalyticsService, UserAuthGuard, UserSession } from '@novu/application-generic';
import { IJwtPayload } from '@novu/shared';

@Controller({
path: 'telemetry',
})
@SkipThrottle()
export class AnalyticsController {
constructor(private analyticsService: AnalyticsService) {}

@Post('/measure')
@UseGuards(UserAuthGuard)
async trackEvent(@Body('event') event, @Body('data') data, @UserSession() user: IJwtPayload): Promise<any> {
await this.analyticsService.track(event, user._id, data);

return {
success: true,
};
}
}
12 changes: 12 additions & 0 deletions apps/api/src/app/analytics/analytics.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { AnalyticsController } from './analytics.controller';
import { SharedModule } from '../shared/shared.module';
import { AnalyticsService } from '@novu/application-generic';
import { AuthModule } from '../auth/auth.module';

@Module({
imports: [SharedModule, AuthModule],
controllers: [AnalyticsController],
providers: [AnalyticsService],
})
export class AnalyticsModule {}
8 changes: 6 additions & 2 deletions libs/shared-web/src/utils/segment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AnalyticsBrowser } from '@segment/analytics-next';
import { IUserEntity } from '@novu/shared';
import { api } from '../api';

export class SegmentService {
private _segment: AnalyticsBrowser | null = null;
Expand All @@ -21,12 +22,15 @@ export class SegmentService {
this._segment?.identify(user?._id);
}

track(event: string, data?: Record<string, unknown>) {
async track(event: string, data?: Record<string, unknown>) {
if (!this.isSegmentEnabled()) {
return;
}

this._segment?.track(event + ' - [WEB]', data);
await api.post('/v1/telemetry/measure', {
event: event + ' - [WEB]',
data,
});
}

pageView(url: string) {
Expand Down

0 comments on commit 8de8c20

Please sign in to comment.