Skip to content

An addon/plugin package to provide Mixpanel data collection services in AdonisJS 4.0+

License

Notifications You must be signed in to change notification settings

stitchng/adonis-mixpanel

Repository files navigation

adonis-mixpanel

An addon/plugin package to provide Mixpanel data collection and tracking services in AdonisJS 4.0+

NPM Version Build Status Coveralls

Getting Started

Install from the NPM Registry - (Please read the instructions.md file to learn how to setup this package properly).

   $ adonis install adonisjs-mixpanel

Usage

Import and use in controllers or the standard AdonisJS Event Bus

const mixpanel = use('MixPanel')
const User = use('App/Models/User')

class UserController {

    async fetch({ request, response }){

        let allUsers = await User.all()

        return response.status(200).json({
            users:allUsers
        });
    }

    async register({ request, response }) {

        let user_details = request.only([
            'dob',
            'email',
            'phone',
            'first_name',
            'last_name'
        ])

        let newUser = await User.create(user_details)

        // track a new user registered to the web OR mobile app
        mixpanel.trackUserBasicAttributes(
            newUser.toJSON()
        );

        return response.status(201).json({
            message:'User Created!',
            user: newUSer.toJSON()
        });
    }
}

module.exports = UserController

More examples

const mixpanel = use('MixPanel')
const User = use('App/Models/User')

class BillingController {

    async payment({ request, response }){

        let user = await User.findBy('id', 1)

        // track the charge made on a user for using the web OR mobile app
        // which ties to revenue from the user for the app
        mixpanel.trackUserBillingCharge(
            user,
            40000 // Naira
        )
    }
}

module.exports = BillingController

This library can also be used to track events using the mixtrack named middleware.

const Route = use('Route')

Route.group(() => {
  Route.get('/all', 'UserController.fetch')
  Route.put('/update/email', function({ auth, request, response }) {
    const user = await auth.getUser()
    user.merge({
      email: request.input('email', null)
    })

    await user.save() // send to database
    await user.reload() // refresh with new email

    request.user = user.toJSON()
    return response.status(200).send('User Updated Email!')
  }).middleware (
    ['auth', 'mixtrack:set;email']
  ) // track updated data for user via 'email'
})
.prefix('user')

License

MIT

Running Tests

    npm i
    npm run lint

    npm run test

Credits

Contributing

See the CONTRIBUTING.md file for info

Support

Coolcodes is a non-profit software foundation (collective) created by Oparand - parent company of StitchNG, Synergixe based in Abuja, Nigeria. You'll find an overview of all our work and supported open source projects on our Facebook Page.

Follow us on facebook if you can to get the latest open source software/freeware news and infomation.

Does your business depend on our open projects? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.