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