Last active
August 16, 2021 21:31
-
-
Save baumicon/3a8e6b04acd8f7a59cdc081fa2edf1df to your computer and use it in GitHub Desktop.
Customized Google Tag Manager Transaction Event tracking with BigCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
window.dataLayer = window.dataLayer || []; | |
var transaction = {}; | |
transaction.transactionProducts = []; | |
function addTrans(orderID, store, total, tax, shipping, city, state, country) { | |
transaction.transactionId = orderID; | |
transaction.transactionAffiliation = store; | |
transaction.transactionTotal = parseFloat(total); | |
transaction.transactionTax = parseFloat(tax); | |
transaction.transactionShipping = parseFloat(shipping); | |
} | |
function addItems(orderID, sku, product, variation, price, quantity) { | |
transaction.transactionProducts.push({ | |
'id': orderID, | |
'sku': sku, | |
'name': product, | |
'category': variation, | |
'price': parseFloat(price), | |
'quantity': quantity | |
}); | |
} | |
function trackTrans() { | |
transaction.event = 'bcTransactionComplete'; | |
dataLayer.push(transaction); | |
} | |
function trackGTMEcommerce() { | |
this._addTrans = addTrans; | |
this._addItem = addItems; | |
this._trackTrans = trackTrans; | |
} | |
var pageTracker = new trackGTMEcommerce(); | |
var order = {} | |
getOrderDetail().then(data => { | |
order = data | |
if(typeof(pageTracker) != 'undefined') { | |
pageTracker._addTrans( | |
order.orderId, | |
location.hostname, | |
order.baseAmount, | |
order.taxTotal, | |
order.shippingCostTotal, | |
order.billingAddress.city, | |
order.billingAddress.state, | |
order.billingAddress.country, | |
'USD', | |
{id: '0', name: 'default', type: 'storefront'} | |
); | |
bcItems = order.lineItems.physicalItems; | |
for (var i = 0, len = bcItems.length; i < len; i++) { | |
bcItem = bcItems[i]; | |
pageTracker._addItem( | |
order.orderId, | |
bcItem.sku, | |
bcItem.name, | |
bcItem.variantId, | |
bcItem.salePrice, | |
bcItem.quantity | |
); | |
} | |
pageTracker._trackTrans(); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment