| |

How to Capture GCLID in WordPress Contact Form 7

A Google Click ID—GCLID—can connect a WordPress lead with the Google Ads click that generated it. Capturing the identifier in a form is useful when the valuable conversion happens later in a CRM, on the phone or after a sales conversation.

This guide shows a practical Contact Form 7 implementation. It captures gclid, gbraid, wbraid and selected UTM parameters, carries them across the visit and adds them to the form submission.

Important: the code only captures attribution values. It does not upload an offline conversion to Google Ads. You still need a CRM, Google Sheet, Data Manager connection or another controlled process for returning qualified-lead or sale data.

What GCLID is and why it matters

When Google Ads auto-tagging is enabled, Google can append a parameter similar to this to the landing-page URL:

https://example.com/contact/?gclid=example-click-id

Saving that identifier with the lead lets you associate a later offline outcome with the originating click. Google also uses gbraid and wbraid in some privacy-constrained environments, so a modern implementation should be prepared to retain those values too.

Google currently recommends enhanced conversions for leads for new offline-measurement setups. Click identifiers remain useful match keys, but the wider implementation should follow the current Google Ads Data Manager and enhanced-conversions guidance.

Before adding any code

  • Confirm that Google Ads auto-tagging is enabled.
  • Test that redirects, caching rules and security plugins do not remove query parameters.
  • Decide whether you want first-touch or last-touch attribution.
  • Review the setup with your privacy adviser or consent-management provider.
  • Do not capture the entire query string indiscriminately. URLs can contain data you did not intend to store.

The example below stores only a defined allow-list. In the EEA and UK, do not run advertising-storage code before the required consent signal. Configure the script through your consent-management platform rather than treating server-side or first-party storage as a consent bypass.

Step 1: Add hidden fields to Contact Form 7

Open the relevant form under Contact → Contact Forms and add these fields before the submit button:

[hidden gclid id:gclid]
[hidden gbraid id:gbraid]
[hidden wbraid id:wbraid]
[hidden utm_source id:utm_source]
[hidden utm_medium id:utm_medium]
[hidden utm_campaign id:utm_campaign]
[hidden landing_page id:landing_page]

Add the corresponding mail tags—such as [gclid] and [utm_campaign]—to the Contact Form 7 email template if email is part of your lead workflow. A CRM or database integration is preferable for dependable offline-conversion processing.

Step 2: Capture and populate the attribution fields

Add the following JavaScript through a controlled snippets plugin, your child theme or Google Tag Manager. If consent is required, make the relevant consent state a condition for running it.

<script>
(() => {
  const storageKey = 'lead_attribution';
  const allowed = [
    'gclid', 'gbraid', 'wbraid',
    'utm_source', 'utm_medium', 'utm_campaign'
  ];

  const params = new URLSearchParams(window.location.search);
  let saved = {};

  try {
    saved = JSON.parse(localStorage.getItem(storageKey) || '{}');
  } catch (error) {
    saved = {};
  }

  // First-touch attribution: retain the first non-empty value observed.
  allowed.forEach((key) => {
    const value = params.get(key);
    if (value && !saved[key]) saved[key] = value;
  });

  if (!saved.landing_page) {
    saved.landing_page = window.location.origin + window.location.pathname;
  }

  localStorage.setItem(storageKey, JSON.stringify(saved));

  const populateForms = () => {
    Object.entries(saved).forEach(([key, value]) => {
      document.querySelectorAll(
        'input[name="' + key + '"]'
      ).forEach((field) => {
        field.value = value;
      });
    });
  };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', populateForms);
  } else {
    populateForms();
  }
})();
</script>

This uses first-touch behaviour: an existing value is not overwritten by a later visit. For last-touch attribution, replace if (value && !saved[key]) with if (value).

Step 3: Store the values with the lead

A click identifier is useful only if it remains attached to the correct lead and later sales outcome. Send the allowed fields into your CRM, lead-management platform or structured Google Sheet alongside a stable lead ID and timestamps.

Treat hidden form values as untrusted user input: visitors can edit them. Sanitize the values server-side, restrict lengths and never use a GCLID or UTM parameter for authentication or access control.

Step 4: Return qualified conversions to Google Ads

Do not upload every form submission as a high-value conversion if only some become customers. Return the deeper outcome—such as qualified lead, appointment, contract or sale—using the conversion name, conversion time and permitted match data required by your chosen Google Ads workflow.

  • Google Ads Data Manager: suitable for scheduled or connected data sources.
  • Enhanced conversions for leads: Google’s recommended direction for durable lead measurement using permitted first-party data.
  • CRM or automation integration: useful when lead stages already live in HubSpot, Salesforce, Zapier or another platform.
  • Manual test upload: useful for validating field names and timestamps before automating the process.

Google’s upload methods and API requirements change. Check the current documentation before building an automation; legacy Google Ads API upload routes began migrating to the Data Manager API in 2026.

How to test the implementation

  1. Open a private browser window after applying the appropriate consent state.
  2. Visit the form through a test URL containing a recognisable gclid and UTM values.
  3. Navigate to another page before opening the form.
  4. Inspect the hidden fields in the browser and submit a test lead.
  5. Confirm the same values arrive in the email, CRM or sheet.
  6. Verify that a later offline upload is accepted and attributed in Google Ads diagnostics.

Common reasons GCLID capture fails

  • A redirect removes the query string before the landing page loads.
  • The script runs before consent, is blocked, or never runs after consent is granted.
  • The Contact Form 7 field name does not match the JavaScript key.
  • A multi-step form rebuilds its fields after the initial population.
  • Caching or optimisation changes the script execution order.
  • The identifier is emailed but never saved against the CRM record used for offline conversion import.

Need the complete tracking workflow?

I can connect WordPress forms, Google Tag Manager, GA4, Google Ads and your CRM or lead-management system as part of a tracking implementation. The work includes testing consent behaviour and validating the final offline-conversion flow, not merely adding a hidden field.

Official Google guidance

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *