2

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?

2 Answers 2

1

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>
4
  • The minifier already wraps every file it includes to prevent any conflicts. So basically if there is no risk of conflicts there is no drawbacks in what I'm doing?
    – sed
    Commented May 4, 2013 at 11:07
  • Well the only risk would be the external file which is google-analytics.com/ga.js if Google becomes unresponsive, then what? I think your'd need to test the code using a dummy domain for example... no-response.com/ga.js and see what happens.. if the rest of the JavaScript executes fine then it should be ok to use, but if it halts the JS then you have an issue... Test it out and let us know. Commented May 4, 2013 at 11:14
  • 1
    Tested it out it seems it does not slow down page load speed. If anything it speeds it up since it no longer pulls ga.js from google. It seems that it's all good.
    – sed
    Commented May 4, 2013 at 11:25
  • Did you test with a false domain so that the ga.js would not load and how this would effect other JS executions as well as the dom events. Commented May 4, 2013 at 15:45
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.