Viktor
Viktor

Reputation: 633

How to set Analytics Event in jQuery without a click event?

i am trying to set an Analytics Event in jQuery like this

jQuery(document).ready(function() {
   var time_to_open=15000;
   if(readCookie('cookie')!='set'){
      setTimeout(function() {
        jQuery('#my_animation').animate({left: 0}, 1000);
        createCookie('cookie', 'set', 1);
        _gaq.push(['_trackEvent', 'animation', 'started', time_to_open]);
      },time_to_open);  
   }
});

this should track how often an animation was shown. But it is not working. Are _trackEvent only targeting click events? Or what i am doing wrong?

Upvotes: 1

Views: 104

Answers (2)

Gaurang Jadia
Gaurang Jadia

Reputation: 1516

As per the documentation,

  1. category: animation
  2. action: started
  3. opt_label: time_to_open (label for the action)
  4. opt_value: 15000 (Int value)
  5. opt_noninteraction: false

Here is the sample:

jQuery(document).ready(function() {

    var time_to_open = 15000;

    if(readCookie('cookie') != 'set') {
        var t = window.setTimeout(function() {

            jQuery('#my_animation').animate({left: 0}, 1000);

            createCookie('cookie', 'set', 1);

            _gaq.push(['_trackEvent', 'animation', 'started', 'time_to_open', time_to_open, false]);

        }, time_to_open);
   }
});

Upvotes: 1

mike
mike

Reputation: 7177

_trackEvent can silently fail if the opt_label parameter is not a string. Either convert time_to_open to a string, or pass it as the opt_value parameter.

_gaq.push(['_trackEvent', 'animation', 'started', undefined, time_to_open]);

(Google Analytics _trackEvent docs)

Upvotes: 2

Related Questions