Embedding User Agent in Google Analytics 4 Reports

How can you push user agent information to Google Analytics and Google Analytics 4 for use in reporting?

Blink SEO, an Elevar partner, came to us with a request, how can we get user agent information along with other reporting in Google Analytics and Google Analytics 4? We put our heads together and came up with the setup below, and a Google Data Studio report for analysis.

Adding User Agent Into Google Tag Manager As A Data Layer Variable

  1. Create a variable in Google Tag Manager that captures the User Agent. This can be done by going to 'Variables' section on the left side and clicking 'New'.

  1. Name the variable 'User Agent', click on the lego block, then select 'JavaScript Variable'

  1. Fill the 'Global Variable Name' field with window.navigator.userAgent and press save. This will pull the value into the data layer.

Now that you have user agent in the data layer you can choose to send it to Google Analytics, Google Analytics 4 or both.

Send User Agent to Google Analytics 4

Google Analytics 4 works slightly differently than Google Analytics.

  1. First we'll need to create the dimension in the Google Analytics 4 interface. Go to Google Analytics 4 and click Configure, then Custom Definition then 'Create Custom Dimensions'.

  1. Fill out the indicated fields shown below and click save.

  1. To send this variable to Google Analytics 4 you'll need to open your Google Analytics 4 Configuration variable. This should be referenced in all of your Google Analytics 4 event tags. In the Elevar prebuilt GA4 container this variable will be called GA4 Base Tag Configuration. Under 'User Properties' add a new row with property name set to user_agent and value set to variable 'User Agent', make sure to include the brackets.

  1. You'll now be sending the dimension to GA4 on every tag hit