Skip to content

Instantly share code, notes, and snippets.

@gtempesta
Last active August 10, 2020 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gtempesta/0576d2e25980303595b1689103e62a2a to your computer and use it in GitHub Desktop.
Save gtempesta/0576d2e25980303595b1689103e62a2a to your computer and use it in GitHub Desktop.
Reverse Enhanced Ecommerce Transaction in Google Analytics
// Reverse purchase in Google Analytics with Enhanced Ecommerce
// Source: https://support.google.com/analytics/answer/1037443?hl=en
var items = [
{
id: 573, // original ID
price: "57000.00", // original price
quantity: "-2500" // negative quantity
},
{
id: 573,
price: "95181.83",
quantity: "-2500"
},
{
id: 573,
price: "163363.64",
quantity: "-2500"
},
{
id: 550,
price: "64119.00",
quantity: "-2500"
},
{
id: 550,
price: "123288.00",
quantity: "-2500"
},
{
id: 555,
price: "143907.84",
quantity: "-2500"
},
{
id: 550,
price: "249147.59",
quantity: "-2500"
}
];
// loop through items
for(var i = 0; i < items.length; i++){
var product = items[i];
// add single items
ga('ec:addProduct', {
'id':product['id'],
'price': product['price'],
'quantity': product['quantity']
});
}
// add the whole purchase
ga('ec:setAction', 'purchase', {
'id': "6425422", // original transaction id
'revenue': "-896007.75", // negative total
'tax': "0", // taxes included in the revenue so it's ok to have them at 0
'shipping': "0" // free shipping
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment