3

I want to track events on a chrome extension I'm building. So I want to have a unique identifier for each user.

I don't want any information about the user, simply I want all of they're events grouped together.

Is there a good way to solve this, without pinging a server. Or will I just use a really long random string so the probability of getting the same string by another user is low.

I'm using segment.io's analytics.js package to integrate with mixpanel.

Eoin Murray
  • 1,935
  • 3
  • 22
  • 34
  • how did you manage to get this work, I am loading segment.io in my content script but it seems to only send page not tracks – user299709 Aug 30 '14 at 03:42

2 Answers2

3

You can generate an id upon extension install/update. You can then use this value as your unique id. Something like this should work (put it in your background page):

chrome.runtime.onInstalled.addListener(function(info){
    //    
    // info.reason should contain either "install" or "update"

    var sessionId = localStorage.getItem("session-id");

    if(!sessionId){
      localStorage.setItem("session-id", "random-session-id");
    }
});

More info on onInstalled is here: https://developer.chrome.com/extensions/runtime#event-onInstalled. NB: it fires on install, extension update and Chrome update.

Philip Nuzhnyy
  • 4,630
  • 1
  • 25
  • 17
1

Not sure why Mixpanel is tagged on this question, but since it is, I'll mention that the Mixpanel jslib solves this problem.

If you send events without identifying the user, the jslib automatically generates a UUID based on time, Math.random(), and browser characteristics. This data is stored in a cookie.

raylu
  • 2,630
  • 3
  • 17
  • 23
  • I was going to use segmentio to send events to mixpanel, I should have specified. – Eoin Murray Feb 21 '13 at 20:13
  • What a noob I am to not think of using the current time to generate a unique id. I wont use mixpanels jslib, but you deserve the accepted answer nonetheless. – Eoin Murray Feb 21 '13 at 20:17
  • yes true, but using it as a seed for a 32 digit long random string will be okay for me – Eoin Murray Feb 22 '13 at 11:46
  • Using Math.random() is not guaranteed to be unique. It probably serves well enough for anonymous statistics, but if you are going to track across browser restarts, you should generate a unique id and then store it using chrome.storage. When starting up, you can check if it exists in storage and use that. There's a great implementation of RFC4122 [here](http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript). – michaelday Aug 13 '14 at 01:13