Get a User’s Email Address Into Google Analytics

The Problem

How do you get identifying information on a site user into Google Analytics?

The Solution

If you are using Google Analytics you may want specific data on each user so you can accurately track and value an event.

How would you get some personifying user details into Google Analytics? You can do this by using Google Analytics ‘events’. When a user submits a form, take the user’s email address and send that as a Google Analytics ‘event’. Google Tag Manager makes this relatively easy.

First make a new user defined variable called ’email’ that is of type ‘Data Layer Variable’.

Next create a new Trigger that will fire on form submission.

Important: If you are using a web library where events don’t bubble to the root of the document (e.g. React), then you will have to manually push the event data into the window.dataLayer. Otherwise the Google Tag Manager ‘Form Submission’ event won’t bubble up to the DOM root, and therefore Google Tag Manager will never register it. In that case the ‘Trigger’ should be of type ‘Custom Event’, then use the code below that runs after successful form submission.

window.dataLayer = window.dataLayer || []
// push user's email address first
// now the event
// the event string below should be the same name as the 'event name'
// of the trigger in Google Tag Manager
window.dataLayer.push({event: 'formSubmitted', formId: 'someForm'})

Now setup the ‘Tag’ in Google Tag Manager.

Next save Google Tag Manager and publish the new version.

The Result

User email as an event in Google Analytics

You can now correlate a specific user with specific actions on your site in Google Analytics.