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

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -