How to reverse transaction in Google Analytics for gtag.js and analytics.js

Do you want expert help in setting up/fixing GA4 and GTM?

If you are not sure whether your GA4 property is setup correctly or you want expert help migrating to GA4 then contact us. We can fix your website tracking issues.

In this article, I will show you how to reverse transactions in Google Analytics both for gtag.js and analytics.js libraries.

You can fix test orders and cancelled orders in Google Analytics by reversing an ecommerce transaction.

Google Analytics does not automatically reverse an ecommerce transaction, in the event the transaction is later declined or cancelled.

So if someone has placed an order on your website and later cancelled the order or his order did not go through for some reason, Google Analytics will still report that order and the corresponding sale generated from that order.

Introduction to reversal transaction

In order to maintain the accuracy of your ecommerce data in Google Analytics Ecommerce reports, you should manually reverse an ecommerce transaction in Google Analytics.

The transaction that is reversed is known as a reversal transaction.

There is a common misconception that reversing a transaction means removing the transaction. But unfortunately, this is not true.

When you reverse a transaction in Google Analytics, it does not remove it. GA simply deduct a particular amount from the revenue total. So what is removed is the revenue/sales associated with the reversal transaction. No other information associated with the transaction is removed.

For example,

  • User ‘A’ placed an order of $200 on Monday.
  • Total website sales on Monday were $500.

Now if you reverse the transaction on Monday, then the total sales amount for Monday would be reported to be: $500 – $200 = $300

If you reverse the transaction on say Tuesday, then

the total sales amount for Monday would remain $500

the total sales amount for Tuesday would be: Total website sales for Tuesday – $200

So if the original transaction occurred on Monday and you reversed it on Tuesday then make sure that you are analyzing a date range that includes both Monday and Tuesday.

Google, in fact, recommends doing the reversal on the same day as the original transaction.

Note: Both the original and reversal transaction will be included and reported in the transaction count.

Reversing an ecommerce transaction if you use gtag.js

If you use the gtag.js JavaScript library then follow the steps below to reverse an ecommerce transaction:

Step-1: Create and load a duplicate of your order confirmation page (receipt page) and name it ‘order cancellation page‘ for easy reference.

Step-2: Update the ecommerce tracking code on the ‘order cancellation’ page.

Follow the steps below:

#2.1 Add negative values for the following transaction fields:

  1. revenue
  2. shipping
  3. tax

For example:

gtag(‘event’, ‘purchase’, {
“transaction_id”: “1234”,
“affiliation”: “Acme Clothing”,
“value”: -11.99, // Negative revenue
“currency”: “USD”,
“tax”: -1.29, // Negative tax
“shipping”: -5, // Negative shipping

#2.2 Add negative value for the ‘quantity’ item field. But keep the ‘price’ item field positive.

For example:

“items”: [
{
“id”: “1234”,
“name”: “Fluffy Pink Bunnies”,
“list_name”: “Search Results”,
“brand”: “ToysRus”,
“category”: “Party Toys”,
“variant”: “Black”,
“list_position”: 1,
“quantity”: -1, // Negative Quantity.
“price”: ‘11.99’ // Positive Unit price.
},

So if following was your original transaction:

gtag('event', 'purchase', {
"transaction_id": "1234",
"affiliation": "Acme Clothing",
"value": 11.99,
"currency": "USD",
"tax": 1.29,
"shipping": 5,

"items": [
{
"id": "1234",
"name": "Fluffy Pink Bunnies",
"list_name": "Search Results",
"brand": "ToysRus",
"category": "Party Toys",
"variant": "Black",
"list_position": 1,
"quantity": 1,
"price": '11.99'
},

Then your reversal transaction may look like the one below:

gtag('event', 'purchase', {
"transaction_id": "1234",
"affiliation": "Acme Clothing",
"value": -11.99, // Negative revenue
"currency": "USD",
"tax": -1.29, // Negative tax
"shipping": -5, // Negative shipping

"items": [
{
"id": "1234",
"name": "Fluffy Pink Bunnies",
"list_name": "Search Results",
"brand": "ToysRus",
"category": "Party Toys",
"variant": "Black",
"list_position": 1,
"quantity": -1, // Negative Quantity.
"price": '11.99' // Positive Unit price.
},

Note: Do not use the code mentioned above. This is just an example code and not the complete code.

Do you want expert help in setting up/fixing GA4 and GTM?

If you are not sure whether your GA4 property is setup correctly or you want expert help migrating to GA4 then contact us. We can fix your website tracking issues.

Reversing an ecommerce transaction if you use analytics.js

If you use the analytics.js JavaScript library then follow the steps below to reverse an ecommerce transaction:

Step-1: Create and load a duplicate of your order confirmation page (receipt page) and name it ‘order cancellation page‘ for easy reference.

Step-2: Update the ecommerce tracking code on the ‘order cancellation’ page.

Follow the steps below:

#2.1 Add negative values for the following transaction fields:

  1. revenue
  2. shipping
  3. tax

For example:

ga(‘ecommerce:addTransaction’, {
‘id’: ‘1234’, // Transaction ID. Required. Same as original transaction
‘affiliation’: ‘Acme Clothing’, // Affiliation or store name.
‘revenue’: ‘-11.99’, // Negative revenue
‘shipping’: ‘-5’, // Negative shipping
‘tax’: ‘-1.29’ // Negative tax
});

#2.2 Add a negative value for the ‘quantity’ item field. But keep the ‘price’ item field positive.

For example:

ga(‘ecommerce:addItem’, {
‘id’: ‘1234’, // Transaction ID. Required.
‘name’: ‘Fluffy Pink Bunnies’, // Product name. Required.
‘sku’: ‘DD23444’, // SKU/code.
‘category’: ‘Party Toys’, // Category or variation.
‘price’: ‘11.99’, // Positive Unit price.
‘quantity’: ‘-1’ // Negative Quantity.
});

So if the following was your original transaction:

ga('ecommerce:addTransaction', {
'id': '1234', // Transaction ID. Required.
'affiliation': 'Acme Clothing', // Affiliation or store name.
'revenue': '11.99', // Grand Total.
'shipping': '5', // Shipping.
'tax': '1.29' // Tax.
});

ga('ecommerce:addItem', {
'id': '1234', // Transaction ID. Required.
'name': 'Fluffy Pink Bunnies', // Product name. Required.
'sku': 'DD23444', // SKU/code.
'category': 'Party Toys', // Category or variation.
'price': '11.99', // Unit price.
'quantity': '1' // Quantity.
});

Then your Reversal transaction may look like the one below:

ga('ecommerce:addTransaction', {
'id': '1234', // Transaction ID. Required. Same as original transaction
'affiliation': 'Acme Clothing', // Affiliation or store name.
'revenue': '-11.99', // Negative revenue
'shipping': '-5', // Negative shipping
'tax': '-1.29' // Negative tax
});

// Use the ecommerce:addItem object of the original transaction
ga('ecommerce:addItem', {
'id': '1234', // Transaction ID. Required.
'name': 'Fluffy Pink Bunnies', // Product name. Required.
'sku': 'DD23444', // SKU/code.
'category': 'Party Toys', // Category or variation.
'price': '11.99', // Positive Unit price.
'quantity': '-1' // Negative Quantity.
});

Important points about reversing an ecommerce transaction

Keep the following points in mind before you reverse an ecommerce transaction, regardless of the JavaScript library (gtag.js or analytics.js) you use:

  1. Use the transaction ID (order ID) of the original transaction, as the transaction ID for the reversal transaction.
  2. Use negative values for the following transaction fields: ‘revenue’, ‘shipping’ and ‘tax’.
  3. Use negative value for the ‘quantity’ item field. But keep the ‘price’ item field positive.
  4. Do the reversal on the same day as the original transaction.
  5. Whenever you do a reversal on a date different than the original transaction date, annotate that reversal transaction date via the GA annotation feature.
  6. If the original transaction occurred on say January 4 and you reversed it on January 7 then make sure that you are analyzing a date range that includes both dates.
  1. Google Analytics Ecommerce Tracking Tutorial
  2. Enhanced Ecommerce Tracking in Google Analytics – Tutorial
  3. Set up Enhanced Ecommerce Tracking in Google Analytics
  4. Using Enhanced Ecommerce segments for Remarketing in Google Analytics
  5. Using Cohort Analysis & Enhanced Ecommerce to Understand User Behavior
  6. Duplicate Transactions (orders) in Google Analytics
  7. Enhanced Ecommerce Tracking via Google Tag Manager
  8. Ecommerce Tracking Google Tag Manager (GTM) – Tutorial
  9. Shopify Enhanced Ecommerce Tracking in Google Analytics – Tutorial
  10. GA4 (Google Analytics 4) Ecommerce Tracking via GTM – Tutorial
  11. Why Google Analytics and Shopping Cart Sales data don’t match and how to fix it
  12. How to remove / modify Google Analytics ecommerce transaction in one click
  13. Tracking Google Analytics Paypal Referral and other payment gateways
  14. Subscription & Recurring Revenue Analytics in Google Analytics
  15. Understanding Google Analytics Product Data Import
  16. Shopping Cart Analytics Tutorial
  17. Learn To Read E-Commerce Reports In Google Analytics
  18. Dealing with Google Analytics Refund – Reverse Transaction
  19. How to Fix Missing Ecommerce Data in Google Analytics

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade