What Google Tag Manager Custom JavaScript Variables Can Be used to Capture Form Data?

When capturing elements from a form, our team has found the following Custom Javascript Variables very useful.

Click to Create a New User-Defined Variable

Enter a Name for the Variable and click on 'Custom JavaScript'
The name is important as it may be used as a variable within a tag.

Cut and Paste one of the Snippets below

By ID

function() {
  var inputField = document.getElementById("ENTER ID HERE");
  return inputField.value || "";
}
<br>

By Name

function a () {
  var inputField = document.getElementsByName("NAME GOES HERE")[0];
  return inputField.value || "";
}
<br>

Don't forget to enter the ID or Name you find from the Form Element.

In the case of Hashtags (#), create the following variable

function() {
  return window.location.pathname + window.location.search + window.location.hash;
}<br>

Still need help? Contact Us Contact Us