# Track Contact Form 7 Sends in Google Analytics 4

> Sends a generate_lead event to Google Analytics 4 or Google Tag Manager each time a Contact Form 7 form is sent successfully, with the form ID attached.

- Type: PHP
- Runs: Frontend only
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: contact-form-7, analytics, ga4
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cf7-ga4-conversion-event/

## Code

```php
$wpide_cf7_event_name = 'generate_lead'; // GA4 event name.

add_action( 'wp_footer', function () use ( $wpide_cf7_event_name ) {
    if ( ! wp_script_is( 'contact-form-7', 'enqueued' ) ) {
        return;
    }
    ?>
<script>
document.addEventListener( 'wpcf7mailsent', function ( event ) {
    var name = <?php echo wp_json_encode( $wpide_cf7_event_name ); ?>;
    var data = { form_id: String( event.detail.contactFormId ), form_type: 'contact-form-7' };
    if ( typeof window.gtag === 'function' ) {
        window.gtag( 'event', name, data );
    } else {
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push( Object.assign( { event: name }, data ) );
    }
}, false );
</script>
    <?php
} );
```

## Notes

Works with gtag.js or a GTM dataLayer already on the site; rename the event at the top to match your conversion setup.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Track Contact Form 7 Sends in Google Analytics 4" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
