Duplicate transactions in Google Analytics are among the most common errors in implementing electronic commerce in Google Analytics.

This error leads to increasing actual revenues from advertising channels, which leads to incorrect conclusions about their efficiency.

Why does data duplication occur?

  1. When the EE code is triggered more than once. When the thank you page with order details is reloaded. The customer may visit it more than once. For example, he might use an email with order details or when the page refreshes while the browser reloads.

  2. Your website logic not so frequently comes across transaction IDs. You need to fix it on the backend level.

How to identify duplicate transactions? 

The simplest way to identify duplicated transactions is to make a custom report with two fields: transactionId and transactions.

To make this report, you can follow the link and automatically add it to your account. Or you can create a custom report with the following parameters.

Fixing duplicated transactions via Google Tag Manager

In our case duplicated transactions came from one device. To solve this issue, we decided to augment the solution that we found on the blog called LunaMetrics. We liked the idea, but it turned out there is no need to involve a developer.

The essence of the method

We have prepared the script which is activated when the electronic commerce data are sent, and performs two functions:

  1. It saves the transaction ID to the browser cookies. Thus, the user browser always saves the last transaction identifier.

  2. When the transaction ID is saved to cookies, it checks if this is a unique transaction. Depending on the answer, a user event is sent: newTransaction or duplicateTransaction.

The transaction ID check is made as follows: the transaction ID we are planning to put down in cookie files is compared to the transaction ID which is already in cookies.

If a transaction is unique, then the tag responsible for sending the transaction data to Google Analytics should be activated in Google Tag Manager.

For that you need to create:

  • transaction_id — data level variable.

  • Transaction Checker — custom html tag.

  • newTransaction — trigger — custom event.

DataLayer TransactionId 

It returns the number of the order transaction.

The value: ecommerce.purchase.actionfield.id
This variable will put the transaction ID to the javascript code responsible for writing this transaction to the user cookie.

Transaction Checker

 Create a custom tag. Place the following code in it (please mind that it refers to the variable transaction_id which was created at the first stage):

<script type="text/javascript">
function checkCookies() {
    var cookievalue = "";
    var cname = "_transactionId=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++){
      var ck = ca[i].trim().toString();
      if (ck.indexOf(cname)==0) {
          cookievalue = ck.substring(cname.length).toString();
          break;
      };
    };

    if ("{{transaction_id}}" != "") {
        if (!cookievalue || cookievalue != "{{transaction_id}}") {
            var d = new Date();
            d.setTime(d.getTime() + (365 * 2 * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toGMTString();
            document.cookie = cname + "{{transaction_id}}; " + expires;
            dataLayer.push({ 'transactionType': 'new' });
            dataLayer.push({'event':'newTransaction'});
        } else {
            dataLayer.push({ 'transactionType': 'duplicate' });
          	dataLayer.push({'event':'duplicateTransaction'});

        }
        dataLayer.push({ 'event': 'transaction' });
    }
};
checkCookies();
</script>

Set a condition that shows the electronic commerce data on a page if delivered as a trigger for tag execution.

That is, the data layer containing the order data, including transaction_id, should be initialized before the Transaction Checker tag.

In our case, this is the “thank you” page.

NewTransaction trigger 

You need to refresh the response trigger in the tag responsible for sending transactions to Google Analytics. It should only be executed if the tag Transaction Checker returned newTransaction as a response.

Thus, only unique transaction IDs will be submitted to Google Analytics.

To do that, you need to go to triggers and create a new one:

Set Custom Event as the trigger type and specify newTransaction as the value.

You can also create an additional trigger and use duplicateTransaction as the value. This trigger can later be used to count duplicated transactions. For instance, via events.

Please keep in mind:

  1. If a user clears their cookies, and after that, the code sends transaction responses again — this transaction is duplicated.

  2. Google Analytics is filtering duplicated transactions, which happen within one session. This means that if for some reason your code responds twice in a row, this will not lead to transaction duplication.