Google Analytics Tracking GA.js vs Analytics.js

sahmed24 picture sahmed24 · Apr 24, 2013 · Viewed 12.3k times · Source

I'm trying to use this code to track an event in Google Analytics

    _trackEvent(category, action, opt_label, opt_value, opt_noninteraction)

This seems to be meant to be use with the GA.js Analytics "package", however I'm using the Analytics.js

Like this

    (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');

Is there a way I can do even tracking with this code only ? Or do I have to use

  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);
  })();

Do I need both of these codes? Or is there some different way using only Analytics.js

Thanks in advance

**EDIT: Indeed I probably didn't explain what I want, it might not even possible. I want to work with Funnels on Events, and I want to use _trackEvent (that's what I thought it would do) to mark a user as having Entered the Funnel, if the event is send then we will have the normal funnel. So if 10 users enter the page and 1 clicks on the button, I would have 10 events on the Funnel with 1 success

Answer

Crayon Violent picture Crayon Violent · Apr 24, 2013

analytics.js will report to GA same as the old code, but it has different syntax, so you can't use _trackEvent.

Here is a link to the basic on-page syntax with analytics.js, and here is a link for event tracking with analytics.js

<!-- 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');      // GA account ID goes here
ga('send', {
  'hitType': 'event',           // Required.
  'eventCategory': 'category',  // Required.
  'eventAction': 'action',      // Required.
  'eventLabel': 'opt_label',
  'eventValue': opt_value,
  'nonInteraction': opt_noninteraction
});
</script>
<!-- End Google Analytics -->