Skip to content
Skip to content

Capturing Google Ad Campaign Info In A WPCF7 Contact Form

The Problem: 

A client is getting 20 leads a day through their contact form. Some of them more qualified than others. We want to determine if there's a correlation between certain campaigns and the qualities of the leads.

Contact form submission is being tracked by Google Ads (still hard not to say AdWords since they changed the name!) so we know which campaigns are generating leads. But there's no immediate way to tell which of those leads were qualified and which were time-wasters.

We know which campaigns are bringing in the most leads, but we want to know which are bringing in the best!

The Solution:

A field on the email you get that indicates which campaign led to that specific email.


- Google Ads
- Google AdWords Editor (will really speed up adding variables to ad urls) Turns out we don't need this any more as URL Options can be set at campaign level, with the added bonus of your ads not having to be resubmitted for approval.
- Contact Form 7
- Contact Form 7 Dynamic Text Extension
- A Child Theme (Optional, but you'll save yourself having to do this all over again when your theme and/or plugins get updated.

Super Simple Version:

1. Add variables to ad URLs that indicate which campaign brought a lead to the site.

We used 'cid' (which stands for campaign id) but you could use whatever you want so long as your consistent in the steps that follow.

  • In your Google Ads dashboard click into a Campaign, then Settings>Additional Settings>Campaign URL Options
  • Set up your marker. You can set up all sorts awesome dynamic variables to get more detail but we're going to keep it super simple and go with something like 'cid={campaignid}' which will attach the Campaign ID assigned by Google Ads to your final url. Or to make it even simpler, we can set the marker ourselves so in our example we'll set it to 'cid=LST' (standing for 'Lead Search Toronto'). That way we get an instantly recognizable string rather than a series of numbers we'd have to cross reference.
  • Hit the test button to see that you've set up your tracking correctly, then on to the contact form setup


2. Set up Contact Form 7 Dynamic Text Extension

3. Add your dynamic text field to your contact form.

Ultimately we're going to make this a hidden field but we'll leave it visible while we're testing. To do this we'll use the shortcode:

[dynamictext urldata "CF7_GET key='cid'"]


We'll put this in our wpcf7 contact form.


Baring in mind hear that if you didn't use 'cid' as your query parameter you need to change it in this shortcode too.

As with all Contact Form 7 fields, remember to add it to the 'Mail' tab as well as the 'Form' tab or you won't get any info in the message sent to you. So in the above example we'll want to add [urldata] to the Mail tab somewhere.

4. Test

Go to your page with the contact form adding a test query string (e.g.

You should see your dynamic text field is now filled with AWESOME

5. Make the field hidden


[dynamictext urldata "CF7_GET key='cid'"]


[dynamichidden urldata "CF7_GET key='cid'"]

in your contact form.

6. Go home, you're donezo.

Pretty straightforward, right?

The drawback to this method is that the string query will only exist on the page the user lands on, so if they navigate elsewhere on your site before deciding to contact you, you won't get the info.

More Thorough Version:

In order to get around the drawback of the above simple version above we're going to save some session variables and then create a custom callback to them in wpcf7.

1. Create A Session Variable

First we want to turn our cid query string above into a session variable that will follow the user from page to page while they're browsing your website. To do this we're going to start a session in the header of your site (put it in your child theme to avoid it being overwritten on update!)

function setseshvars(){
if (!isset($_SESSION['done'])) { // We only want to run this once per browsing session
$_SESSION['campaignid'] = $_GET["cid"]; // Captures the 'cid' that we placed in our Google ad final url and sets it as a session variable called 'campaignid'
$_SESSION['done'] = 1; // Sets a value for 'done' so this won't run again when we load another page.


2. Create A Shortcode to capture the session variable

For this we're going to want to set up a shortcode through our child theme's functions.php that we'll be putting in our contact form.

Here I'm using a modified version of one of the shortcodes set up by Contact Form 7 Dynamic Text Extension

/* Insert a $_SESSION variable */
function cf7_sess($atts){
'key' => 0,
), $atts));
$value = '';
if( isset( $_SESSION[$key] ) ){
$value = urldecode($_SESSION[$key]);
return $value;
add_shortcode('CF7_SESSION', 'cf7_sess');


Basically it's going to call a session variable instead of a query from the url.

3. Add your shortcode to the contact from.

Something like:

[dynamictext sessiondata "CF7_SESSION key='campaignid'"]

And don't forget to include [sessiondata] in the mail section somewhere so that it gets sent to you.

4. Test it out!

For the purposes of testing you could make your both use both the URL query string field ans the session data field and set the both to be visible by using the dynamictext shortcode.

Go to the site with a test query string again (e.g. then jump to your contact form. You should see that both of them show your variable (e.g. AWESOME)

Then go to another page that also has your contact form on it. You'll notice that the query string box is now empty but the session variable box remains set.

5. Make your fields hidden again.

Keep it all neat and tidy.

6. Buy yourself a beer, celebrate!

Good job! Now you're ready to capturing that sweet, sweet Google Ad campaign info in your contact form.

Other Options:

- You could use Contact Form 7's built in Mail 2 to send yourself two copies of the email, one with the added info and one without.
That way when you reply to the client you won't be sending any messy varible info.

- For bonus points you could use filters in gmail or gsuite to:
1. Separate your Mail 2 versions, making it clearer which ones are for data purposes (bypassing the inbox) and which one's are to be replied to.
2. Separate out mail by campaign.
A tidy inbox with mail filtered by campaign can tell you a story about what's going on even with just a glance.

- I'm toying around with using {loc_physical_ms}

{loc_physical_ms} is pretty cool, it can tell you where the physical click happened. The only issue is that it'll echo a numerical value that corresponds to a location in a table of locations Google has. I'd probably want that to correspond to the name of the place in the email. I'm thinking you could have hidden checkboxes, the value of which would be the loc_physical_ms numeric value, but I'd need to think about it a bit more.