4

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 documented? Do the GA team really expect every user to have to write code to prevent duplications?

2

There are two possible solutions to this problem:

1) Use a server side script to prevent the tracking code to be shown multiple times. This could be done by checking if the transaction information made it into the database or not. OR You can try redirecting the user away from the receipt page after the ecommerce info has been sent to Google Analytics, then preventing the user from returning to that page.

2) Use browser cookies and transaction timestamps to detect duplicates

Check out LunaMetric's Duplicate Transactions in Google Analytics – The Check and the Fix post.

0
1

Set up an intermediate page and once the transaction is sent change the windows location. The intermediate page will not appear in the browser back button history so the user will not be able to easily reload the page.

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

  $(document ).ready(function() {
      window.location = "/order-confirmed?orderReference=#{order.reference}";
    });

This is much easier than setting up a cookie or trying prevent page reloading.

2
  • No idea why but this isnt recording all the transactions! – DD. Nov 16 '14 at 0:33
  • I would hypothesize that the solution as written has big potential for a race condition. ga may not be done initializing and sending data off by the time document.ready() hits. window.location change should only be done in ga "completed" callback. – lkraav Sep 6 '15 at 18:01
0

Simple JQuery Cookie Solution.

  if (!$.cookie('myOrderReference')){
//insert analytics code here

       $.cookie('myOrderRefernce', true);  

 }

You should setup your script to dynamically insert the order reference instead of 'myOrderReference'.

However, if a user loads the order confirmation page into a different browser a duplicate will still occur.

1
  • A much better idea, would be, on order completion, to send a cookie from the server side. On the client side you should check if such cookie exists, and if so delete it and send the order to analytics. This would ensure uniqueness fairly well. – tacone Nov 19 '15 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.