I am using require.js ( http://requirejs.org/ ) for a number of functions on my site, and for now it works Well. However, while trying to enable the Google Analytics code, I ran into a problem. The code seems to refuse to add utm.gif and does not send the beacon to Google. I wonder if this is a subject of visibility.
define(function() { var Analytics = {}; Analytics.Apply = function() { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _gaq.push(['_trackPageview']); var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); } return Analytics; });
ga.debug does not produce errors, and utm.gif does not appear. If I translate the code outside of require.js (by which I mean modular javascript using require.js, so just adding it to the line on the page), utm.gif is added to the page successfully, and ga.debug sends its beacon.
I found this site that seems to be using it successfully, but I'm not sure this site does different: http://paceyourself.net/2011/05/14/managing-client-side-javascript-with-requirejs/
Does anyone else run into problems combining require.js and GA?
source share