Skip to content

Instantly share code, notes, and snippets.

@WesleyDRobinson
Last active October 25, 2019 23:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WesleyDRobinson/2b3812e735671383c6e23c446ce8ea0a to your computer and use it in GitHub Desktop.
Save WesleyDRobinson/2b3812e735671383c6e23c446ce8ea0a to your computer and use it in GitHub Desktop.
Please see comments for more up to date implementations!!
<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>
@JimmyCouch
Copy link

Thanks for the integration code!

I'm currently using this as a part of segment/shopify integration, and i'm seeing an error.

I'm not sure when Shopify made made this change, but order.discounts can't be converted into a json string. As a result from line 34, I get something like "object cannot be filtered to json"

This essentially breaks any additional scripts on the order confirmation page. I found this out when I noticed a discrepancy from the data that segment is passing to Google Analytics, and that orders which had a discount code wouldn't appear.

I recommend using the liquid object discounts_amount see documentation here

For line 47, I'm currently using this instead: discount: {{ discounts_amount | money_without_currency}},

@litch
Copy link

litch commented Jun 10, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment