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

Events not firing #175

Closed
hansy opened this issue Sep 7, 2020 · 5 comments
Closed

Events not firing #175

hansy opened this issue Sep 7, 2020 · 5 comments

Comments

@hansy
Copy link

hansy commented Sep 7, 2020

I'm not sure why, but no events are being sent via the track function.

import Mixpanel from 'mixpanel'

const mixpanel = Mixpanel.init(process.env.MIXPANEL_TOKEN)
mixpanel.track('testEvent', { distinct_id: "abc123" })

I don't even know how to debug this. I even tried adding a callback argument to look for an error, but nothing:

mixpanel.track('testEvent', { distinct_id: "abc123" }, (err) => {
  console.log(err) // no output
  console.log('Something') // no output
})

What am I doing wrong?

System info:
Mixpanel v0.13.0
Node v14.5.0
MacOS Catalina 10.15.6

@hansy hansy closed this as completed Sep 7, 2020
@hansy
Copy link
Author

hansy commented Sep 7, 2020

Since I was using this in an async serverless function I needed to wait for the Mixpanel request to complete. I ended up turning track into a Promise via promisify to await the Mixpanel response.

@abierbaum
Copy link

@hansy Thanks for this tip. Saved me a ton of time. I really miss having promises in this API.

@steviec
Copy link

steviec commented Mar 15, 2023

It's kinda ridiculous. The library just doesn't work in AWS lambda at all unless you wrap it since the callback doesn't have time to fire before the lambda ends.

@walterholohan
Copy link

If anyone wants an example of how to create a promise wrapper here is one.

/**
 * Due to the fact that the mixpanel library does not support promises
 * we have to wrap the track function in a promise. Otherwise the
 * lambda function will finish before the request has been sent.
 * @param event - The event to track
 * @param properties - The properties to track with the event
 * @param mixpanel - The mixpanel instance
 * @param userId - The user id
 * @returns A promise that resolves when the track request has been sent
 */
async function mixpanelAsyncTrack({
  event,
  properties,
  mixpanel,
  userId,
}: {
  event: string
  userId: string
  properties?: { [key: string]: string | number | boolean | undefined }
  mixpanel: Mixpanel.Mixpanel
}) {
  return new Promise((resolve, reject) => {
    mixpanel.track(event, { distinct_id: userId, properties }, (err) => {
      if (err) {
        reject(err)
      } else {
        resolve('success')
      }
    })
  })
}

@Collin0810
Copy link

@walterholohan Thanks! Have been struggling for hours to set up Mixpanel event tracking in a NextJS API route (which gets turned into a serverless lambda function when deploying to Vercel into production). This works great!

pmackle added a commit to mixpanel/docs that referenced this issue May 14, 2024
Clarifying that when you are tracking node in a serverless implementation, you need to make adjustments in Node.

Relevant github issue: mixpanel/mixpanel-node#175
saminmadani added a commit to mixpanel/docs that referenced this issue May 15, 2024
* Update nodejs.md

Clarifying that when you are tracking node in a serverless implementation, you need to make adjustments in Node.

Relevant github issue: mixpanel/mixpanel-node#175

* Update nodejs.md

---------

Co-authored-by: Shayan Aminmadani <shayan.aminmadani@mixpanel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants