Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Google Analytics Event tracking on “add to cart” button
 
larsjuulmadsen
Member
 
Avatar
Total Posts:  33
Joined:  2011-07-29
Copenhagen, Denmark
 

Hi,

In the past we have tracked how many added something to the cart, by taking them directly to the cart and have a goal in analytics showing that.

But we no longer want to take customers directly to the cart, but have them around on the site for a longer period.
Therefore I would like to add tracking to the “add to cart” button.

1. Where should I place the code (in which file, line, paragraph please)

2. Is it possible to track what product the button belong to, so we can track which products are added to the cart most often?

Thanks in advance.

Best regards

 
Magento Community Magento Community
Magento Community
Magento Community
 
larsjuulmadsen
Member
 
Avatar
Total Posts:  33
Joined:  2011-07-29
Copenhagen, Denmark
 

So I have this code now in my footer:

<button type=\"submit\" class=\"form-button-alt f-right\” onSubmit=\"pageTracker._trackEvent(\"Contact Forms\”, \"Newsletter Signup\"><span>Tilmeld</span></button>

And then I have my GA code:

<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type=\"text/javascript\">
//<![CDATA[
(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\’;
(document.getElementsByTagName(\’head\’)[0] || document.getElementsByTagName(\’body\’)[0]).appendChild(ga);
})();

var _gaq = _gaq || [];

_gaq.push([\’_setAccount\’, \’XXXXX\’]);
_gaq.push([\’_trackPageview\’]);

//]]>
</script>
<!-- END GOOGLE ANALYTICS CODE --></div>

But I dont get any events in my Google Analytics, what am I doing wrong?

Best regards

 
Magento Community Magento Community
Magento Community
Magento Community
 
duffybelfield
Sr. Member
 
Avatar
Total Posts:  119
Joined:  2008-08-14
 

Because it will only be running that code on page load and not on the “onclick” of the add to cart button.

You maybe be able to extend the button and have some ajax madness running the script.

It maybe worth having a look at the ajax add to basket and extending it to run the script

http://www.magentocommerce.com/magento-connect/aheadworks/extension/1185/ajax-cart-pro

 
Magento Community Magento Community
Magento Community
Magento Community
 
HelgeB
Jr. Member
 
Total Posts:  14
Joined:  2012-01-14
 

You can add a onclick function to the add to cart button in the addtocart.phtml template - for example:

onclick="_gaq.push([’_trackEvent’, ‘Your event Category’, ‘Your Event Action’,’<?php echo $this->jsQuoteEscape($this->stripTags($_product->getName(), null, true)) ?>’]);

In this example the product name is send as event label to google analytics.

Regards,
H.

 
Magento Community Magento Community
Magento Community
Magento Community
 
chiefair
Mentor
 
Avatar
Total Posts:  1848
Joined:  2009-06-04
 

Product view page Add To Cart button code:

<button type="button" id="gat-<?php echo $_product->getId(); ?>" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="productAddToCartForm.submit(); submitGaTrack(); return false;"><span><span><?php echo $this->__('Add to Cart'?></span></span></button>
            
<?php $gatSku preg_replace("/[^a-zA-Z0-9-\s]+/"""$_product->getSku()); $gatName preg_replace("/[^a-zA-Z0-9-\/\s]+/"""$_product->getName()); $gatPrice intval($_product->getPrice()); ?>
            
<script type="text/javascript">
                function 
submitGaTrack() {
                    _gaq
.push(['_trackEvent''addtocart''<?php echo $gatSku; ?>''<?php echo $gatName; ?>'<?php echo $gatPrice?>]);
                
}
            </script>

With the operative portion being in an onclick="” action being added to the button code. The add button will fail on any error caused by data imported from Magento not being properly santitized, it is best to put it in a separate java function so it doesn’t block the button submit from working:

onclick="productAddToCartForm.submit(); submitGaTrack(); return false;"

Assembly of the trackEvent information to be sent from Magento which strips out extraneous garbage that causes the gaq submission to fail or javascript errors:

<?php $gatSku preg_replace("/[^a-zA-Z0-9-\s]+/"""$_product->getSku()); $gatName preg_replace("/[^a-zA-Z0-9-\/\s]+/"""$_product->getName()); $gatPrice intval($_product->getPrice()); ?>

And the separate javascript function to be added to the button onclick="” action so the addbutton doesn’t get blocked if there’s an error in the gaq code. striptags() is not sufficient to prevent code blowout:

<script type="text/javascript">
                function 
submitGaTrack() {
                    _gaq
.push(['_trackEvent''addtocart''<?php echo $gatSku; ?>''<?php echo $gatName; ?>'<?php echo $gatPrice?>]);
                
}
            </script>
 
Magento Community Magento Community
Magento Community
Magento Community
 
r.vthoff
Member
 
Total Posts:  31
Joined:  2011-02-03
 

I think using this URL in your Analytics funnels should work to register add to cart actions

/checkout/cart/add
 
Magento Community Magento Community
Magento Community
Magento Community
 
scarlettstark
Jr. Member
 
Total Posts:  5
Joined:  2014-01-26
 

Hi,

If you haven’t solved your problem, I think this module can be the solution to your issue:

http://restaurant.web-cooking.net/seo/magento-google-universal-analytics.html

Some fonctionnalities:

- Use the new Google Universal Analytics with your Magento
- Choose between classical transaction system (transaction sent on order success page) or server-side transaction system (transaction sent when invoicing)
- By choosing server-side system, you will have an exact conversion rate!
- Server-side transaction system, using Google’s Measurement Protocol support, allow you to track real transactions (only paid orders!)
- Track following events : Customer Registration, Customer Login, Customer Logout, Add product to cart, Add coupon code, Share wishlist, Add product to wishlist, Newsletter subscription & unsubscription (Ask for more!)
- Track magento ajax sections in onepage checkout
- Manage dimensions : customer ID, customer Email, customer group ID

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jase007
Guru
 
Total Posts:  388
Joined:  2013-11-05
 

Hi,

Have a look this thread http://www.practicalecommerce.com/articles/4023-Google-Analytics-5-Custom-Ecommerce-Events-to-Track

Hope this will help you!!

Thank

iLoveMage

 
Magento Community Magento Community
Magento Community
Magento Community
 
hollyscott
Jr. Member
 
Total Posts:  5
Joined:  2014-03-17
 

Hi,

I don’t know if you are still searching but we have this good extension:

http://restaurant.web-cooking.net/seo/magento-google-universal-analytics.html

And some fonctionnalities:

Use the new Google Universal Analytics with your Magento
Choose between classical transaction system (transaction sent on order success page) or server-side transaction system (transaction sent when invoicing)
By choosing server-side system, you will have an exact conversion rate!
Server-side transaction system, using Google’s Measurement Protocol support, allow you to track real transactions (only paid orders!). But if you want, an option allow you to track all orders (with non-paid ones).
Track following events : Customer Registration, Customer Login, Customer Logout, Add product to cart, Add coupon code, Share wishlist, Add product to wishlist, Newsletter subscription & unsubscription (Ask for more!)
Track magento ajax sections in onepage checkout
Manage dimensions : customer ID, customer Email, customer group ID, product SKU, product Name

Hope it helps !

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top