import { Body, Controller, Inject, Param, Post } from '@nestjs/common'; import { init } from 'mixpanel'; import { Authenticated } from 'src/authentication/authentication.decorator'; import { RequestUser, User } from 'src/authentication/user.decorator'; @Authenticated() @Controller('trackEvent') export class MixpanelController { constructor( @Inject('MIXPANEL_TOKEN') private readonly mixpanelToken: string, ) {} @Post(':id') async trackEvent(@Param('id') id, @Body() body, @User() user: RequestUser) { delete body.info; const mixpanel = init(this.mixpanelToken); await mixpanel.people.set(user.username, { $name: user.username, $email: user.username, customer_name: user.customer_name, }); await mixpanel.track(id, { distinct_id: user.username, customer: user.customer_name, env: process.env.ENV, ...body, }); return { id, body, user: user.username }; } }