mixpanel-node icon indicating copy to clipboard operation
mixpanel-node copied to clipboard

Add User Agent support

Open codynguyen opened this issue 9 years ago • 8 comments

Hi,

We're using the library in our project and there are usecases where we want to pass the user agent header (from the requests users made to our server) to mixpanel tracking calls so we can see from which browsers the requests are made.

I can see that the requests mixpanel-node is making have headers set to blank {}. It'd be nice to be able to pass in request headers to the mixpanel calls. Combined with the current ability to manually set ip for the tracking calls, this helps make event tracking more consistent across client, and it makes a nice addition to this article https://blog.mixpanel.com/2015/07/06/community-tip-how-to-keep-properties-consistent-across-client-and-server-libraries/

codynguyen avatar Oct 31 '16 06:10 codynguyen

While we could add an option to configure HTTP headers for each track call (or globally), I don't think it will help with your stated problem of achieving client-server parity in user agent detection: the browser/os/device properties that you see in events sent by the browser JS library are in fact calculated on the client.

If you think it would be useful, we could explore extracting those utils to work outside of a browser context, so you could just import them in your server-side code like so:

import { detectBrowser } from 'mixpanel-browser/utils';

// in your request-handling code:
const trackingProps = {
  $browser: detectBrowser(req.headers['user-agent']),
};

As you can see in the browser JS code, there are some issues around Opera and Mobile Safari detection needing info that isn't available in the user agent string, but if you can deal with the loss of granularity there or supply values for vendor/opera yourself then it should be usable for you.

tdumitrescu avatar Nov 01 '16 20:11 tdumitrescu

import { detectBrowser } from 'mixpanel-browser/utils' is no longer working.

I copied some codes from mixpanel-browser/utils and rewrote a simple library to do this.

I shared the code on my gist https://gist.github.com/jerrybendy/8e03cda8f2ece23b6469acd181c594ee

It accepts an userAgent as its parameter, and returns mixpanel-liked object format, such as $os, $browser etc. But some extra parameters like $current_url, $referrer and ip, you should add them by yourself.

jerrybendy avatar Nov 20 '17 11:11 jerrybendy

Can confirm that @jerrybendy's solution works like a charm. However, I think there'd be a lot of value to a solution baked into the module here, something where I can just pass userAgent and it does that for me.

wbobeirne avatar Jul 27 '18 18:07 wbobeirne

the year is 2022... this functionality has not yet made its way into the mixpanel-node library.

that is despite the fact that mixpanel recommends server side tracking whenever possible...

xgabz avatar May 07 '22 15:05 xgabz