Skip to content

Instantly share code, notes, and snippets.

@jfeldstein
Last active August 29, 2015 14:20
Show Gist options
  • Save jfeldstein/6a17ae78bb58bacd28a5 to your computer and use it in GitHub Desktop.
Save jfeldstein/6a17ae78bb58bacd28a5 to your computer and use it in GitHub Desktop.
Attribution Tracking Javascripts
(function(){var t,e,n,r,u,i,o,m,_,a,c,s;n=function(){return{first_referrer:e("first_referrer")||u()}},u=function(){var n,r,u,i,o;return o=document.referrer,n=t(o),r=null==n,i=n===t(document.location),u=null==o||i||r?e("latest_referrer"):n,{latest_referrer:u}},r=function(){var t,n,r,u;if(t={first_utm_name:e("first_utm_source"),first_utm_source:e("first_utm_source"),first_utm_medium:e("first_utm_medium"),first_utm_term:e("first_utm_term"),first_utm_content:e("first_utm_content")},null==t.first_utm_name){u=i();for(n in u)r=u[n],t[n.replace("latest_","first_")]=r}return t},i=function(){var t,n,r,u,i,o;return u=c(),r=u.utm_campaign,i=u.utm_source,n=u.utm_medium,o=u.utm_term,t=u.utm_content,null==r&&(r=e("latest_utm_name"),i=e("latest_utm_source"),n=e("latest_utm_medium"),o=e("latest_utm_term"),t=e("latest_utm_content")),{latest_utm_name:r,latest_utm_source:i,latest_utm_medium:n,latest_utm_term:o,latest_utm_content:t}},c=function(){var t,e;return t=document.location.search.substr(1),e={},t.split("&").forEach(function(t){var n;return n=t.split("="),e[n[0]]=decodeURIComponent(n[1])}),e},t=function(t){var e;return null==t||""===t?null:(0!==String(t).indexOf("http")&&(t="http://"+t),e=document.createElement("a"),e.href=t,e.hostname&&e.hostname.replace(/^www\./i,"").toLowerCase())},o=function(t){var e,n;return e={cookie_domain:document.location},n=a(e,window.attribution_tracking_options),n[t]},s=function(e,n){var r,u;return u=new Date((new Date).getTime()+2592e6),r=o("cookie_domain"),document.cookie=[encodeURIComponent(e),"=",JSON.stringify(n),"; expires="+u.toUTCString(),"; domain="+t(r)].join("")},e=function(t){var e,n;return n="; "+document.cookie,e=n.split("; "+t+"="),2===e.length?e.pop().split(";").shift():null},a=function(t,e){var n,r;null==e&&(e={});for(n in e)r=e[n],t[n]=r;return t},m=function(){var t;return t=function(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)},""+t()+t()+"-"+t()+"-"+t()+"-"+t()+"-"+t()+t()+t()},(_=function(){var t,e,o;t={distinct_analytics_id:m()},a(t,r()),a(t,i()),a(t,n()),a(t,u());for(e in t)o=t[e],s(e,o);return t})()}).call(this);
(function(){var t,n,e,r,i,o,a,u,c,s,m,_,l,d,f;i=function(){return{first_referrer:r("first_referrer")||a().latest_referrer}},a=function(){var n,e,i,o,a;return a=document.referrer,n=t(a),e=null==n,o=n===t(document.location),i=null==a||o||e?r("latest_referrer"):n,{latest_referrer:i}},o=function(){var t,n,e,i;if(t={first_utm_name:r("first_utm_source"),first_utm_source:r("first_utm_source"),first_utm_medium:r("first_utm_medium"),first_utm_term:r("first_utm_term"),first_utm_content:r("first_utm_content")},null==t.first_utm_name){i=u();for(n in i)e=i[n],t[n.replace("latest_","first_")]=e}return t},u=function(){var t,n,e,i,o,a;return i=d(),e=i.utm_campaign,o=i.utm_source,n=i.utm_medium,a=i.utm_term,t=i.utm_content,null==e&&(e=r("latest_utm_name"),o=r("latest_utm_source"),n=r("latest_utm_medium"),a=r("latest_utm_term"),t=r("latest_utm_content")),{latest_utm_name:e,latest_utm_source:o,latest_utm_medium:n,latest_utm_term:a,latest_utm_content:t}},d=function(){var t,n;return t=document.location.search.substr(1),n={},t.split("&").forEach(function(t){var e;return e=t.split("="),n[e[0]]=decodeURIComponent(e[1])}),n},t=function(t){var n;return null==t||""===t?null:(0!==String(t).indexOf("http")&&(t="http://"+t),n=document.createElement("a"),n.href=t,n.hostname&&n.hostname.replace(/^www\./i,"").toLowerCase())},c=function(t){var n,e;return n={cookie_domain:document.location},e=l(n,window.attribution_tracking_options),e[t]},f=function(n,e){var r,i;return i=new Date((new Date).getTime()+2592e6),r=c("cookie_domain"),document.cookie=[encodeURIComponent(n),"=",encodeURIComponent(JSON.stringify(e)),"; expires="+i.toUTCString(),"; domain="+t(r)].join("")},r=function(t){var n,e;return e="; "+document.cookie,n=e.split("; "+t+"="),2===n.length?JSON.parse(decodeURIComponent(n.pop().split(";").shift())):null},l=function(t,n){var e,r;null==n&&(n={});for(e in n)r=n[e],t[e]=r;return t},s=function(){var t;return t=function(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)},""+t()+t()+"-"+t()+"-"+t()+"-"+t()+"-"+t()+t()+t()},e=function(){return r("distinct_analytics_id")||s()},n=function(){var t,n,r;t={distinct_analytics_id:e()},l(t,o()),l(t,u()),l(t,i()),l(t,a()),window.current_attribution_data||(window.current_attribution_data={});for(n in t)r=t[n],f(n,r),window.current_attribution_data[n]=r;return t},m=function(t){var n,e,r;if(null==t&&(t=!1),t&&(n=window.analytics=window.analytics||[],!n.initialize)){if(n.invoked)return window.console&&console.error&&console.error("Segment snippet included twice.");for(n.invoked=!0,n.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","group","track","ready","alias","page","once","off","on"],n.factory=function(t){return function(){var e;return e=void 0,e=Array.prototype.slice.call(arguments),e.unshift(t),n.push(e),n}},r=0;r<n.methods.length;)e=n.methods[r],n[e]=n.factory(e),r++;return n.load=function(t){var n,e;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",n=document.getElementsByTagName("script")[0],n.parentNode.insertBefore(e,n)},n.SNIPPET_VERSION="3.0.1",n.load(window.attribution_tracking_options.segment_key)}},(_=function(){return n(),m(window.attribution_tracking_options.segment_key),analytics.identify(window.current_attribution_data.distinct_analytics_id,window.current_attribution_data),analytics.track("Page View",{attribution:window.current_attribution_data}),analytics.page()})()}).call(this);
<html>
<head>
<!-- Add the following <script>...</script> to your <HEAD /> -->
<script>
window.attribution_tracking_options = {
'cookie_domain': '.tradeshift.com',
'segment_key': '[ADD YOUR KEYS HERE]' // Keys are secret and included in the email
};
</script>
<script src="//tradeshift-marketo-sync.herokuapp.com/attr_trk_js.js" async></script>
<!-- That is all. -->
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment