0

i would to use fastest asynchronous google analytics snippet, but i'm not a good programmer and so i don't know what is the best:

A)

<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<script async src='//www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->

https://developers.google.com/analytics/devguides/collection/analyticsjs/tracking-snippet-reference#async-snippet-minified

B)

<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :   'http://www') + '.google-analytics.com/ga.js';
ga.setAttribute('async', 'true');
document.documentElement.firstChild.appendChild(ga);
})();
</script>

About these codes i would like to know if is a problem to put the code inside not <script> but inside <script type="text/javascript"> and if is a problem to put not before </head> (like suggested for "normal" snippet)

I hope you can help me and sorry for my english :) Thanks a lot!

Borja
  • 3,359
  • 7
  • 33
  • 66
  • 2
    These are two different versions of the GA tracking code. The second version is deprecated and does not support all features (no custom metrics and dimensions, no enhanced ecommerce tracking etc). So you should use the first version no matter what. – Eike Pierstorff Mar 12 '16 at 16:00
  • Please @EikePierstorff can you show me a differente example of the first version ? in my website (with CMS drupal) is difficult (for me) add the first version for the presence of the tag script with async attribute. And i don't know if i can use it without that attribute. I hope you can help me please :( – Borja Mar 12 '16 at 16:06
  • 1
    This has nothing to do with speed and all to do with the fact that the first one is the current version of Google analytics (universal analytics which contains a large number of features) the second is classic analytics which is an older version of google analytics which doesn't track as much information. You should be using analytics.js no matter what. – Linda Lawton - DaImTo Mar 14 '16 at 08:19
  • @DaImTo ok thanks a lot, i will use first version :) – Borja Mar 14 '16 at 11:38

2 Answers2

0

The would guess the first one. It uses async and doesn't depend on DOM modification while loading the page.

Jørgen
  • 2,157
  • 1
  • 23
  • 24
0

For HTML5, it does not matter if you use <script> or <script type="text/javascript">.

As for where to put in your code, normally javascript should be at the bottom of the page after all of your css/html has loaded. Javascript is loaded synchronously, so each file load will stop the rest of the page from loading until that specific file is fully loaded. The async tag is not fully supported by all browsers, so I would not rely on that. Instead, you should use an asynchronous loader (like requirejs or LAB.js).

As for where you should put the google analytics script, in my experience it doesn't matter too much. It sounds like you're prematurely optimizing -- https://stackoverflow.com/a/385529/5780021. Per google's instructions, I believe it's supposed to go in the header in order to improve their statistics around page load speeds.

Community
  • 1
  • 1
Alex R
  • 624
  • 3
  • 10