: Any drawbacks by including Google Analytics in your main js file? I'm using MVC that is compressing and minifying all my js files into one file so basically there is only one request. As far
I'm using MVC that is compressing and minifying all my js files into one file so basically there is only one request.
As far as I see every page has Google Analytics code below their js includes. If I will not do that but instead add it in my main js file will it cause any problems?
More posts by @Deb1703797
2 Comments
Sorted by latest first Latest Oldest Best
I would say no drawbacks. If you add the code at the bottom of your main js file it won't be different than adding it inline in the HTML after the main js file.
The possible variable conflicts could be the same either it's added inline in HTML page or at the bottom of your main js file.
I don't even see how it could slow down because it's calling an external JavaScript file. It's calling it anyway even if added inline in HTML so it does not make any difference.
Moreover the external js file is called later on because it's added using createElement, so even if it does not exist it shouldn't slow down the page.
You may even get such a small speed increase that it will be hardly noticeable by anyone.
Taken from Stack Overflow
You ALWAYS have to worry about collissions of variables defined in the
global scope in JavaScript, REGARDLESS of whether you minify your
scripts or not. Use a functional closure wrapper to wrap your code if
you want to minimize chances of collission.
The problem is that it can conflict with other scripts of course you can use variables and closures to ensure this doesn't happen but the other problem which is not included in the answer on overflow is that its calling upon an external JavaScript which can slow down the min JavaScript file.
Below is the standard usage of the Script and as you can see it calls upon a external JavaScript:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
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);
})();
</script>
Due to the fact it is calling upon an external script I would be say you are best not serving it within a main JavaScript as its raises problems of conflicts and would prove little gain because of the external file.
I believe the best you can do is minify the inline code which will save around 400 bytes like so:
<script>var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];(function(d){var g=d.createElement('script'),s=d.scripts[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document))</script>
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.