DD.
DD.

Reputation: 22001

Google Analytics Duplicate Transactions

I'm using Universal Analytics on my order confirmation page:

// Create the tracker
ga('create', 'UA-XXXXX-Y');

// Fire off a pageview
ga('send', 'pageview');

// Include the ecommerce plugin
ga('require', 'ecommerce', 'ecommerce.js');

// Initialize the transaction
ga('ecommerce:addTransaction', {
             id: '1234abc',     // Transaction ID*
    affiliation: 'Tech Shirts', // Store Name
        revenue: '52.19',       // Total
       shipping: '10',          // Shipping
            tax: '3.22'         // Tax
});

// Add a few items
ga('ecommerce:addItem', {
          id: '1234abc',            // Transaction ID*
         sku: 'TSHIRT-12A',         // Product SKU
        name: 'Analytics Wizard',   // Product Name*
    category: 'Men\'s Shirts',      // Product Category
       price: '12.99',              // Price
    quantity: '1'                   // Quantity
});
ga('ecommerce:addItem', {
          id: '1234abc',            // Transaction ID*
         sku: 'TSHIRT-36B',         // Product SKU
        name: 'Best Developer',     // Product Name*
    category: 'Women\'s Shirts',    // Product Category
       price: '12.99',              // Price
    quantity: '2'                   // Quantity
});

// Send off the transaction
ga('ecommerce:send');

For some reason the analytics team have decided to record the same transaction twice if the user refreshes the page.

It seems illogical to record the same transaction twice given that transaction ID is the same (it obviously represents the same transaction so why duplicate it?).

Is this expected behaviour as it is not documenated? Do the GA team really expect every user to have to write code to prevent duplications?

Upvotes: 4

Views: 5128

Answers (2)

Matt Bradley
Matt Bradley

Reputation: 19

We actually wrote a little bit of javascript which cookies the transaction ID and prevents it being sent to Google twice. Avoids the need to start writing server side code or modifying the code in your chosen ecommerce platform.

http://www.inventpartners.com/eliminate-duplicate-transactions-in-google-analytics

Essentially, just include the bit of JS and then wrap a conditional around your GA transaction tracking code:

<script type="text/javascript">
function checkIsSent(ref) {
    var cookiename = 'gaSaleSent_' + ref;

    var ca = document.cookie.split(';');

    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(cookiename + '=') != -1){
            return true;
        }
    }

    var d = new Date();
    d.setTime(d.getTime() + (90*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cookiename + '=1; ' + expires;

    return false;
}

if(!checkIsSent('YOUR CART ID HERE')){
      PASTE YOUR GOOGLE TRANSACTION TRACKING CODE HERE
}
</script>

Upvotes: 1

kevintechie
kevintechie

Reputation: 1521

This is expected behavior. This enables you to send negative transactions to cancel a purchase. You need to modify the page code to not include the e-commerce tracking on reload.

Upvotes: 1

Related Questions