Detecting Asynchronous and Universal Syntax in Analytics

Detecting Asynchronous ga.js and Universal Syntax analytics.js of Google Analytics is easy. For any reasons stated below, this article might be helpful.

Possible Reasons Why Do You Want to Determine Google Analytics Syntax

  • You want to upgrade your current analytics
  • Exact code to be implemented with your website
  • Determining which is better
  • Module compatibility for your GA
  • You are a Web Developer
  • You’re a Marketer

Whatever reasons you have in mind I hope this helps.

Determining Your Current GA Script

When you look at your website page source by keyboard shortcut ctrl+u (windows) or command+option+u (Mac) you will probably see this following codes, otherwise, it wasn’t installed.

For Universal Analytics (analytics.js) Script

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXX-Y', 'auto');  // Just replace "UA-XXXX-Y" with your actual GA ID
ga('send', 'pageview');

</script>
<!-- End Google Analytics -->

For Asynchronous Analytics (ga.js) Script

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXX-Y']); // Just replace "UA-XXXX-Y" with your actual GA ID
  _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>

Source Google Analytics.

Javascript Detection for Developers

Well, if you are a Web Developer like me, you can do a simple browser detection whether a certain function exist or not, which goes like this.

try{ (typeof _gaq.push === "function")
    alert("Asynchronous ga.js found");
   }catch(e){
    alert("Asynchronous ga.js not found");
}

try{ (typeof window[window['GoogleAnalyticsObject']] === "function")
    alert("Universal analytics.js found");
   }catch(e){
    alert("Universal analytics.js not found");
}

These are just two Google Analytics format that most users are using.

It is recommended to upgrade your code to analytics.js which was posted in their website that states “The analytics.js library is the latest JavaScript library for Google Analytics designed to take advantage of all the new features of the Universal Analytics platform.” – Google Developers. And as far as I know, the latest has many features that the Asynchronous GA version.

Detecting Asynchronous and Universal Syntax in Analytics1.00/5 (20.00%) 1 vote
About

Elvin is one of the contributors of InnoveDesigns.com. He has been creating Wordpress Plugins, Magento Extensions, & Mobile Applications. If you find this article useful, please Follow him on .

Published under Web Development Guide