Statcounter asynchronous code - non render blocking

From ETCwiki
Jump to navigationJump to search

Recently I have been working on asynchronous loading of javascript resources on my pages. I use Statcounter as my way to see who visits my sites. Statcounter's code by default is not asynchronous. Pagespeed Insights says that it does not like statcounter for speed reasons because it will block the rest of the site from rendering. If you use Google's Pagespeed Insights, it will read:

Eliminate render-blocking JavaScript and CSS in above-the-fold content

Statcounter async code

To make your statcounter code asynchronous, add the word "async" inside the script tag for your statcounter code. Yes, this really does fix the pagespeed problem with statcounter being a render-blocking script. This is new as of HTML5. Your result will look like this:

<!-- Start of StatCounter Code for Default Guide -->
 <script type="text/javascript">
 var sc_project=YOURPROJECTHERE; 
 var sc_invisible=1; 
 var sc_security="YOURCODEHERE"; 
 var sc_https=1; 
 var sc_remove_link=1; 
 var scJsHost = (("https:" == document.location.protocol) ?
 "https://secure." : "http://www.");
 document.write("<sc"+"ript type='text/javascript' src='" +
 scJsHost+
 "statcounter.com/counter/counter.js' async></"+"script>");
 </script>
 <noscript><div class="statcounter"><img class="statcounter"
 src="http://c.statcounter.com/YOURPROJECTHERE/0/YOURCODEHERE/1/"
 alt="shopify traffic stats"></div></noscript>
 <!-- End of StatCounter Code for Default Guide -->


Your new result in Pagespeed Insights should be better than before.