Skip to content

Instantly share code, notes, and snippets.

@MFRWDesign
Created January 30, 2018 00:43
Show Gist options
  • Save MFRWDesign/7ac45f490032714295d82bb3d9f74a55 to your computer and use it in GitHub Desktop.
Save MFRWDesign/7ac45f490032714295d82bb3d9f74a55 to your computer and use it in GitHub Desktop.
Solidus Multi-tenant Google Analytics Tracker
<% if tracker = Spree::Tracker.current(current_store) %>
<script id="solidus_trackers">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '<%= tracker.analytics_id %>', 'auto' {'name': '<%= current_store.name %>'});
ga('<%= current_store.name %>.require', 'displayfeatures');
ga('<%= current_store.name %>.send', 'pageview');
<% if @order && order_just_completed?(@order) %>
<%# more info: https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce %>
ga('<%= current_store.name %>.require', 'ecommerce', 'ecommerce.js');
ga('<%= current_store.name %>.ecommerce:addTransaction', {
'id': '<%= j @order.number %>', // Transaction ID. Required.
'affiliation': '<%= current_store.name %>', // Affiliation or store name.
'revenue': '<%= @order.total %>', // Grand Total.
'shipping': '<%= @order.ship_total %>', // Shipping.
'tax': '<%= @order.tax_total %>', // Tax.
'currency': '<%= @order.currency %>' // local currency code.
});
<% @order.line_items.each do |line_item| %>
ga('<%= current_store.name %>.ecommerce:addItem', {
'id': '<%= j @order.number %>', // Transaction ID. Required.
'name': '<%= j line_item.variant.product.name %>', // Product name. Required.
'sku': '<%= j (line_item.variant.sku || line_item.variant_id) %>', // SKU/code.
'category': '', // Category or variation.
'price': '<%= line_item.price %>', // Unit price.
'quantity': '<%= line_item.quantity %>' // Quantity.
});
<% end %>
ga('<%= current_store.name %>.ecommerce:send');
<% end %>
</script>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment