Duplicate Google Analytics Transactions

I am using Universal Analytics on the 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\ 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\ Shirts',    // Product Category
       price: '12.99',              // Price
    quantity: '2'                   // Quantity
});

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

      

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

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

Is this the expected behavior as it is not documented? Does the GA team really expect every user to write code to avoid duplication?

+3


source to share


2 answers


This is expected behavior. This allows negative transactions to be sent to cancel the purchase. You need to change the page code to not enable ecommerce tracking on reload.



+1


source


We actually wrote some javascript that will buy the transaction ID and prevent it from being sent to Google twice. Avoids having to start writing server-side code or changing code on your chosen ecommerce platform.

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



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

<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>

      

+1


source







All Articles