Created
February 5, 2018 03:31
-
-
Save toddheslin/06721cd1a647f472250b88e9e0d838fc to your computer and use it in GitHub Desktop.
Shopify Checkout Additional Scripts
This file contains 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 || []; | |
function _ready(fn) { | |
if (document.readyState != 'loading') { | |
fn(); | |
} else { | |
document.addEventListener('DOMContentLoaded', fn); | |
} | |
} | |
_ready(function() { | |
/* | |
* Module will be immediately executed | |
*/ | |
var totalDiscount = 0, | |
transaction = {}, | |
customer = {}; | |
if ( isNewOrder() ) { | |
transaction = setTransaction(); | |
customer = setCustomer(); | |
sendTransaction(); | |
sendCustomer(); | |
updateTrackingCode(); | |
} else { | |
customer = setCustomer(); | |
sendCustomer(); | |
updateTrackingCode(); | |
} | |
function isNewOrder() { | |
{% if first_time_accessed %} | |
return true; | |
{% else %} | |
return false; | |
{% endif %} | |
} | |
function calculateDiscount() { | |
{% for discount in order.discounts %} | |
try { | |
totalDiscount += {{ discount.savings | times: 0.01 }} | |
} catch(e){} | |
{% endfor %} | |
} | |
function setTransaction() { | |
return { | |
transactionId: '{{ order.order_number }}', | |
transactionTotal: {{ order.total_price | times: 0.01 }}, | |
transactionRevenue: {{ order.subtotal_price | times: 0.01 }}, | |
transactionCurrency: '{{ shop.currency }}' || 'USD', | |
transactionShipping: {{ order.shipping_price | times: 0.01 }}, | |
transactionTax: {{ order.tax_price | times: 0.01 }}, | |
transactionDiscount: calculateDiscount(), | |
transactionProducts: [ | |
{% for line_item in order.line_items %} | |
{ | |
id: '{{ line_item.product_id }}', | |
sku: '{{ line_item.sku }}', | |
name: '{{ line_item.title }}', | |
category: '{{ line_item.type }}', | |
price: {{ line_item.line_price | times: 0.01 }}, | |
quantity: {{ line_item.quantity }} | |
}, | |
{% endfor %} | |
] | |
} | |
} | |
function setCustomer() { | |
return { | |
id: '{{ customer.id }}', | |
name: '{{ customer.name }}', | |
firstName: '{{ customer.first_name }}', | |
lastName: '{{ customer.last_name }}', | |
email: '{{ customer.email }}', | |
phone: '{{ customer.default_address.phone }}', | |
address: { // uses the default address | |
street: '{{ customer.default_address.street }}', | |
city: '{{ customer.default_address.city }}', | |
state: '{{ customer.default_address.province }}', | |
stateCode: '{{ billing_address.province_code }}', | |
postalCode: '{{ customer.default_address.zip }}', | |
country: '{{ customer.default_address.country }}', | |
countryCode: '{{ customer.default_address.country_code }}' | |
}, | |
totalSpent: '{{ customer.total_spent }}', | |
allOrdersCount: '{{ customer.orders_count }}', | |
allOrderIds: [{% for order in customer.orders %}'{{ order.id }}',{% endfor %}], | |
tags: [{% for tag in customer.tags %} '{{ tag }}', {% endfor %}] | |
} | |
} | |
function sendTransaction() { | |
window.dataLayer.push({ event : 'transactionComplete', checkout: function() { return transaction } }); | |
} | |
function sendCustomer() { | |
window.dataLayer.push({ event : 'orderStatus', customer: function() { return customer } }); | |
} | |
function updateTrackingCode() { | |
try { | |
var trackingUrl = document.querySelector('.tracking-info__number p a'); | |
if(trackingUrl !== null){ | |
var trackingCode = trackingUrl.href.split('=')[1]; | |
trackingUrl.href = 'https://auspost.com.au/parcels-mail/track.html#/track?id=' + trackingCode; | |
} | |
} catch (e) { | |
console.log('Order not yet fulfilled'); | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment