We have a Rails application where we are including our application dependencies in the html head within application.js
:
//= require jquery
//= requi
This can happen if you don't wait for the script to load that defines analytics
or if you do not define the order in which the javascript files are loaded. Make sure that the script that defines analytics
is always loaded before you try to call its method track
. Depending on your setup the scripts could load in random order, leading to this unpredictable behavior.
You tried to make sure everything was loaded, but the listener $(document).ready(function () {});
just makes sure that the DOM is ready, not that analytics is available. And here you have the same problem. $
is just jQuery so $ is not defined
means jQuery hasn't been loaded yet. So probably your script came before jQuery was loaded and tried to call what wasn't defined yet.