Solved

Amplitude doesn't work

  • 22 February 2022
  • 4 replies
  • 741 views

I added amplitude event tracking calls next to all my mixpanel event tracking calls in my nodeJS serverless backend, but no events are showing up, and I’m not sure how to fix it as no errors are thrown. 

Here is the event code in JS:

 

amplitudeClient.logEvent({
              event_type: 'EVENT NAME',
              user_id: (60 character long UUIDV4 string),
              ip: '127.0.0.1',
              event_properties: {
                prop1: prop1,

                prop2: prop2
              }
            });

 

😂

icon

Best answer by qingzhuo 25 February 2022, 03:15

View original

4 replies

Userlevel 2
Badge +1

Hi @b100 This is the doc for Node SDK. Feel free to refer to it.

 

For your issue, is the client has been initialized with correct api key? And maybe put a flush call after logEvent? Normally this would happen automatically, and I wonder if serverless backend affects that.

Userlevel 5
Badge +5

@qingzhuo Thanks for great trouble-shooting questions. As per communication in a zendesk ticket with @b100,  the issue has been solved.

“I was calling the amplitude event tracking calls right before the terminating line in a nodeJS serverless function (res.send()), which would terminate the tracking call before it was completed, which is why it wasn't working.”

Badge

@b100 @Yuanyuan Zhang 

What was the solution? Can you please share it with us? Thanks in advance!

Badge

Never mind. I figured it out. Since track() returns a promise, we can use `await` to wait for the callback. Anyone who’s using ampli can try this:

export default async function handler (req, res) {

const result = await ampli.songPlayed(‘user-id’, { title: ‘Song #1’ }).promise;
console.log(result.statusCode) // 200

res.status(200).json({ status: ‘success’ });
}

Here’s the documentation that points to the callback example:
https://github.com/amplitude/Amplitude-TypeScript/tree/main/packages/analytics-node#callback

Reply