简体   繁体   中英

Google Analytics page views javascript vs event tracking javascript

Currently, I have a standard google analytics snippet that counts page views:

  (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-xxxxxxxx', 'example.com');
  ga('send', 'pageview');

However, I would now like to add event tracking to some elements on my website and for that I need to add this tracking code as suggested by GA:

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _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);
  })();

My question: do I need to replace the original code with the tracking code, or should I have both scripts?

Google FAQ wasn't very clear about it, but it turns out there are two javascript libraries for google analytics analytics.js and ga.js , where analytics.js is a new library and ga.js is an older library, but both providing the same service.

So to use tracking with analytics.js you don't need to include any extra snippets, but make sure that the tracking APIs you are using are for the newest library

Upgrade: ga.js to analytics.js

Event Tracking (analytics.js)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM