How to encrypt data capture using Google Tag Manager

Author avatar

Emma Barnes

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!).


+44 (0) 1225 480 480

20 Manvers Street




+44 (0) 113 260 4010

2nd floor, 2180 Century Way,

Thorpe Park,

Leeds, LS15 8ZB.


+44 (0) 113 260 4010

5th Floor, Cordy House,

91 Curtain Road

London, EC2A 3BS

Part of the St. Ives Group

  • By pressing submit you consent for Edit to contact you via your email or telephone number for purposes relevant to your request for our goods or services. Your contact details, including your name, company, telephone number and email address will be used by Edit. By contacting you are agreeing to Edit’s Privacy Policy. If you have any questions, please ensure you review this section before submitting.

  • This field is for validation purposes and should be left unchanged.

© 2018 Edit. St Ives Group. Company reg. no. 3624881, All rights reserved. VAT Registered GB 927458295 Privacy Policy | Terms & Conditions | Cookie Policy