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

feat: add backend proxy for segment #5474

Merged
merged 5 commits into from
May 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 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 @@ -90,6 +91,7 @@ const baseModules: Array<Type | DynamicModule | Promise<DynamicModule> | Forward
RateLimitingModule,
ProfilingModule.register(packageJson.name),
TracingModule.register(packageJson.name, packageJson.version),
AnalyticsModule,
];

const enterpriseModules = enterpriseImports();
Expand Down
18 changes: 18 additions & 0 deletions apps/api/src/app/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Body, Controller, Post } from '@nestjs/common';
import { AnalyticsService } from '@novu/application-generic';

@Controller({
path: 'analytics',
davidsoderberg marked this conversation as resolved.
Show resolved Hide resolved
})
export class AnalyticsController {
davidsoderberg marked this conversation as resolved.
Show resolved Hide resolved
constructor(private analyticsService: AnalyticsService) {}

@Post('/telemetry')
davidsoderberg marked this conversation as resolved.
Show resolved Hide resolved
davidsoderberg marked this conversation as resolved.
Show resolved Hide resolved
async trackEvent(@Body('event') event, @Body('data') data, @Body('userId') userId): Promise<any> {
await this.analyticsService.track(event, data, userId);

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

@Module({
imports: [SharedModule],
controllers: [AnalyticsController],
providers: [AnalyticsService],
})
export class AnalyticsModule {}
11 changes: 9 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,18 @@ 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);
const user = await this._segment.user();

await api.post('/v1/analytics/telemetry', {
event: event + ' - [WEB]',
userId: user.id(),
data,
});
}

pageView(url: string) {
Expand Down