Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PiotrKrzyzek/932023f2cdeefa145c1053d15898f6b9 to your computer and use it in GitHub Desktop.
Save PiotrKrzyzek/932023f2cdeefa145c1053d15898f6b9 to your computer and use it in GitHub Desktop.
Track FluentForm View & Submission Event in Google Analytics 4 with custom data
// Wait for jQuery, using the full name for compatability reasons
jQuery('document').ready(function(){
// This is our main function that will loop
function doFFGa(){
// If google tag manager or analytics is NOT detected
// Then loop this function with a two second delay
// return afterwards just in case
if(typeof gtag != 'function') {
setTimeout(doFFGa, 2000);
return;
}
// If we get here, this means that Google Analytics is detected
// Get all the FluentForms detected
var fluentForms = jQuery('form.frm-fluent-form');
// Loop through each
fluentForms.each(function() {
var form = jQuery(this); // Get the form
var formId = form.attr('data-form_id'); // Get the form ID
// Configure the form view event and send it
gtag('event', 'ViewForm', {
event_category: 'FluentForms',
event_label: 'View Form',
form_url: window.location.href,
form_id: formId
});
// If and when the form is successfully submitted ...
// Send submission event with some data
form.on('fluentform_submission_success', function() {
gtag('event', 'FormSubmission', {
event_category: 'FluentForms',
event_label: 'Form Submitted',
form_url: window.location.href,
form_id: formId
}); // End send submit event
}); // end on success event
}); // end for each
} // end function
// After the definition above, wait 2seconds and then run it.
setTimeout(doFFGa, 2000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment