You should add the data layer push on the form submission event before the user is redirected to the thank you page.
This is because you want to capture and send the data at the exact moment the user submits the form, ensuring the most accurate and timely data collection for your conversion tracking.
Adding it to the thank you page might result in missed data capture if the user navigates away or closes the browser before the thank you page fully loads.
Here’s a simplified example of how you might implement this if you use the Gravity Form WordPress plugin for embedding forms on your website:
// Listen for the Gravity Form submission event
document.addEventListener(‘gform_confirmation_loaded’, function(event) {
// Assuming event.detail.formId is available and corresponds to your form’s ID
if (event.detail.formId === YOUR_FORM_ID) {
// Capture form data
var email = document.getElementById(‘input_YOUR_FORM_ID_X’).value; // Replace X with the field ID for the email
var phone_number = document.getElementById(‘input_YOUR_FORM_ID_Y’).value; // Replace Y with the field ID for the phone number
// Construct your data object
var leadsUserData = {
’email’: email,
‘phone_number’: phone_number,
// Include other fields as needed
};
// Push the data to the data layer
window.dataLayer = window.dataLayer || [];
dataLayer.push({
‘event’: ‘leadSubmission’,
‘userData’: leadsUserData
});
}
});
Make sure to replace YOUR_FORM_ID, X, and Y with the actual ID of your form and the specific input IDs for the email and phone number fields.
Follow these step-by-step instructions to add a data layer push on a form submission event before the user is redirected to the thank you page using Google Tag Manager (GTM).
This approach captures user input from a form and uses GTM to push this data into the data layer at the right moment during the form submission process.
Step-1: Navigate to your Google Tag Manager account and click on ‘Variables’.
Step-2: Now Click on “Configure” under built-in Variables.
Step-3: Now select all the check boxes under “Forms”.
Step-4: Now click on the “Tags” Tab.
Step-5: Click on the ‘New’ button to create a new tag:
Step-6: Name your tag and then click on ‘Tag configuration’.
Step-7: Select “Custom HTML” as a tag type.
Step-8: Enter your below JavaScript code in the HTML area.
Here’s a simplified example that pushes the email field into the data layer. Adjust the ID and field names as necessary:
Note: This code only captured the email address as user-provided data. You can modify the code to add extra user-defined parameters like First name, Last name, Phone number, etc.
Replace [name=”your_email_field_name”] with the actual name or ID selector of your email input field.
Step-9: Click on the trigger button to create a new trigger.
Step-10: Name your trigger and click on “Trigger Configuration”
Step-11: Select “Form Submission” as the trigger type.
Step-12: Now select condition “All Forms” from the radio button and then click on “Save”.
Step-13: Before publishing your changes, enter Preview mode in GTM to test the setup. This allows you to see in real-time how tags are fired without affecting live site visitors.
Step-14: Submit the Form: Go to the page with the form on your website while in Preview mode and submit the form.
Check Data Layer: Use GTM’s Preview mode panel or your browser’s developer tools to verify that the custom event that we created is fired.
The data layer should also be updated with the user’s email information upon form submission.
Debug if Necessary: If the data isn’t pushed as expected, double-check your trigger and tag configurations, ensuring they’re correctly set up to capture and push form submission data.
Step-15: Once you’ve confirmed that the data layer is correctly updated upon form submission, exit Preview mode and publish the changes in GTM to make the data layer push active on your live site.
Create data layer Variables in GTM:
For each piece of user data you want to track (e.g., email, phone number), create a Data Layer Variable in GTM. This tells GTM where to look in the data layer for the information it needs.
Step-1: Navigate to the ‘Variables’ tab in Google Tag Manager.
Step-2: Under “User-Defined Variables”, click on ‘New’.
Step-3: Name the variable and click on the “Variable configuration”:
Step-4: Select the variable type as a “Data Layer Variable”.
Step-5: Type “userData.email” under Data Layer Variable Name and click on ‘Save’.
Note: If your form has other details (e.g., first name, last name, phone number), you can repeat the same steps and create the data layer variables in GTM.
Create User-Provided Data variable in GTM
To create a new User-Provided Data variable in GTM, follow the below steps
Step-1: Navigate to the ‘Variables’ tab in Google Tag Manager:
Step-2: Under “User-Defined Variables”, click on ‘New’.
Step-3: Name the variable and click on the “Variable configuration”
Step-4: Select the variable type as “User-Provided Data”.
Step-5: Name the Variable and click on “Manual configuration”.
Step-6: Now click on the drop-down under “Email” and select the data layer variable that we created earlier.
Note: There are other fields as well in the configuration settings of the variable like below. If your form contains these variables you can reference them to the data layer variables in GTM that we created earlier.
Create a new tag of type ‘Google Ads User-Provided Data Event’ in GTM
Follow the below steps
Step-1: Navigate to your GTM container.
Step-2: In your “Workspace”, click ‘Tags’ from the navigation menu:
Step-3: Click ‘New’ to create a new tag:
Step-4: Rename your tag like “Google Ads User-Provided Data Event” and click on Tag Configuration:
Step-5: Now, under choose tag type click on “Google Ads”:
Step-6: Now click on ‘Google Ads User-Provided Data Event’.
Step-7: In “Tag Configuration”: In the field “Conversion ID”, supply your Google Ads Conversion Tracking ID.
Note: Make sure that this conversion action has the same conversion tracking ID and label as the conversion action that you enabled enhanced conversions for in your Google Ads account.
Step-8: Now, under the “User-provided data” drop-down, select the User-Provided Data variable that we created earlier.
Step-9: Now click on the “Trigger”:
Step-10: Select the trigger “Form Submission” that we created earlier while testing the custom html tag.
Step-11: Save the Tag.
#4 Verify your setup and publish the container
Verify your setup and publish your tag.
Before you publish your container, make sure that the tag behaves as intended using Preview mode.
If you are familiar with networking and hashing algorithms, you can also validate user data hashing.
Validate your tag setup using Preview mode
To validate your setup with Preview mode, follow these steps:
Step-1: In your Tag Manager workspace, click the ‘Preview’ button at the top. A new tab opens with Tag Assistant.
Step-2: Enter your website’s URL in Google Tag Assistant. Tag Assistant opens your website and the Tag Assistant summary. Keep both tabs open.
Step-3: On your website, fill out your form and click on “Submit”.
Step-4: Check the “Tag Assistant” summary. Your Google Ads User-Provided Data Event tag should be listed under the “Tags Fired” section in the summary.
If your tag is listed under “Tags Not Fired”, you need to adjust the tag’s trigger settings.
Step-5: Click on the tag to see which values were passed to the tag. Check if your input was received.
If the tag did not receive any input: Verify your implementation method.
If another tag fires before the Google Ads User-Provided Data Event tag, edit the “Form Submission” trigger. Set the “Wait for Tags” option.
Step-6: Now publish the workspace in Google Tag Manager.
Validate user data hashing using network calls
To validate that user data is submitted hashed using Google Chrome, follow these steps:
Step-1: Open your website and navigate to the form field you want to validate.
Step-2: Open the developer tools by right-clicking on the web page and select “Inspect”:
Step-3: Click on the “Network” Tab.
Step-4: Now click on the checkbox “Preserve log” and Disable cache”.
To make validation easier, clear any pre-existing network logs. By clicking on the clear button
Step-5: Now type “https://google.com/pagead” in the Filter box
Step-6: With your Network tab in the developer tools open, submit the data to the form field that you want to validate.
Result: You should see a request sent to https://google.com/pagead/form-data/ with a page parameter em=xyz.
The ’em’ parameter contains the hashed user data. Here the, ’em’ parameter is highlighted in “Yellow”
Once your tag collects the data parameters you need, Submit your changes in Tag Manager.
Congratulations!!!
You have successfully Set up enhanced conversions for leads using the data layer in Google Tag Manager.
My best selling books on Digital Analytics and Conversion Optimization
Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.
Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.
Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.
Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.
About the Author
Himanshu Sharma
Founder, OptimizeSmart.com
Over 15 years of experience in digital analytics and marketing
Author of four best-selling books on digital analytics and conversion optimization
Nominated for Digital Analytics Association Awards for Excellence
Runs one of the most popular blogs in the world on digital analytics
Consultant to countless small and big businesses over the decade
Free GA4 Mastery Course (30 Days Access) – Certification Upon Completion! (Over 4000 Students)
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.