New Google Analytics code looks like one below:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_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>
How to move the whole new Google Analytics Asynchronous Tracking Code into an external JavaScript file?
I'm asking especially about "var _gaq = _gaq || []; [...]" part because I know it's possible to move the rest e.g.
index.html
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-0000000-00']);
_gaq.push(['_trackPageview']);
</script>
<script src="include.js" type="text/javascript"></script>
include.js
function includeGA()
{
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);
}
$(document).ready(function()
{
includeGA();
});
I've already tried to place the "var _gaq = _gaq || []; [...]" code into various locations but nothing worked.
You need to add this script to the head tag of your website. In Next. js, you use the Script component from next/script. This component is an extension of the HTML script tag.
You shouldn't put GA code in function for two reasons:
People criticizing the use of external JS for GA code, are bit wrong here. With introduction of async attribute in js embed, it makes sense. I keep this GA code in external js and embed it async right in the head of document. Two benefits:
So the HTML would become:
<head>
<script type="text/javascript" src="static/scripts.js" async></script>
</head>
and scripts.js
will have:
var _gaq=_gaq||[];
_gaq.push(['_setAccount','UA-XXXXXXXX-1']);
_gaq.push(['_setDomainName','.domain.net']);
_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)
})();
Then again: don't put your Google Async snippet in an external file because it will block other contents from downloading. The main point of having an asynchronous tracking code is to make it parallel.
If you use the asynchronous GA just put it in the top of you document in an inline script tag. This is the recommendation on Google Analytics website as well:
Insert the asynchronous snippet at the bottom of the
<head>
section of your pages, after any other scripts your page or template might use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With