Skip to content Skip to sidebar Skip to footer

Google Analytics In Js File Instead?

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 ass

Solution 1:

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.

Solution 2:

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

functioncallGA() {
    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();

Post a Comment for "Google Analytics In Js File Instead?"