Search code examples
google-tag-managergoogle-analytics-4

Multiple GTM variables that can be user_id - how to send the correct one to GA4?


The website I am managing exposes the user's unique ID in a number of different places depending upon the journey they take. The user ID may be retrieved from the data layer a referring URL parameter or a page parameter.

In GTM I am retrieving the user ID and storing in 5 different variables, only 1 of which will have the actual ID - the others will be NULL values.

If I want to send the actual user ID to GA4 using the user_id event parameter how can I select the value that may be in 1 of 5 variables without having the correct value overwritten by NULLs?

I can imagine a JS script might do it, but I don't know where that would live and be executed.


Solution

  • Fortunately the blank variables weren't 'NULL' but 'undefined'. This meant I could use the GTM 'Lookup Table' variable and test for an undefined value using the 'Undefined' GTM variable.

    If the input variable value was 'undefined' then the table would use another defined variable, if not undefined then you can set the default value to the same as the 'input' or another variable depending on the logic needed. You can cascade these as per the suggestion from @Eike Pierstorff .