Last active
May 22, 2019 19:07
-
-
Save troex/15c9aef03cbe51285801ffe2c4ea12d2 to your computer and use it in GitHub Desktop.
AttributionApp Cross-domain Tracking Snippet
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
// Attribution snippet code | |
window.Attribution=window.Attribution||[];window.Attribution.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","group","track","ready","alias","page","once","off","on"];window.Attribution.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);window.Attribution.push(t);return window.Attribution}};for(var i=0;i<window.Attribution.methods.length;i++){var key=window.Attribution.methods[i];window.Attribution[key]=window.Attribution.factory(key)}window.Attribution.load=function(e){if(document.getElementById("attribution-js"))return;var t=document.createElement("script");t.type="text/javascript";t.id="attribution-js";t.async=true;t.src="//scripts.attributionapp.com/attribution.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)};window.Attribution.load(); | |
// set your project key | |
window.Attribution.projectId="YOUR_APP_KEY"; | |
// cross-domain hook | |
Attribution.ready(function() { | |
// list of your hostnames that you want to share visitors | |
var crossDomains = ['hostA.com', 'www.exampleB.net', 'otherC.com']; | |
// search current url for anonymous_id | |
var currentUrl = new URL(window.location.href); | |
var anonymousId = currentUrl.searchParams.get('anonymous_id'); | |
if (anonymousId) { | |
// set anonymous id | |
Attribution.user().anonymousId(anonymousId); | |
} else { | |
// read generated anonymous id | |
anonymousId = Attribution.user().anonymousId(); | |
} | |
// track current pageview | |
Attribution.page(); | |
// check every link and tag external links to our crossDomains with anonymous_id | |
var elements = document.body.getElementsByTagName('a'); | |
for (var i = 0; i < elements.length; i++) { | |
var link = elements[i].getAttribute('href'); | |
// we use try here because not every link is a valid url | |
try { | |
var url = new URL(link); | |
// if not current hostname and hostname is in our cross-domain list | |
if ((url.hostname != currentUrl.hostname) && (crossDomains.indexOf(url.hostname) > -1)) { | |
var newLink = link + ((link.indexOf('?') > -1) ? '&' : '?') + 'anonymous_id=' + anonymousId; | |
// update link to include anonymous_id | |
elements[i].setAttribute('href', newLink); | |
// console.warn(link + ' matched, updated to ' + newLink) | |
} else { | |
// console.warn(link + ' domain is not in our list') | |
} | |
} | |
catch (e) { | |
// console.warn(link + ' is not an URL'); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment