Currently, I have integrated require.js (http://requirejs.org/) for various functionalities on my website and it is functioning smoothly. However, I encountered a problem when attempting to incorporate Google Analytics code. The code does not seem to be adding a utm.gif file and is failing to send a beacon to Google. I suspect that it might be related to a scope issue.
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;
});
The ga.debug feature does not produce any errors and the utm.gif file is not displayed. When I tested moving the code outside of require.js (meaning integrating the modular JavaScript directly into the page), the utm.gif file was added successfully and ga.debug sent out the beacon as expected.
I came across a site where this integration seems to work perfectly, but I am unsure about what they are doing differently:
Has anyone else faced challenges when combining require.js with Google Analytics?