Last active
October 25, 2019 23:58
-
-
Save WesleyDRobinson/2b3812e735671383c6e23c446ce8ea0a to your computer and use it in GitHub Desktop.
Please see comments for more up to date implementations!!
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 type="text/javascript"> | |
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.0.1"; | |
window.analytics.load(""); | |
// identify the customer if they have an account | |
{% if customer.id %} | |
window.analytics.identify("{{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: ["{{ customer.tags | join: '", "' }}"] | |
}); | |
{% endif %} | |
window.analytics.page('Checkout Page'); | |
}}(); | |
// calculate totalDiscount for Completed Order Event | |
var discounts = "{{ order.discounts | json }}" | |
var totalDiscount = 0; | |
for (var i = 0; i< discounts.length; i++ ) { | |
totalDiscount += discounts[i].savings; | |
} | |
analytics.track('Completed Order', { | |
orderId: "{{ order_number }}", | |
total: "{{ total_price | money_without_currency }}", | |
revenue: "{{subtotal_price | money_without_currency }}", | |
shipping: "{{shipping_price | money_without_currency }}", | |
tax: "{{tax_price | money_without_currency }}", | |
discount: totalDiscount, | |
products: [ | |
{% for line_item in line_items %} | |
{ | |
id: "{{ line_item.product_id }}", | |
sku: "{{ line_item.sku }}", | |
name: "{{ line_item.title }}", | |
price: "{{ line_item.price }}", | |
quantity: "{{ line_item.quantity }}" | |
} | |
{% endfor %} ] | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this and the comment by @JimmyCouch! I've just gone through and added Segment to my store and found a number of bugs in the existing documented solutions, so have updated them some here:
https://gist.github.com/litch/568bc224ddb28507f90ea7081c5776fd