I have been using Universal Analytics previously and now switched to Google Analytics 4.

In UA, along with the page view we used to set and send a custom variable called userName. This variable helps us to track which user is accessing the page.

Below is the code used in UA:

ga('create', 'UA-XXXXXX', 'www.testsite.com');
ga('set', 'userName',variableUserName);
ga('send', 'pageview',
             'dimension1': variableUserName

Now I want to send the same custom variable in Google Analytics 4, set up also for pageviews.

I'm not sure how to set up custom dimension/event parameter while registering GA script. Below is my set up code using GTM:

(function (w, d, s, l, i) {
 w[l] = w[l] || []; w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
 var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true;
 j.src ='https://www.googletagmanager.com/gtm.js?id=' + i + dl + '&gtm_auth=**********-**********&gtm_preview=env-1&gtm_cookies_win=x'; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-*******')

Please guide me in how I can do this.

  • 1
    You can set custom parameters to all events (and pageviews are events in GA4). But, be aware, if GA4 interprets any custom parameter value as PII, they will simply discard it. Sep 11, 2023 at 13:04
  • Also, and more pertinently, Google are introducing new GA4 tags for GTM at the moment. I'd post my comment as an answer, but it's likely to be out of date inside a week. As soon as the changeover is complete, I'll post something more. Sep 11, 2023 at 13:06
  • 1
    Going off @GeoffAtkins first comment, if Google sees that you might be trying to pass PII, it will discard it and or shut your account down as well since it is against T&Cs. Rather than passing the userName, I would look for a UUID type identifier to pass. It would follow the same process just a PII compliant version of it. It will cause more work on your end but you won't have the risk of losing data. Nov 8, 2023 at 16:13

1 Answer 1


First thing you need to do is get the value from the site. There are a lot of different ways you can get the data into GTM (JavaScript, Data Layer, DOM Element). Your approach depends on how you can access the data.

Once you are grabbing the data in someway and populating a variable, you need to make sure to add it as a parameter on the tag. There are two different ways to do this: User or Event. If you are putting this on the main Google Tag, I would suggest setting it as a User parameter. I would suggest testing out the new parameter in preview mode in GTM or debug mode in GA to make sure the data is being passed properly.

After you have created the variable and parameter on the appropriate tag and published your container, you will need to go into Google Analytics and create a custom dimension in the Admin panel >> Custom Definitions. Once created it will take a few days in order to use the new dimension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.