Magento – How to implement e-commerce tracking code in magento website

magento-1.7

The brief procedure, How to add e-commerce tracking code in Magento website.

Best Answer

What We'll Cover

Enabling E-Commerce Tracking Tracking Codes In order to properly track E-Commerce data using Google Analytics, you need to install your own SSL Certificate.

Enabling E-Commerce Tracking

1. Login to your Google Analytics account.

2. Open the site for which you want to enable e-commerce tracking, and click on it.

3. Click Admin on the navigation bar.

4. Select View Settings in the right column.

5. Set the default page to index.php.

6. Switch Ecommerce tracking to the On position.

7. Click Save.

E-Commerce Tracking Code

Paste the following into your Bigcommerce Control Panel under Setup & Tools › Web Analytics.

Be sure to replace UA-XXXXXXX-X and yourdomain.com with your own number and domain in the following codes.

Universal Analytics

If your Google Analytics profile is using Universal Analytics use this code.

How can I tell if I am using Universal 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-xxxxxxxx-x', 'yourdomain.com');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');

function trackEcommerce() {
this._addTrans = addTrans;
this._addItem = addItems;
this._trackTrans = trackTrans;
}
function addTrans(orderID,store,total,tax,shipping,city,state,country) {
ga('ecommerce:addTransaction', {
    'id': orderID,
    'affiliation': store,
    'revenue': total,
    'tax': tax,
    'shipping': shipping,
    'city': city,
    'state': state,
    'country': country
});
}
function addItems(orderID,sku,product,variation,price,qty) {
ga('ecommerce:addItem', {
    'id': orderID,
    'sku': sku,
    'name': product,
    'category': variation,
    'price': price,
    'quantity': qty
});
}
function trackTrans() {
    ga('ecommerce:send');
}
var pageTracker = new trackEcommerce();
</script>

Standard(async) Analytics

If your Google Analytics profile is using Standard Analytics use this code.

How can I tell if I am using Standard Analytics?

<script type="text/javascript"> 
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-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);
})();

function old2new() {
    this._addTrans = addTrans;
    this._addItem = addItem;
    this._trackTrans = trackTrans;
}

function addTrans(orderID,store,total,tax,shipping,city,state,country) {
    _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
    _gaq.push(['_addTrans',
        orderID,
        store,
        total,
        tax,
        shipping,
        city,
        state,
        country
    ]);
}

function addItem(orderID,sku,product,variation,price,qty) {
    _gaq.push(['_addItem',
        orderID,
        sku,
        product,
        variation,
        price,
        qty
    ]);
}

function trackTrans() {
    _gaq.push(['_trackTrans']);
}

var pageTracker = new old2new();
</script>