Created
February 5, 2018 03:26
-
-
Save toddheslin/4a20213c1f5934c61b084132987d5aa8 to your computer and use it in GitHub Desktop.
Shopify GA 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
// Our custom GA additional scripts module | |
var additionalGA = { | |
host: window.location.hostname, | |
path: window.location.pathname.split('/'), | |
init: function() { | |
var isCheckout = this.isCheckout(); | |
isCheckout ? this.loadSegment() : null; | |
//isCheckout ? null : this.ready(this.gaLinker); | |
}, | |
isCheckout: function(){ | |
// if (this.host === 'checkout.shopify.com') { | |
// return true | |
// } else | |
if (this.path[1] === '15733025') { | |
return true | |
} else { | |
return false | |
} | |
}, | |
ready: function(fn) { | |
if (document.readyState != 'loading'){ | |
fn(); | |
} else { | |
document.addEventListener('DOMContentLoaded', fn); | |
} | |
}, | |
loadSegment: function() { | |
!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","reset","group","track","ready","alias","debug","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="4.0.0"; | |
window.analytics.load('fZCJQR00sIALmSwOVTLkNR8caz4gsBaD'); | |
window.analytics.page(); | |
window.analytics.track('Order Progressed', { step : window.Shopify.Checkout.step }); | |
}}(); | |
}, | |
gaLinker: function() { | |
typeof window.ShopifyAnalytics.links == 'undefined' ? window.ShopifyAnalytics.links = [] : null; | |
var linksToDecorate = []; | |
linksToDecorate.push(document.querySelector('.slideshow__heading-cta')); | |
linksToDecorate.push(document.querySelector('#footer-buy')); | |
linksToDecorate.push(document.querySelector('#footer-buy-two')); | |
linksToDecorate.push(document.querySelector('.navigation__links >li.navigation__entrance-animation:first-child > a')); | |
// Two footers for some reason | |
Array.prototype.forEach.call(document.querySelectorAll('.site-footer__linklist >li:first-child > a'), function (node) { | |
linksToDecorate.push(node) | |
}); | |
linksToDecorate.forEach(function(link) { | |
try { | |
link.addEventListener('click', function() { | |
ga('linker:decorate', link); | |
dataLayer.push({ | |
event: 'clickedBuyButton', | |
button: '#' + link.id + '.' + link.className | |
}) | |
}); | |
window.ShopifyAnalytics.links.push(link); | |
} catch(e) {} | |
}); | |
} | |
} | |
// Call init function | |
additionalGA.init(); |
@prakashmalviya I wish I could help you but this is a very old script I don't use anymore.
I believe Shopify actually has hidden the 'additional scripts' for new customers anyway.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How exactly we can use this script on the checkout page?