<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mesonrale Ope </title>
    <description>The latest articles on DEV Community by Mesonrale Ope  (@seunope).</description>
    <link>https://dev.to/seunope</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1248021%2Fd6d6607a-edf4-4b95-b2d7-28ce0c16e2ef.jpeg</url>
      <title>DEV Community: Mesonrale Ope </title>
      <link>https://dev.to/seunope</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/seunope"/>
    <language>en</language>
    <item>
      <title>How to Integrate Mixpanel with NestJS</title>
      <dc:creator>Mesonrale Ope </dc:creator>
      <pubDate>Fri, 02 Feb 2024 15:47:37 +0000</pubDate>
      <link>https://dev.to/seunope/how-to-integrate-mixpanel-with-nestjs-3i76</link>
      <guid>https://dev.to/seunope/how-to-integrate-mixpanel-with-nestjs-3i76</guid>
      <description>&lt;p&gt;In NestJs application you can integrate Mixpanel with these simple steps:&lt;/p&gt;

&lt;p&gt;Install the Mixpanel library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add  mixpanel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a service for Mixpanel by running the following command. The service will host all Mixpanel features like init, track, people etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest generate service mixpanel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following code to the &lt;code&gt;mixPanel.service&lt;/code&gt; that was just created.  You can setup the ConfigService or just pass your Mixpanel project token to the init() method as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as Mixpanel from 'mixpanel';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class MixpanelService {
  private mixpanel: any;

  constructor(private configService: ConfigService) {
    this.mixpanel = Mixpanel.init(
       "mixPanel Project Token", //this.configService.get&amp;lt;string&amp;gt;('mixPanelToken'),
      {
        debug: true,
        // protocol: 'http',
      },
    );
  }

  public track(eventName: string, action: any = {}): void {
    this.mixpanel.track(eventName, action);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To track an event in a module, add the mixPanel service to the module constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  constructor(
    ....
    private readonly mixpanelService: MixpanelService,
  ) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example track:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    this.mixpanelService.track('Sign Up', {
        $first_name: 'John,
        $last_name: 'Doe,
        $created: new Date().toISOString(),
        type: 'patient',
        distinct_id: 'userId',
        //ip: ip.address(),
      });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is all when integrating Mixpanel with your NestJS app using a dedicated Mixpanel service. You can find more information on Mixpanel's documentation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FULL CODE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;app.module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { MixpanelService } from './mixpanel/mixpanel.service';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: [
        '.env.production',
        '.env.development',
        '.env',
      ],
      // load: [configuration],  //for env variables
      isGlobal: true,
    }),
  ],
  controllers: [AppController],
  providers: [AppService, MixpanelService],
})
export class AppModule {}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;mixpanel.service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as Mixpanel from 'mixpanel';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class MixpanelService {
  private mixpanel: any;

  constructor(private configService: ConfigService) {
    this.mixpanel = Mixpanel.init(
      'mixPanel Project Token', //this.configService.get&amp;lt;string&amp;gt;('mixPanelToken'),
      {
        debug: true,
        protocol: 'http',
      },
    );
  }

  public track(eventName: string, action: any = {}): void {
    this.mixpanel.track(eventName, action);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;app.service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as ip from 'ip';
import { MixpanelService } from './mixpanel/mixpanel.service';
import { ForbiddenException, Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  constructor(private readonly mixpanelService: MixpanelService) {}
  async signup(dto: any) {
    try {
      this.mixpanelService.track('Sign Up', {
        $first_name: dto.firstName,
        $last_name: dto.lastName,
        $created: new Date().toISOString(),
        type: 'patient',
        distinct_id: dto.id,
        ip: ip.address(),
      });
      return {
        status: true,
        message: 'User has been created successfully',
        data: dto,
      };
    } catch (err) {
      throw new ForbiddenException({
        status: false,
        message: err.message || 'Error: user not created!',
        error: 'Bad Request',
      });
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is all folks!&lt;br&gt;
Reference : &lt;a href="https://dev.to/muzeyr/to-integrate-mixpanel-with-nestjs-1bhl"&gt;Uzeyr OZ&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>mixpanel</category>
      <category>node</category>
    </item>
  </channel>
</rss>
