Zen Cart Logo
Forums / General Questions / Tracking products added to basket in google analytics / event tracking

Tracking products added to basket in google analytics / event tracking

Views: 2,868

Results 1 to 7 of 7
1 Oct 2013, 6:24 PM
#1
dave1st avatar

dave1st

New Zenner

Join Date:
Sep 2013
Posts:
25
Plugin Contributions:
0

Tracking products added to basket in google analytics / event tracking

Hi,

I´d like to track products that have been added to basket.

As decribed here http://www.blog.analyticsinspector.com/how-to-track-products-added-to-basket-in-google-analytics/ I have to add event tracking code to my "add to cart" - button.

In html_output.php I added the code ```php
// form input button
$css_button = '<input class="' . $mouse_out_class . '" ' . $css_button_js . ' type="submit" value="' .$text . '"' . $parameters . $style . ' onclick="_gaq.push(['_trackEvent', 'addtobasket', 'Product name', 'Tracker Button']);" />';
}


So my html-output is
```html
<!--bof Add to Cart Box -->
                  <div id="cartAdd">
    Anzahl: <input type="text" name="cart_quantity" value="1" maxlength="6" size="4" /><br /><br />
<input type="hidden" name="products_id" value="944" />
<input class="cssButton button_in_cart" onmouseover="this.className='cssButtonHover button_in_cart button_in_cartHover'" onmouseout="this.className='cssButton button_in_cart'" type="submit" value="In den Warenkorb " style="width: 110px;" onclick="_gaq.push(['_trackEvent', 'addtobasket', 'Product name', 'Tracker Button']);" />        
 </div>
  <!--eof Add to Cart Box-->

But in analytics my conversion-goal is alway 0.
My goal:
category (Equals to) =addtobasket
Action (Equals to) =Product Name
Label (Equals to) = Tracker Button

Any idea what might have gone wrong?

Best ragards
Dave

1 Oct 2013, 6:38 PM
#2
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: Tracking products added to basket in google analytics / event tracking

Have you tried adding "javascript:" to the start of the onclick action?

1 Oct 2013, 7:40 PM
#3
dave1st avatar

dave1st

New Zenner

Join Date:
Sep 2013
Posts:
25
Plugin Contributions:
0

Re: Tracking products added to basket in google analytics / event tracking

No, I havent.

But I am not sure what have in mind ... how would the code look like?

1 Oct 2013, 10:03 PM
#4
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: Tracking products added to basket in google analytics / event tracking

Nevermind on the "javascript:", it is not needed... I do know the Google Analytics code needs to be loaded and using _gaq before the link is clicked on... I typically use jQuery to attach GA tracking to links and submit actions... Can say it works that way. Can also tell you events do not always show up right away in Google Analytics, sometimes they take 24-48 hours.

2 Oct 2013, 8:27 AM
#5
dave1st avatar

dave1st

New Zenner

Join Date:
Sep 2013
Posts:
25
Plugin Contributions:
0

Re: Tracking products added to basket in google analytics / event tracking

Thanks for your help.

Can you pls. tell me the code I should use with jQuery to load and use _gaq?

3 Oct 2013, 5:50 PM
#6
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: Tracking products added to basket in google analytics / event tracking

Keep in mind these examples REQUIRE the following (for the web page):

  1. jQuery library loaded.
  2. Google Analytics "ga.js" asynchronous code loaded.
  3. Google Analtyics configured for tracking your domain.
  4. Google Analtyics snippet uses _gaq (not a different variable).
    These NEED to occur before any "submit" or "click". There is still a small chance the event will not be tracked.

You will ALSO want to look into adding Google Analytics "e-commerce tracking" when a transaction is completed. There are a few threads on this website and the internet with examples :)

EXAMPLE - track "all" form submits (not just successful ones). Yes this should be attached to the "form" not the "input".

$(document).ready(function() {
  $('form').submit(function(event) {
    
    // Add some code to determine which product is added
    var product = 'Product Name + ID';

    var _gaq = _gaq || []; // Left to avoid issues if GA not loaded
    _gaq.push(['_trackEvent', 'addtobasket', 'product added', product]);
  });
 });

EXAMPLE - track clicks sending visitors to external resources (other websites such as social media):

jQuery(document).ready(function() {
  var re = new RegExp('^http(:?s)?://' + document.location.hostname);
  $('body').on('click', 'a', (function() {
    var href = $(this).attr('href');
    if(!re.test(href)) {
      var _gaq = _gaq || []; // Left to avoid issues if GA not loaded
      _gaq.push(['_trackEvent', 'external', 'link', href]);
    }
  }));
});
3 Oct 2013, 9:36 PM
#7
dave1st avatar

dave1st

New Zenner

Join Date:
Sep 2013
Posts:
25
Plugin Contributions:
0

Re: Tracking products added to basket in google analytics / event tracking

Hi lhungil,

thanks for your support.

I am a beginner in GA and zencart, so I am not sure what you mean.

1-4 should be ok because I can track visitors on the webside. e-commerce-tracking is also activated.

"Yes this should be attached to the "form" not the "input"."

Do you mean? ```php
// form input button
$css_button = '<input class="' . $mouse_out_class . '" ' . $css_button_js . ' type="submit" value="' .$text . '"' . $parameters . $style .

$(document).ready(function() {
$('form').submit(function(event) {

// Add some code to determine which product is added
var product = 'Product Name + ID';

var _gaq = _gaq || []; // Left to avoid issues if GA not loaded
_gaq.push(['_trackEvent', 'addtobasket', 'product added', product]);

});
});

" />';
}

That seems not to work. Or do you mean anything else?