: Duplicate transactions in Google Analytics caused by page refreshes I'm using Universal Analytics on my order confirmation page: // Create the tracker ga('create', 'UA-XXXXX-Y'); // Fire off a pageview
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?
More posts by @Ravi8258870
3 Comments
Sorted by latest first Latest Oldest Best
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.
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.
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.