Rails: Javascript for tracking sessions firing inconsistently -


i have js code in rails app fires tracking event mixpanel on new session.

theoretically, before other event fired, should see "new session" event first. on visits, don't see "new session" event means it's not being fired on occasions.

what's wrong below code?

$(function(){   var currentlocation = window.location.hostname;   var lastlocation = document.referrer;   if (lastlocation.indexof(currentlocation) > -1) {   } else {     mixpanel.track("new session", {});   }   mixpanel.track("page view", {}); }); 

if you're using turbolinks ready event not fired after first page load, need bind custom turbolinks events page:load like:

var ready; ready = function() {   var currentlocation = window.location.hostname;   var lastlocation = document.referrer;   if (lastlocation.indexof(currentlocation) > -1) {   } else {     mixpanel.track("new session", {});   }   mixpanel.track("page view", {}); };  $(document).ready(ready); $(document).on('page:load', ready); 

for rails 5 event name changed turbolinks:load


Comments

Popular posts from this blog

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

matlab - error with cyclic autocorrelation function -

php - Using grpc in Laravel, "Class 'Grpc\ChannelCredentials' not found." -