How To Take GHL Form Data & Use It as a Variable in GTM

We’re going to use the existing ‘_ud’ local storage variable that GHL populates with form data. 

Ensure GTM is installed before you continue.

STEP 1:

To create a variable for a users email address; create a new ‘Custom JavaScript’ variable in GTM with the following code: 

function() {
  var userDataString = localStorage.getItem('_ud');
  if (userDataString) {
    try {
      var userData = JSON.parse(userDataString);
      
      return userData.email || 'Default Name'; // Use a default or return an empty string if preferred
      
    } catch(e) {
      // Handle parsing error
      console.error('Error parsing _ud:', e);
      return 'Error Name'; // Return an error name or any other fallback value
    }
  }
  
  // Return null, a default name, or any other fallback value if _ud is not found
  return 'No Name Found';
}

Save the variable and publish the container.

Test the funnel in tagassistant.google.com

Once you submit the form, you should see a variable in tagassistant populated with the users email address.

STEP 2:

To create another variable for another field on the GHL form, you can edit the ‘return userData.email’ part of the code above to match the parameter you require.

For example to populate a variable with the users first name, you would change it to:

return userData.first_name

You can find a full list of the form fields and their identifiers by viewing the local storage _ud variable and choosing one of the items listed in the JavaScript object. 

You can access this information by going to Inspector > Application > Local storage > Select your domain > click on the _ud variable. 

VISIT UKB MARKETING

Contact us today to learn more about how we can help your brand grow.