tags:

views:

39

answers:

2

Instead of putting the code at the head or end of body (i put it at the end of body), is it ok if i stick the code inside of a JS file instead of its own script tag in html?

(i assume it works fine like any other code but i am asking in case)

+1  A: 

is it ok if i stick the code inside of a JS file instead of its own script tag in html?

It is perfectly fine, it is basically the same thing; you are importing the javascript in your html page anyway.

Sarfraz
+1  A: 

Yes, you can just add the google analytics to a external js file. Like this:

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

And after this, you call on every page or put in the same js external file:

callGA();
Topera