Google-Analytics-Logo

Why?

Suppose a scenario where online customer made a purchase from an E-Commerce site and Google Analytics tracked and included this into Ecommerce transaction report for the site. Now if the customer canceled the order then we need to make sure that Google Analytics report should reflect this -ve transaction also. So that GA Ecommerce Transaction report display the actual transactions and relevent data like amount etc.

How?

Here is the process to cancel a transaction from Google Analytics. First we need to create a duplicate page for cancellation of order (This may be Order Cancellation Confirmation page or something similar). Now we can use the same JS, which we had used to report a new transaction, but with following limitations :

  • Use the same transaction ID for the transaction and item form data as was used for the purchase.
  • The Total field is negative.
  • There is a positive per unit price and a negative quantity.
  • If any tax or delivery charges are being credited back they should be shown as negative values in the respective fields in the transaction form data.

Here is an example:
If your original transaction was submitted as follows:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

var pageTracker = _gat._getTracker("UA-XXXXX-1");
pageTracker._initData();
pageTracker._addTrans(
	"[1212]", // Order ID
	"[Test Shop]", // Affiliation
	"[10.49]", // Total
	"[3.29]", // Tax
	"[4]", // Shipping
	"[Miami]", // City
	"[Florida]", // State
	"[USA]" // Country );

pageTracker._addItem(
	"[1234]", // Order ID
	"[PRO12]", // SKU
	"[Notebook]", // Product Name
	"[Stationary]", // Category
	"[10.49]", // Price
	"[1]" // Quantity );

pageTracker._trackTrans();
</script>

Then, the code to remove the transaction needs to be set as follows:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

var pageTracker = _gat._getTracker("UA-XXXXX-1");
pageTracker._initData();
pageTracker._addTrans(
 "[1212]", // Order ID
 "[Mountain View]", // Affiliation
 "[-10.49]", // Total
 "[-3.29]", // Tax
 "[-4]", // Shipping
 "[Miami]", // City
 "[Florida]", // State
 "[USA]" // Country );

pageTracker._addItem(
 "[1234]", // Order ID
 "[PRO12]", // SKU
 "[Notebook]", // Product Name
 "[Stationary]", // Category
 "[10.49]", // Price
 "[-1]" // Quantity );

pageTracker._trackTrans();
</script>

Important:

You will still be able to see the actual transaction and the duplicate negative transaction when you select the day on which these transactions were recorded. However, when you select a date range that includes both the original and the negative transaction, the transaction will not be shown in the report. The negative item quantity should result in the same effect in the products reports.

 

– Abhishek Singh