i've been fighting with this problem from a while now. I think it all started when i've added my website to google webmaster's tool: at some point I started receiving an alert saying my website was missing the tracking code, but actually it was there, and even though it said it was missing, i still got my analytics stats.
at that time, the code was include in a .js file containing all of my javascript. it was included right before the </body>
tag and it was loaded this way:
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "js/build/production.min.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else
window.onload = downloadJSAtOnload;
</script>
</body>
i've found this script on some blog (http://www.giftofspeed.com/defer-javascripts/) and I use this to defer javascript loading to make the page to load faster.
at some point i thought deferring the ga code was the problem (as it's all in that production.min.js
file) so i've moved the ga code outside of that. and now it's like this
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "js/build/production.min.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else
window.onload = downloadJSAtOnload;
</script>
<script>
//my google analytics code here
</script>
</body>
once I did this, the alert got solved BUT the tracking is not happening anymore.
I've been using GA from ages and I never had problems like these. What could this be? I feel like i've tried it all.
Approach for modern browsers
https://github.com/jfriend00/docReady/blob/master/docready.js
Your customisation: