GA4 events for forms and scroll depth in WordPress
Google Analytics 4 replaces Universal Analytics, which stops processing data on July 1, 2023. GA4 counts page views and, with Enhanced Measurement, also scrolls, outbound clicks, site search, file downloads and form interactions without extra code. Custom events are needed only for what these automatic events do not see, such as a Contact Form 7 form that was really sent.
Problem: The site wants to know how many visitors send the contact form and how far they read the long guides, and the automatic GA4 events report form “starts” and only one scroll mark at 90 percent.
Solution: Load the GA4 tag once in wp_head, check which Enhanced Measurement events are already on, and send custom events with gtag( 'event', ... ): generate_lead on the Contact Form 7 event wpcf7mailsent, and scroll marks at 25, 50 and 75 percent.
How to add the tag in general: add Google Analytics to WordPress.
The tag
add_action( 'wp_head', function () {
if ( is_user_logged_in() && current_user_can( 'edit_posts' ) ) {
return; // do not count the editors
}
$id = 'G-XXXXXXXXXX';
?>
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $id ); ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?php echo esc_js( $id ); ?>');
</script>
<?php
}, 5 );
In the GA4 property, Admin, Data streams, the web stream: Enhanced Measurement lists the automatic events. Leave on what you use; for scrolls and forms below, decide whether you want both the automatic and the custom events.
A form that was really sent
document.addEventListener( 'wpcf7mailsent', ( event ) => {
if ( typeof gtag !== 'function' ) {
return;
}
gtag( 'event', 'generate_lead', {
form_id: String( event.detail.contactFormId ),
page_location: window.location.href,
} );
} );
Contact Form 7 fires wpcf7mailsent only after the mail was accepted by the server; a plain submit listener would also count forms with validation errors. generate_lead is one of Google’s recommended event names and can be marked as a conversion in the GA4 admin. Never send the form fields themselves: personal data in Analytics is against Google’s terms.
Scroll depth
( function () {
const marks = [ 25, 50, 75 ];
const sent = new Set();
const onScroll = () => {
const doc = document.documentElement;
const percent = ( ( window.scrollY + window.innerHeight ) / doc.scrollHeight ) * 100;
marks.forEach( ( mark ) => {
if ( percent >= mark && ! sent.has( mark ) && typeof gtag === 'function' ) {
sent.add( mark );
gtag( 'event', 'scroll_depth', { percent_scrolled: mark } );
}
} );
if ( sent.size === marks.length ) {
window.removeEventListener( 'scroll', onScroll );
}
};
window.addEventListener( 'scroll', onScroll, { passive: true } );
} )();
Each mark is sent once per page view, and the listener is removed after the last one. To see percent_scrolled in reports, register it as a custom dimension (Admin, Custom definitions, scope “Event”).
Enqueue
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script( 'ha-ga-events', get_theme_file_uri( 'js/ga-events.js' ), [], '1.0.0', true );
} );
NOTE: In the EU and the UK, Analytics needs the visitor’s consent. With a consent banner, load the tag only after consent, or use Google’s consent mode, and test the events in GA4’s DebugView (Admin, DebugView) with the Tag Assistant extension before relying on the numbers.