Reputation: 460
Cross-domain tracking with GTM.
On a specific page, a button click send the user from domainA to domainB.
I want to send a value (the referrer of domainA) as an url parameter inside the target link.
And then I retrieve this parameter value into a datalayer on domainB.
However, every button click is overridden by the Google Analytics cross-domain tracking.
On domainA:
Onclick :
Expected result : https://domainB/page?referrer=google
Actual result = https://domainB/page?_ga=123456.789654321
Here are some codes I have tried...
Onclick, update the link target :
<script>
function urlBouton () {
var newUrl= "https://domainB/page?referrer=google";
document.getElementById("btn").onclick = function() {changeUrl()};
function changeUrl() {
document.getElementById("btn").href = newUrl;
}
}
</script>
When the page opens (DOM ready), change the button link :
<script>
var newUrl = "https://domainB/page?referrer=google";
document.getElementById("btn").href.setAttribute("href", newUrl);
</script>
The JS console displays the new updated link... but on browsing, it is not displayed as intended.
Upvotes: 0
Views: 399
Reputation: 145
For full disclosure I don't know google analytics internals so if someone does, they may know something I don't. However, I do know browser mechanics pretty well and what you are doing is impossible due to security restrictions. One way of doing this, assuming both of the domains (windows) are going to be running within the same browser session, is using message passing window.postMessage(). This is however slightly restrictive because the second window must be open and listening to incoming messages before you post the message.
window.addEventListener('message', function _handler(event) {
// Code for handling the message
// This will automatically remove the listener after receiving one message.
// Good for when you send only one message and don't want the listener lingering and eating up memory.
window.removeEventListener('message', _handler);
});
If you are dealing with a situation where this is not going to be running within the same browser you will have setup some kind of "shared memory" in the form of a simple backend server/service.
Upvotes: 1