The Contact Form 7 plugin is one of the most popular form builders for WordPress. One feature users frequently look for is redirecting to a thank-you page after a successful form submission.
In most cases, the reason for a redirect is conversion tracking in Google Analytics. But you don’t actually need a redirect for that – you can track form submissions directly without any page change.
In this guide I’ll show you how to redirect to a thank-you page after a CF7 form submission, and also how to send events to Google Analytics 4 without a redirect.
If Google Analytics isn’t set up on your site yet, check out this guide on adding Google Analytics 4 to WordPress.
Redirect to a Different URL After CF7 Form Submission
The simplest approach is using Contact Form 7’s DOM events to run JavaScript. When a form is submitted successfully, the plugin fires an event called wpcf7mailsent.
The following code redirects the user to a different URL after a successful submission:
<?php
function redirect_cf7_after_submission() { ?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
window.location.href = 'https://example.com/thank-you/';
}, false );
</script>
<?php }
add_action( 'wp_footer', 'redirect_cf7_after_submission' );Add this function to your child theme’s functions.php file. Replace the URL with the page you want to redirect to.
If you have multiple forms on your site and need different redirects for each, use event.detail.contactFormId to identify which form was submitted.
Here’s an example that redirects each form to a different thank-you page:
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( event.detail.contactFormId === '123' ) {
window.location.href = 'https://example.com/thank-you-contact/';
} else if ( event.detail.contactFormId === '456' ) {
window.location.href = 'https://example.com/thank-you-quote/';
}
}, false );Replace 123 and 456 with the actual IDs of your forms.
Track CF7 Form Submissions in Google Analytics 4
If you use Google Analytics 4 (GA4) to monitor your site traffic, you’ll probably want to track form submissions and receive events for every successful send.
Universal Analytics (analytics.js and UA-XXXXX) stopped processing data in July 2023. If your code still uses ga('send', ...), you need to migrate to GA4.
Option A – Send Events Directly with gtag.js
If you use gtag.js directly (not through Google Tag Manager), add the following to your child theme’s functions.php:
<?php
function send_ga4_event_cf7() { ?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
gtag( 'event', 'generate_lead', {
event_category: 'Contact Form',
event_label: 'CF7 Submit',
contact_form_id: event.detail.contactFormId
});
}, false );
</script>
<?php }
add_action( 'wp_footer', 'send_ga4_event_cf7' );The event name generate_lead is one of GA4’s recommended events for tracking conversions from contact forms.
Option B – Send Events via Google Tag Manager
If you’re using Google Tag Manager, the approach is to push an event to the dataLayer and handle it through GTM.
Add this to your child theme’s functions.php:
<?php
function push_cf7_to_datalayer() { ?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'cf7_submission',
formId: event.detail.contactFormId
});
}, false );
</script>
<?php }
add_action( 'wp_footer', 'push_cf7_to_datalayer' );Then in GTM, create a Custom Event trigger with the event name cf7_submission and connect it to a GA4 Event tag.
If you need both a redirect and event tracking, send the event first and then redirect using
setTimeoutwith a 1-2 second delay.
Additional Contact Form 7 DOM Events
Beyond wpcf7mailsent, the plugin provides several other events worth knowing:
| Event | When It Fires |
|---|---|
wpcf7submit | After every submission, regardless of the result |
wpcf7mailsent | After a successful mail send |
wpcf7mailfailed | When submission succeeds but mail sending fails |
wpcf7invalid | When fields contain invalid input |
wpcf7spam | When spam is detected |
Each event contains an event.detail object with useful data like contactFormId, inputs (field values), and unitTag.
FAQs
Common questions about redirecting and tracking Contact Form 7 submissions:
wp_footer hook), not in the header. Also check for JavaScript errors in the browser console and verify that the CF7 plugin is up to date.event.detail.contactFormId to identify which specific form was submitted, then set window.location.href conditionally based on the form ID.gtag() or through Google Tag Manager - no thank-you page redirect required.wpcf7submit event fires after every submission - even when validation fails or spam is detected. wpcf7mailsent only fires when the email is actually sent successfully, making it the right choice for conversion tracking and redirects.on_sent_ok setting was removed in Contact Form 7 version 5.0. The correct approach now is to use DOM events like wpcf7mailsent as shown in this guide.generate_lead for contact forms. This lets GA4 recognize the conversion type and display tailored data in your reports.Summary
I covered two things in this guide: how to redirect users to a new URL after a Contact Form 7 submission, and how to send events to Google Analytics 4 without needing a thank-you page redirect at all.
All examples use the wpcf7mailsent DOM event, which fires only on successful mail delivery. Whether you’re tracking conversions through gtag.js directly or via Google Tag Manager, the core pattern is the same – listen for the event and act on it.
If you’re working with Contact Form 7, you might also find these useful: optimizing CF7 for performance, preventing spam with Akismet, adding reCAPTCHA v3, and customizing the CF7 form design. For a deeper look at GA4 events and how conversions work, check out events in Google Analytics and setting up conversion tracking in GA4.

