1.1.0 • Published 5 months ago

@redredgroup/nestjs-mixpanel v1.1.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

Nestjs Mixpanel Actions Status Node Version NPM version

Introduction

This package is a module that converts the asynchronous Mixpanel to Nestjs.

Installation

using npm

npm install @redredgroup/nestjs-mixpanel

using yarn

yarn install @redredgroup/nestjs-mixpanel

using pnpm

pnpm add @redredgroup/nestjs-mixpanel

Import module

import { Module } from '@nestjs/common';
import { MixpanelModule } from '@redredgroup/nestjs-mixpanel';

@Module({
  imports: [
    MixpanelModule.forRoot({
      mixpanelOptions: {
        projectToken: 'YOUR_MIXPANEL_PROJECT_TOKEN',
      },
    }),
  ],
})
export class AppModule {}

//Or the forRootAsync module using @nestjs/Config

import { Module } from '@nestjs/common';
import { MixpanelModule } from '@redredgroup/nestjs-mixpanel';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    MixpanelModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        mixpanelOptions: {
          projectToken: configService.get('YOUR_MIXPANEL_PROJECT_TOKEN'),
        },
      }),
      inject: [ConfigModule],
    }),
  ],
})
export class AppModule {}

Example

Track Event

import { Injectable } from '@nestjs/common';
import { MixpanelService } from '@redredgroup/nestjs-mixpanel';

@Injectable()
export class AppService {
  constructor(private readonly mixpanelService: MixpanelService) {}
  eventTrack(): string {
    const USER_ID = 'USER1';

    this.mixpanelService.event.track({
      eventName: 'CREATE_USER',
      distinctId: USER_ID,
      properties: {
        is_happy: true,
      },
    });

    return 'ok';
  }
}

People Increment

import { Injectable } from '@nestjs/common';
import { MixpanelService } from '@redredgroup/nestjs-mixpanel';

@Injectable()
export class AppService {
  constructor(private readonly mixpanelService: MixpanelService) {}
  peopleIncrementAdd(): string {
    const USER_ID = 'USER1';

    this.mixpanelService.people.set({
      distinctId: USER_ID,
      properties: {
        happy_count: 1,
      },
    });

    return 'ok';
  }
}

Many Data Import

Alternatively, you can process a lot of data at once with Array values. For example:

import { Injectable } from '@nestjs/common';
import { MixpanelService, TrackParams } from '@redredgroup/nestjs-mixpanel';

@Injectable()
export class AppService {
  constructor(private readonly mixpanelService: MixpanelService) {}

  eventTrackMany(): string {
    const USERS: TrackParams[] = [
      {
        eventName: 'CREATE_USER',
        distinctId: 'USER_1',
        properties: {
          is_happy: true,
        },
      },
      {
        eventName: 'CREATE_USER',
        distinctId: 'USER_2',
        properties: {
          is_happy: false,
        },
      },
    ];

    await this.mixpanelService.event.trackMany(USERS);

    return 'ok';
  }
}

In common, all methods can inject data from an Array of values by appending Many after the role.

Copyright

© 2023 REDREDGROUP Software. All Right Reserved.

License

Apache-2.0

1.1.0

5 months ago

1.0.0

5 months ago