Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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>
share|improve this answer
    
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? –  Qmal May 4 '13 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. –  bybe May 4 '13 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. –  Qmal May 4 '13 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. –  bybe May 4 '13 at 15:45

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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