Preventing loss of source/medium if user moving from one domain to another with different GA4 ID's

Preventing loss of source/medium if user moving from one domain to another with different GA4 ID's

This came as a use case for one of my client where he manages 3 different business units with different domains and all of them were linked with a single main domain.

Whenever he activates a campaign, he activates on the main domain and the user has to navigate to another ones according to the selection.

First, lets name those domains.

Main domain = maindomain.com

Biz Unit 1 = bizunit1.com

Biz Unit 2 = bizunit2.com

Biz Unit 3 = bizunit3.com

All of these domains had a separate GA4 property but were enabled via a single GTM Web Container.

Now you know the problem he will face here is when the user lands on maindomain.com, and goes to bizunit1.com as per the selection and triggers a conversion event on bizunit1.com, the source/medium will be "direct/none" as he also excluded maindomain.com from "Unwanted Referral List".

And this screwed his data.

So, here is what I thought to solve this case-

We will record the value of "Referrer" on visitor's landing page in "sessionStorage" of the browser tab and will then utilize it in updating the "Referrer" of the GA4 Config of business unit's website (lets take bizunit1.com).

This is being done because the value of source/medium in GA4 is taken from "Referrer."

There is one more case which needs to be handled here (for which I will come in the last).

Lets see how its done in Google Tag Manager.

Step 1: Make a Custom HTML tag to store the referrer value in sessionStorage

Move to GTM > Tags > New Tag > Custom HTML

Add the below code-

<script>
var hasEnteredBefore = sessionStorage.getItem('hasEntered');  
if (!hasEnteredBefore && document.referrer != '') {  
  console.log('users first page');
    var sourceAndMedium = sessionStorage.setItem('sourceAndMedium','{{Referrer}}');
    console.log('sourceAndMedium set to '+ sessionStorage.getItem('sourceAndMedium'))
    sessionStorage.setItem('hasEntered', 'true');
</script>        

Explanation of the code:

I am firstly fetching a sessionStorage item's value named as 'hasEntered' and storing in a variable.

If that will be the user's first page, there will not be any sessionStorage item named as 'hasEntered'.

So, my first condition will become true on a landing page and I am also checking if the traffic is not having any referrer because then we dont need to store anything or do anything.

Now when both of the conditions become true, I am storing the value of "Referrer" in sessionStorage as "sourceAndMedium."

Since we are now done with storing the value, its time to override the value of "hasEnteredBefore" variable so that the sessionStorage do not gets updated again.

Step 2: Add a trigger which fires before your GA4 config. I prefer Consent Initialization - All Pages. Hit Save.

Step 3: Move to Variables > New User-Defined Variables > Custom Javascript and add this script

function(){
 
  if ((!{{Referrer}}.includes('maindomain.com')) && ({{Page URL}}.includes('bizunit1.com')))
  {
    var actual_referrer = sessionStorage.getItem('sourceAndMedium');
    return actual_referrer;
  }
  else
  {
    return {{Referrer}};
}  
}        

Explanation of the code:

Checking if the "Referrer" to bizunit1.com is maindomain.com or not. If yes, then only my further steps should execute, else it should return whatever "Referrer" it got.

Save this as "cJS - Modified Referrer".

Now we have to utilize this variable in updating the GA4 Config Tag's Referrer settings.

Step 4: Open your GA4 Config Tag and add things like below:

This is how GA4 config needs to be to update the referrer



So, now you are done.

Right?

No!

What will happen if the user lands on maindomain.com with some utm parameters or gclid or fbclid?

You have to also get those values to be fetched to GA4. Right?

Now, we will modify Page URL of the GA4 Config by storing those URL's which have query parameters with it.

We are modifying Page URL because UTM parameters, gclid, fbclid, etc are all picked from Page URL by GA4.

Step 5: Open the tag you made in Step 1 and modify it to store query parameters.

Make your code look like this

<script>
var hasEnteredBefore = sessionStorage.getItem('hasEntered');  
if (!hasEnteredBefore && document.referrer != '') {  
  console.log('users first page');
    var sourceAndMedium = sessionStorage.setItem('sourceAndMedium','{{Referrer}}');
    console.log('sourceAndMedium set to '+ sessionStorage.getItem('sourceAndMedium'))
    sessionStorage.setItem('hasEntered', 'true');

if ({{Page URL}}.indexOf('?')>-1)
  {
    sessionStorage.setItem('pageUrlWithQuery', {{Page URL}}.slice({{Page URL}}.indexOf('?')));
  } 
</script>        

The added "if" condition checks if the Page URL contains "?" or not and if yes, it takes all the values from the Page URL from "?" and stores it in sessionStorage's new item.

Lets say if the Page URL is https://maindomain.com?fbclid=123abc then the stored value in "pageUrlWithQuery" will be "?fbclid=123abc".

We will now use this value to append in the Page URL of GA4 Config of bizunit1.com .

Step 6: Move to Variables > New User-Defined Variables > Custom Javascript and add this script and name it as "cJS - Page URL"

function()
{
if((!{{Referrer}}.includes('maindomain.com')) && ({{Page URL}}.includes('bizunit1.com')))  
{
  if(sessionStorage.getItem('pageUrlWithQuery') != null)
  {
return {{Page URL}}+sessionStorage.getItem('pageUrlWithQuery');
  }
else{
 return {{Page URL}};
}
}
else{
  return {{Page URL}};
}
}        

Explanation of the code:

As in the modification of referrer custom variable, here as well, I am checking if the referrer is maindomain.com and the current page URL is bizunit1.com or not.

If yes, again it checks if there is any value in 'pageUrlWithQuery' sessionStorage item.

If yes, I am appending the current page URL with the value I received in 'pageUrlWithQuery'.

If no condition satisfies, I am returning the original Page URL.

Save this.

Step 7: Again, open your GA4 Config Tag and add your new variable like below:

This is how GA4 config needs to be to update the referrer & page_location

Step 8: Thank me in the comments, if this took you close to wisdom :-)



Stefan Olsen

CTO / Founder Owner at Up & Away Digital AS // Founder / Owner Fabrikken Kulturscene AS

10 个月

This is gold! Thank you! Is there a similar way to split all UTM tags in the parameters into session campaign, manual ad content, and so forth?

Nikhil Suryawanshi

Certified OneTrust | GA4 | Google Tag Manager | sGTM | Adobe Analytics | Adobe Launch with holding expertise more than 11+ years in various industries users and events data tracking.

1 年

Thanks Aman for sharing

要查看或添加评论,请登录

社区洞察