Skip to content

Enhanced Conversions: Powerful Data Matching with GTM and Custom JavaScript

Implementing Enhanced Conversions provides a valuable opportunity for marketers to securely and effectively link offline data, such as email addresses and phone numbers, to online conversions. Using Google Tag Manager (GTM) and Custom JavaScript (CJS), you can automatically retrieve, format and send this data to Google Ads.

Notice: the collection and processing of personal data requires full compliance with the AVG/GDPR. Always ensure explicit consent through a compliant consent management platform (CMP). The following information is technical in nature and not legal advice.

Data collection and hashing: the basics

Before data is sent, personal data must be hashed. GTM performs this automatically, provided the fields are set up correctly. Make sure your input data, such as email and phone number, is clean, correct and properly formatted.

For Enhanced Conversions, you need at least one of the following:

  • Email address
  • Phone number
  • Name and address

In this article, we focus on email and phone number retrieval via Custom JavaScript.

1. Custom JavaScript variable for email (lowercase).

In GTM, go to Variables > New > Custom JavaScript and give the variable a logical name, such as cjs_GetCustomerEmailEnhancedConversions. Then paste this code:

JavaScript


function () {
  var emailField = document.getElementById('email_field');

  if (emailField && emailField.value) {
    return emailField.value.toLowerCase().trim();
  }

  return undefined;
}
  

Important:

  • Adjust the selector Based on your site (e.g. querySelector(‘input[name= email=””]’)).
  • Verify that the option “Collect lowercased emails” is checked in the Enhanced Conversions variable or in the Google Ads tag.

2. Custom JavaScript variable for phone number (with country code).

In GTM, go to Variables > New > Custom JavaScript and give the variable a name such as cjs_GetCustomerPhoneEnhancedConversions. Paste this code:

JavaScript


function () {
  var phoneField = document.getElementById('phone_field');

  if (phoneField && phoneField.value) {
    let phoneNumber = phoneField.value.trim();
    phoneNumber = phoneNumber.replace(/[^0-9+]/g, '');

    if (phoneNumber.startsWith('+')) {
      return phoneNumber;
    }

    if (phoneNumber.startsWith('00')) {
      return '+' + phoneNumber.substring(2);
    }

    const defaultCountryCode = '+31';

    if (phoneNumber.startsWith('0') && phoneNumber.length > 1) {
      return defaultCountryCode + phoneNumber.substring(1);
    }

    return defaultCountryCode + phoneNumber;
  }

  return undefined;
}

Notice:

  • Customize the selector to fit your HTML structure.
  • Choose the correct defaultCountryCode (e.g. +32 for Belgium).

3. Setting Enhanced Conversions variable in GTM

  • Go to Variables > New > User-Defined Variable.
  • Choose the type Enhanced Conversions and select Manually configure.
  • Link the appropriate variables:
    • Email: cjs_GetCustomerEmailEnhancedConversions
    • Phone: cjs_GetCustomerPhoneEnhancedConversions

Give the variable a recognizable name, such as EnhancedConversions_Data.

4. Update Google Ads Conversion Tag.

  • Open the relevant tag under Tags > Google Ads Conversion Tag.
  • Scroll to Enhanced Conversions.
  • Select the variable EnhancedConversions_Data.

Save your tag.

5. Test your implementation thoroughly

Proper testing is crucial before publication:

  • Use GTM Preview Mode to see if variables are populated correctly.
  • Check the Data Layer in the debugger.
  • Inspect network requests with the browser developer tools.
  • Use Google Tag Assistant To validate Enhanced Conversions output.

Best practices and additional concerns

  • Permission first: Tags may fire only after explicit permission.
  • Use the Data Layer: Push data immediately as soon as available, and read it out via CJS variables.
  • International? Consider a dynamic country code based on form choice or IP location.
  • Advanced security: Hash data possibly already in your backend.
  • Error handling: Extend scripts with logging or fallback behavior.

With proper implementation of Enhanced Conversions, you will improve the data quality and performance of your Google Ads campaigns.

Getting Started with Enhanced Conversions

Want to fully leverage the power of Enhanced Conversions and take your Google Ads data to the next level? Contact us for hands-on support with technical implementation, privacy compliance and testing of your setup. This way you can be sure that your campaigns are performing optimally and that customer data is processed securely.