How to encrypt data capture using Google Tag Manager

When I attended CROElite17, one of my favourite points someone (forget who – if it was you please let me know, I’ll give you a link) raised was that you can use Google Tag Manager to pull in form inputs when an error occurs, and then send that data to Google Analytics. This means you get loads of data as to why forms break and if there’s a consistency (e.g. it always breaks when people put brackets on their phone number or with two character surnames) fixes can be made, and conversion rate increases.

There are a few large problems with this idea:

  • GDPR
  • Data Protection
  • Terms of Use

Analytics meme

You really shouldn’t record people’s names, e-mail addresses, phone numbers, financial details and all that stuff in GA. You’ll get in trouble.

Encrypt data meme

To get useful information, you might not need the actual names, e-mail addresses, etc. tracked in Google Analytics. You need to be able to recognise the pattern they are producing (e.g. the character length of an erroring credit card, or a two-character TLD in an e-mail address) in order to properly test your theories and fix errors.

How to encrypt personal information using Google Tag Manager

  • To capture the data, create a new variable with the data you want to capture.
    • Simo gives a good overview of tracking form engagement here
    • This variable should NOT be linked to any tags! It’s only for transformation using the next variable we create
    • Capturing it as a variable means it’s stored temporarily on the user side so it can be transformed into its encrypted state
  • To transform the data, create a New Variable.
    • Variable Type: Custom Javascript
    • Custom Javascript:


   var str = {{YOUR VARIABLE}};

   var newString = str.replace(/[a-z,A-Z]/g, “X”).replace(/[0-9]/g, “1”);

   return newString;


  • Note: Replace YOUR VARIABLE with the name of the first variable you created above. What the Javascript does is it translates all letters to “X” and all numbers to “1”. You can amend this as makes sense to you

Variable configuration - Google Tag Manager

Above: The JavaScript transforms the “Page Query” variable.

Java script

Above: An e-mail address transformed.

  • Create a trigger for when a form is submitted, but not validated or an error occurs
  • Create a tag that pushes an event on this trigger, with the encrypted variable as data

Now you can look at your events report to see what sorts of inputs are most frequently causing errors, without violating Google Analytics Terms of use, or GDPR.

Thanks for reading, if you enjoy this, I’d recommend getting a ticket to CROElite18, the event that helped spark my interest in this topic (cheers guys!).

Related Posts