'GA4 recording logged-in username not working

I'm trying to record the logged-in username into GA4.

I have a page that is hand crafted (not generated using the WP admin). I can't use the gtm plugin on that page so I've put the gtm tags on manually and I've used the following JS:

dataLayer.push({        'visitorLoginState': 'logged-in' , 'visitorUsername': 'username'      });

Where 'username' is the actual username.

Looking at the variables in the console it looks fine, but it doesn't record the username into GA4. The page records other GA4 data fine but not the logged in username.

What have I got wrong there.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source