Google Analytics tracking code into an external file

Google Analytics is easy and is convenient for the access analysis on the homepage. It is possible to use it to analyze Google Adsense. The asynchronization tracking code It puts at the end of the head tag. This tracking code was made a external file and it read.

What is the tracking code of Google Analytics?

The tracking code of Google Analytics becomes the tracking code of the asynchronization if it confirms it recently and is put at the end of the head tag before though inserted at the end of the body tag of HTML. The asynchronization tracking codes are the following one.

<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>

Point where asynchronization tracking code is good

The reading time of the page is shortened.

The collection and the accuracy of data improve.

The error by Javascript's not having been normally read is evaded.

The tracking code of Google Analytics is made a external file and it reads.

Then, it does the lash-up in the head tag when the code is seen and seems to be hard to see though put at the end of the head tag. This is Javascript when often seeing. It experimented why you did not put the tracking code in this because it just read external.js in the head tag the other day. As for the result, the access analysis seems to be done without trouble.

When making it to the external file, the script tag is removed as follows, and it adds it next to an existing file. Thus, time to read the file should be able to be shortened. It must be good also for SEO.

  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);
  })();

The tracking code of Google Adsense has been added to the external file.

When the tracking code of Google Adsense operated by another site and the subdomain is added, the code is similarly added.

window.google_analytics_uacct = "UA-XXXXX-X";

Method of calling tracking code etc. in head tag

This code is preserved in the domain route etc. for instance by the name of example.js, and it up-loads it. To call this, it describes it in the HTML header as follows.

<script type="text/javascript" src="http://hogehoge.jp/example.js"></script>