{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","settings":"","results":{"codes":[]},"params":[]},"next":{"description":"","pages":[]},"title":"Cross-domain tracking","type":"basic","slug":"cross-domain-tracking-1","excerpt":"How to track multiple domains against one project","body":"If you have multiple different domains you want to track as a single site then this hook will work for you. It works by adding **anonymous_id** parameter to links which point to your cross-domain list. It also sets **anonymous_id** if the one is present in current URL. The code below should work for most setups, please update **crossDomains* variable to include all of your sites' domains (hostnames) you want to track. This code need to be placed on every site.\n[block:api-header]\n{\n  \"title\": \"Example script\"\n}\n[/block]\nThe script below is an example of what a cross-domain script may look like. You should be aware to adjust the projectId and host domains for tracking. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Attribution app snippet code\\nwindow.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();\\n\\n// set your project key\\nwindow.Attribution.projectId=\\\"YOUR_APP_KEY\\\";\\n\\n// cross-domain hook\\nAttribution.ready(function() {\\n  // list of your hostnames that you want to share visitors\\n  var crossDomains = ['hostA.com', 'www.exampleB.net', 'otherC.com'];\\n\\n  // search current url for anonymous_id\\n  var currentUrl = new URL(window.location.href);\\n  var anonymousId = currentUrl.searchParams.get('anonymous_id');\\n\\n  if (anonymousId) {\\n    // set anonymous id\\n    Attribution.user().anonymousId(anonymousId);\\n  } else {\\n    // read generated anonymous id\\n    anonymousId = Attribution.user().anonymousId();\\n  }\\n\\n  // track current pageview\\n  Attribution.page();\\n\\n  // check every link and tag external links to our crossDomains with anonymous_id\\n  var elements = document.body.getElementsByTagName('a');\\n  for (var i = 0; i < elements.length; i++) {\\n    var link = elements[i].getAttribute('href');\\n\\n    // we use try here because not every link is a valid url\\n    try {\\n      var url = new URL(link);\\n\\n      // if not current hostname and hostname is in our cross-domain list\\n      if ((url.hostname != currentUrl.hostname) && (crossDomains.indexOf(url.hostname) > -1)) {\\n        var newLink = link + ((link.indexOf('?') > -1) ? '&' : '?') + 'anonymous_id=' + anonymousId;\\n        // update link to include anonymous_id\\n        elements[i].setAttribute('href', newLink);\\n        // console.warn(link + ' matched, updated to ' + newLink)\\n      } else {\\n        // console.warn(link + ' domain is not in our list')\\n      }\\n    }\\n    catch (e) {\\n      // console.warn(link + ' is not an URL');\\n    }\\n  }\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nOnce you've adjusted the snippet above you can install it on all your pages to begin tracking cross-domain as one project. You should use this in a case where you may have domainA & domainB, but you want to see return on ad spend for both in one project. \n\nIf you have any questions please reach out to help:::at:::attributionapp.com","updates":[],"order":0,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"600060e3f3d8e9003caf7297","createdAt":"2021-01-14T15:18:59.735Z","user":"560c92f2ac2859170013faa3","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"FAQ when installing","slug":"faq-when-installing","order":1,"from_sync":false,"reference":false,"_id":"600060c72dcd9d002fa13656","createdAt":"2021-01-14T15:18:31.879Z","version":"560c93ae7e9b9d0d00ca81a5","project":"560c93ad7e9b9d0d00ca81a2","__v":0},"version":{"version":"1.0","version_clean":"1.0.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["560c93af7e9b9d0d00ca81a6","560c9d9399bb5a0d0044f220","560d76d899bb5a0d0044f307","560d76ee1ec45619006069ed","560d86e099bb5a0d0044f32e","560dba80373c0e0d0024ff3b","57c722ecdf19130e001fba5d","57c743d1b6f94a2200659903","58995ec083f743190077bbe2","5fabfb070e9b0600424707dd","60004359deee820012dc1efc","600060c72dcd9d002fa13656","6001ac08c104fe002b933b7c","6009604d81b288001417df30","60097b7be8ec1e00334419f6","600aa98de28fae003a33108a","600aa9ee7d3e350018e7d6e4","601176819ca8c90011cfe6ac"],"_id":"560c93ae7e9b9d0d00ca81a5","project":"560c93ad7e9b9d0d00ca81a2","createdAt":"2015-10-01T02:00:14.709Z","releaseDate":"2015-10-01T02:00:14.709Z","__v":18},"project":"560c93ad7e9b9d0d00ca81a2","__v":0,"parentDoc":null}

Cross-domain tracking

How to track multiple domains against one project

If you have multiple different domains you want to track as a single site then this hook will work for you. It works by adding **anonymous_id** parameter to links which point to your cross-domain list. It also sets **anonymous_id** if the one is present in current URL. The code below should work for most setups, please update **crossDomains* variable to include all of your sites' domains (hostnames) you want to track. This code need to be placed on every site. [block:api-header] { "title": "Example script" } [/block] The script below is an example of what a cross-domain script may look like. You should be aware to adjust the projectId and host domains for tracking. [block:code] { "codes": [ { "code": "// Attribution app snippet code\nwindow.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();\n\n// set your project key\nwindow.Attribution.projectId=\"YOUR_APP_KEY\";\n\n// cross-domain hook\nAttribution.ready(function() {\n // list of your hostnames that you want to share visitors\n var crossDomains = ['hostA.com', 'www.exampleB.net', 'otherC.com'];\n\n // search current url for anonymous_id\n var currentUrl = new URL(window.location.href);\n var anonymousId = currentUrl.searchParams.get('anonymous_id');\n\n if (anonymousId) {\n // set anonymous id\n Attribution.user().anonymousId(anonymousId);\n } else {\n // read generated anonymous id\n anonymousId = Attribution.user().anonymousId();\n }\n\n // track current pageview\n Attribution.page();\n\n // check every link and tag external links to our crossDomains with anonymous_id\n var elements = document.body.getElementsByTagName('a');\n for (var i = 0; i < elements.length; i++) {\n var link = elements[i].getAttribute('href');\n\n // we use try here because not every link is a valid url\n try {\n var url = new URL(link);\n\n // if not current hostname and hostname is in our cross-domain list\n if ((url.hostname != currentUrl.hostname) && (crossDomains.indexOf(url.hostname) > -1)) {\n var newLink = link + ((link.indexOf('?') > -1) ? '&' : '?') + 'anonymous_id=' + anonymousId;\n // update link to include anonymous_id\n elements[i].setAttribute('href', newLink);\n // console.warn(link + ' matched, updated to ' + newLink)\n } else {\n // console.warn(link + ' domain is not in our list')\n }\n }\n catch (e) {\n // console.warn(link + ' is not an URL');\n }\n }\n});", "language": "javascript" } ] } [/block] Once you've adjusted the snippet above you can install it on all your pages to begin tracking cross-domain as one project. You should use this in a case where you may have domainA & domainB, but you want to see return on ad spend for both in one project. If you have any questions please reach out to [email protected]