When you check your website with PageSpeed Insights being warned by it, you will surely find a way to fix it, right? My guide below will definitely fix that problem:
When you create a new and insert Google Analytics tracking code for your website, Google will provide you with a snippet similar to the sample below.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-1');
</script>
Google instructs to insert that code before the closing </head> tag , but actually you can also insert the bottom code before the closing </body> tag . However, whether you pre-insert </head> or before </body> will affect the page load time.
For other platforms do not have to Blogspot:
You just need to paste this JavaScript before the </body> tag is okay:
<script>
window.addEventListener('load', function() {
var is_load = 0
function loadanalytics() {
if (is_load == 0) {
is_load = 1
var an = document.createElement('script')
an.async = true;
an.src = 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-1'
var sc = document.getElementsByTagName('script')[0]
sc.parentNode.insertBefore(an, sc);
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-1');
}
}
window.addEventListener('scroll', function() {
loadanalytics()
})
})
</script>
Note: The highlighted snippet is the Analytics tracking code provided by Google above!
For Blogspot platform
By default in the template, Blogger also provides a default Google Analytics data tag, which has the name:
<b:include data='blog' name='google-analytics'/>
If you check in the template that already has this tag, simply get the tracking ID and insert it in Settings> More> Google Analytics> Analytics web property ID so as
not to affect page performance, after you have insert the Analytics web property ID in the settings, you go to edit the topic to find the Google Analytics data tag and delete it, then re-insert the code before the closing </body> tag as follows:
<b:if cond='data:blog.analyticsAccountNumber'>
<script>
var analyticsAccountNumber="<data:blog.analyticsAccountNumber/>"
//<![CDATA[
var fired=false
window.addEventListener('scroll',function(){
if((document.documentElement.scrollTop!=0&&fired===false)||(document.body.scrollTop!=0&&fired===false)){
(function(){
var an=document.createElement('script')
an.async=true;an.src='https://www.googletagmanager.com/gtag/js?id='+analyticsAccountNumber
var sc=document.getElementsByTagName('script')[0]
sc.parentNode.insertBefore(an,sc)
})();
window.dataLayer=window.dataLayer||[]
function gtag(){dataLayer.push(arguments)}
gtag('js', new Date())
gtag('config',analyticsAccountNumber)
fired=true
}
},true)
//]]></script>
</b:if>
Using the scroll method prevents script loading as soon as the page loads, only when the user scrolls to start loading the script. Also if you leave the Analytics web property ID blank in the settings the script will not load.
Labels :
#Tips and Tricks ,