The short version
To fire a GA4 generate_lead event from a Formidable Forms submission you should bind the GA4 code to Formidable’s frmAfterSubmit JavaScript hook, ensuring the call runs after the form has been validated and the thank‑you markup is present. Using a generic jQuery $(document).ready() handler will often fire too early, resulting in the event being dropped.
Why the jQuery ready handler is a timing trap
Formidable Forms builds its UI with JavaScript. When the page loads, the form fields are injected, and validation is attached. If you place the GA4 snippet inside $(document).ready(), the browser may execute the code before Formidable finishes its own initialisation. In that window the hidden gform_submit_button has not yet been replaced with the final submit button, and the frmAfterSubmit event that signals a successful submission has not been registered. GA4 therefore receives an incomplete payload or no payload at all, and the conversion is not recorded. If an old script remains in the browser after deployment, first review how cache headers affect revalidation before changing the event handler again.
Correct hook: frmAfterSubmit
Formidable provides a JavaScript event that fires only after the form is successfully submitted and any post‑submission markup (such as a thank‑you message) is inserted into the DOM. Listening to that event guarantees that the form data is final and that the page is ready for the GA4 call. Event tracking is part of the form integrations we handle in WordPress development, with successful submissions verified before reporting them as leads.
// assets/js/ga4-formidable.js – enqueue this file in WordPress
document.addEventListener('frmAfterSubmit', function(event) {
// event.detail contains the form ID and the response object
var formId = event.detail.formId;
var response = event.detail.response;
// Only fire for the specific lead capture form
if (formId !== 3) { // replace 3 with your form’s ID
return;
}
// Build the GA4 event parameters
var params = {
currency: 'GBP',
value: parseFloat(response.total || 0),
// Add any custom fields you need, e.g. email
email: response.fields.email || ''
};
// Send the event to GA4 (gtag.js version)
if (typeof gtag === 'function') {
gtag('event', 'generate_lead', params);
}
});
Enqueue the script correctly
Place the following in your theme’s functions.php (or a custom plugin). It works with WordPress 6.5 and gtag.js version 2.
function mytheme_enqueue_formidable_ga4() {
// Ensure Formidable’s scripts are loaded first
wp_enqueue_script(
'ga4-formidable',
get_stylesheet_directory_uri() . '/assets/js/ga4-formidable.js',
array('jquery'), // dependency only for load order
null,
true // load in footer
);
}
add_action('wp_enqueue_scripts', 'mytheme_enqueue_formidable_ga4');
GA4 configuration snippet
Insert the global GA4 snippet once, preferably in the theme’s header.php or via a site‑wide plugin. Use the measurement ID for the property that tracks leads.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
Key insight
GA4 events must be dispatched after Formidable’s frmAfterSubmit event fires; otherwise the event payload can be incomplete or omitted entirely.
Before vs. after: a side‑by‑side comparison
Incorrect approach
Code placed inside $(document).ready() runs as soon as the DOM is ready, before Formidable finishes its own initialisation. The event is often sent while the form is still being built, so GA4 receives no data.
$(document).ready(function() {
gtag('event', 'generate_lead', {value: 0});
});
Correct approach
Listening to frmAfterSubmit guarantees the form has been validated and the thank‑you markup is present. The GA4 call runs with the final values.
document.addEventListener('frmAfterSubmit', function(e) {
var params = {value: e.detail.response.total};
gtag('event', 'generate_lead', params);
});
Handling custom fields
If your form collects additional data such as phone_number or company_name, reference them from event.detail.response.fields. The field keys match the Formidable field keys you set in the form builder.
var phone = response.fields.phone_number || '';
var company = response.fields.company_name || '';
var params = {
value: parseFloat(response.total || 0),
phone_number: phone,
company_name: company
};
gtag('event', 'generate_lead', params);
Testing the implementation
Open the page in a browser with the GA4 DebugView enabled (use the GA Debugger extension or the gtag('set', 'debug_mode', true) flag). Submit the Formidable form and verify that a generate_lead event appears in real‑time. Check that the parameters match the values entered in the form.
Common pitfalls and how to avoid them
- Multiple forms on the same page. Filter by
formIdinside the listener to avoid sending duplicate events. - AJAX navigation. If you use a SPA framework that swaps content without a full page load, re‑enqueue the script on each navigation event or attach the listener once globally.
- Missing
gtagfunction. Ensure the global GA4 snippet loads before the custom script; otherwise wrap the call in aif (typeof gtag === 'function')check as shown.
What to do if you cannot modify the theme directly
Use a code‑snippets plugin to add the enqueue function, and a custom HTML widget (or the “Insert Headers and Footers” plugin) to place the GA4 global snippet. The JavaScript that listens for frmAfterSubmit can also be added via a “Custom JavaScript” field in many optimisation plugins.
Bottom line
Bind the GA4 generate_lead call to Formidable Forms’ frmAfterSubmit event, not to a generic jQuery ready handler. This ensures the conversion fires only after the form is fully processed, giving you reliable lead data in GA4 without inflating contact‑page view counts.
Need help putting this into practice?
Explore our WordPress Web Design, or tell us about your project so we can discuss the next step.
