Halloween Sale 15% off Ends in See the plans
PHP

GA4 Lead Event for Fluent Forms

Sends a generate_lead event to Google Analytics 4 or Google Tag Manager whenever a Fluent Forms form is submitted successfully.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_ff_event_name = 'generate_lead';

add_action( 'wp_footer', function () use ( $wpide_ff_event_name ) {
    if ( ! wp_script_is( 'fluent-form-submission', 'enqueued' ) && ! wp_script_is( 'fluent-form-submission', 'done' ) ) {
        return;
    }
    ?>
<script>
document.addEventListener( 'fluentform_submission_success', function ( event ) {
    var name = <?php echo wp_json_encode( $wpide_ff_event_name ); ?>;
    var form = event.detail && event.detail.form;
    var data = { form_id: form ? String( form.getAttribute( 'data-form_id' ) || '' ) : '', form_type: 'fluent-forms' };
    if ( typeof window.gtag === 'function' ) { window.gtag( 'event', name, data ); }
    else { window.dataLayer = window.dataLayer || []; window.dataLayer.push( Object.assign( { event: name }, data ) ); }
} );
</script>
    <?php
}, 100 );

Notes

Needs gtag.js or a GTM container already on the site.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.