accessing super properties before mixpanel is loaded
Hey Mixpanel,
Just curious why it's not possible to access super-properties before mixpanel is loaded. As far as I understand those are stored in a cookie or localStorage, so can be fetched immediately by the browser. Is there a technical limitation that makes those properties inaccessible before it's loaded? (e.g. using get_property() or mixpanel.persistence.properties())
The only technical limitation is that the code to interact with the persistence layer hasn't been loaded yet; the embed code needs to be as small as possible and basically doesn't know how to do anything except queue up async calls for execution once the lib has loaded. So yes, the browser can access superproperties by sniffing in cookies/localStorage before the full lib loads, but you'd need your own code to do that. Also we don't guarantee that cookie names/formats will never change, as that's not officially part of the public API.
If you're looking to access superproperties synchronously in your own non-Mixpanel code, your best bets are either:
- load the lib using a JS module bundler such as Webpack or Browserify, so that it becomes part of your app's JS bundle and you don't need the Mixpanel 'snippet' at all (at the expense of slightly more JS to load initially)
- use the snippet and run your code in the lib's
loadedcallback. That has the danger though that if the lib for some reason doesn't load (aggressive adblocker, user can't hit our CDN), the code in thatloadedcallback will never run.
Thanks for the detailed reply, @tdumitrescu. Did you guys consider separating the super-properties part into its own module, so it can be used either indepently, or with the core library?
Bundling mixpanel with our code seems to be a good workaround though. I'm trying to do this and bumped into a couple of questions however.
We're using rails, so I'm loading it using rails-assets-mixpanel (some kind of "proxy" between rails gems and bower). I can load mixpanel.min.js, but then it throws this error:
mixpanel.min.self.js?body=1:35 Mixpanel error: 'mixpanel' object not initialized. Ensure you are using the latest version of the Mixpanel JS Library along with the snippet we provide.
I saw on the readme that I can set window.MIXPANEL_CUSTOM_LIB_URL, but not sure what to set this to, because the minification process is done by rails sprockets (we're not using webpack or browserify). Normally any js code gets inlined and minified together with the rest...
Any suggestions on how to resolve this? I see that we can compile our own version, but this will make upgrading more difficult longer term, so if we can fit within the bower/rails-assets flow it would be much better in my opinion... And even then I need to specify a URL...
Thanks again!