If a WordPress website is using Contact Form 7 for form submissions, you might want to demonstrate that a form was submitted by a visitor from a Google Advert. Google uses a GCLID (Google Click ID) to keep track of users from ads. So we can grab the GCLID and send it in the form submission email so that the recipient can quickly see if it came from an advert.
Firstly, using the Code Snippets plugin (or similar), set a script at the end of the site body which collects the GCLID on first visit and places it inside a first party cookie called “firstpartygclid”, so that it is still easily accessed if the user navigates to another page without the cookie parameter:
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const urlParams = new URLSearchParams(window.location.search);
const gclid = urlParams.get('gclid');
if (gclid) {
const date = new Date();
date.setTime(date.getTime() + (30*24*60*60*1000)); // Set the cookie to expire after number of seconds
document.cookie = `firstpartygclid=${gclid}; expires=${date.toGMTString()}; path=/`;
}
});
</script>
You can test this works by adding a fake GCLID parameter to a URL, so something like: https://yourdomain/testpage/?gclid=123xyz
After visiting that, you can use your Chrome browser inspect tool and in the Applications tab, Storage / Cookies / Yourdomain, you’ll find the cookie with the value set, like this image:

Then make a new function, again with Code Snippets plugin if you are using it, or in your child theme functions.php, that creates a new special mail tag shortcode that you can insert in the email CF7 sends upon form submission:
add_filter( 'wpcf7_special_mail_tags', 'wpcf7_my_cookie_mailtag', 10, 3 );
function wpcf7_my_cookie_mailtag( $output, $name, $html ) {
if ( '_my_cookie_mail_tag' != $name ) { // rename the shortcode tag name to whatever;
return $output;
}
if ( ! $contact_form = WPCF7_ContactForm::get_current() ) {
return $output;
}
$val = isset($_COOKIE['firstpartygclid']) ? $_COOKIE['firstpartygclid'] : '';
return $html ? esc_html($val) : $val;
}
Finally, add the shortcode tag to your CF7’s form mail settings using [_my_cookie_mail_tag], or whatever you called it in the function like in the Message body in the image below (ignore the error – that disappears when you use your correct domain):

Hi! I’m trying to implement this, without success. Do you have an updated / extended version of this guide ? It would be very helpful! Thanks!
Which bit are you having trouble with? The cookie, or the form?