Created
May 21, 2015 01:03
-
-
Save pugsbaker/c06eca5ec410986134d8 to your computer and use it in GitHub Desktop.
Shopify Segment init
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
window.ShopifyAnalytics = window.ShopifyAnalytics || {}; | |
window.ShopifyAnalytics.meta = window.ShopifyAnalytics.meta || {}; | |
window.ShopifyAnalytics.meta.currency = 'USD'; | |
(window.gaDevIds=window.gaDevIds||[]).push('BwiEti'); | |
(function () { | |
var customDocumentWrite = function(content) { | |
var jquery = null; | |
if (window.jQuery) { | |
jquery = window.jQuery; | |
} else if (window.Checkout && window.Checkout.$) { | |
jquery = window.Checkout.$; | |
} | |
if (jquery) { | |
jquery('body').append(content); | |
} | |
}; | |
var analytics = document.createElement('script'); | |
var loaded = false; | |
var onload = function() { | |
if (loaded) return; | |
loaded = true; | |
window.ShopifyAnalytics.lib = window.analytics.noConflict(); | |
window.ShopifyAnalytics.lib.initialize({"Google Analytics":{"trackingId":"UA-33595068-1","domain":"auto","siteSpeedSampleRate":"10","enhancedEcommerce":true,"doubleClick":true,"includeSearch":true}}); | |
window.ShopifyAnalytics.lib.page( | |
null, | |
{} | |
); | |
window.ShopifyAnalytics.lib.ready(function() { | |
ga('require', 'linker'); | |
function addListener(element, type, callback) { | |
if (element.addEventListener) { | |
element.addEventListener(type, callback); | |
} | |
else if (element.attachEvent) { | |
element.attachEvent('on' + type, callback); | |
} | |
} | |
function decorate(event) { | |
event = event || window.event; | |
var target = event.target || event.srcElement; | |
if (target && (target.action || target.href)) { | |
ga(function (tracker) { | |
var linkerParam = tracker.get('linkerParam'); | |
document.cookie = '_shopify_ga=' + linkerParam + '; ' + 'path=/'; | |
}); | |
} | |
} | |
addListener(window, 'load', function(){ | |
for (var i=0; i < document.forms.length; i++) { | |
if(document.forms[i].action && document.forms[i].action.indexOf('/cart') >= 0) { | |
addListener(document.forms[i], 'submit', decorate); | |
} | |
} | |
for (var i=0; i < document.links.length; i++) { | |
if(document.links[i].href && document.links[i].href.indexOf('/checkout') >= 0) { | |
addListener(document.links[i], 'click', decorate); | |
} | |
} | |
}); | |
var originalDocumentWrite = document.write; | |
document.write = customDocumentWrite; | |
try { } catch(e) {}; | |
document.write = originalDocumentWrite; | |
}); | |
} | |
analytics.onload = onload; | |
analytics.onreadystatechange = function() { | |
if (!/complete|loaded/.test(analytics.readyState)) return; | |
onload(); | |
} | |
analytics.async = true; | |
analytics.src = "//cdn.shopify.com/s/assets/vendor/analytics-123fde9c0c8e765baf6cd4171639dda8.js"; | |
document.getElementsByTagName('head')[0].appendChild(analytics); | |
var eventsListenerScript = document.createElement('script'); | |
eventsListenerScript.async = true; | |
eventsListenerScript.src = "//cdn.shopify.com/s/assets/shop_events_listener-039aa29e52993d39dcbb917712c30dc7.js"; | |
document.getElementsByTagName('head')[0].appendChild(eventsListenerScript); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment