Leveraging Google Tag Manager to Pass UTM Parameters Across Pages and Populate Hidden Form Fields


Google Tag Manager (GTM) has emerged as a powerful tool that simplifies the process of managing and deploying various tracking codes on a website. One common requirement for marketers is to capture UTM parameters and pass them seamlessly across pages into hidden form fields. This enables a more comprehensive understanding of user behavior and the effectiveness of marketing efforts.

Understanding UTM Parameters

UTM parameters, short for Urchin Tracking Module, are tags appended to a URL that help track the source, medium, campaign name, and other information associated with a user’s visit to a website. The standard UTM parameters include:

  1. utm_source: Identifies the source of the traffic (e.g., Google, Facebook, email).
  2. utm_medium: Specifies the marketing medium (e.g., cpc, banner, email).
  3. utm_campaign: Indicates the name of the campaign that led the user to the website.
  4. utm_term: Used for tracking keywords in paid search.
  5. utm_content: Differentiates between similar content, such as different ads within the same campaign.

These parameters are essential for analyzing the performance of marketing campaigns and understanding the user journey.

Setting up Google Tag Manager

Before delving into custom JavaScript, ensure that Google Tag Manager is properly set up on your website. Follow these steps:

Create a Google Tag Manager Account: Go to the Google Tag Manager website, sign in with your Google account, and create a new account for your website.

Create a Container: Within the account, create a container for your website. This container holds all the tags, triggers, and variables.

Install the GTM Code: After creating the container, Google Tag Manager provides a code snippet. Copy and paste this snippet into theof your website.

Set up a Page View Trigger: In the GTM interface, create a trigger that fires on the page view event. This trigger will be used to initiate our custom JavaScript when a page is loaded.

Creating Custom JavaScript to Capture UTM Parameters

Once Google Tag Manager is set up, the next step is to create custom JavaScript code that captures UTM parameters from the URL and stores them in GTM variables. Follow these steps:

  1. Create GTM Variables for UTM Parameters:
  • In the GTM interface, navigate to “Variables” and create a new user-defined variable for each UTM parameter (e.g., utm_source, utm_medium, utm_campaign, etc.).
  • Use the built-in “URL” variable type and specify the appropriate query parameter for each variable (e.g., for utm_source, set the query key to “utm_source”).

2. Create a Custom JavaScript Variable:

  • In GTM, create a new user-defined variable of type “JavaScript Variable.”
  • Write a JavaScript function to extract the UTM parameter values from the URL. For example:

function() {
var urlParams = new URLSearchParams(window.location.search);
return urlParams.get(‘utm_source’);

Repeat this process for each UTM parameter, modifying the JavaScript function accordingly.

Testing the Variables:

    • Preview your GTM container to test whether the variables are capturing the correct UTM parameter values on different pages.

Passing UTM Parameters to Hidden Form Fields

Now that we have successfully captured UTM parameters using custom JavaScript and GTM variables, the next step is to pass this information into hidden form fields. This ensures that when a user interacts with a form, the associated UTM data is submitted along with their input. Follow these steps:

Create Hidden Form Fields:

  • In your website’s HTML, identify the form where you want to capture UTM data.
  • Add hidden input fields for each UTM parameter:

<input type=”hidden” name=”utm_source” id=”utm_source” value=””>
<input type=”hidden” name=”utm_medium” id=”utm_medium” value=””>
<input type=”hidden” name=”utm_campaign” id=”utm_campaign” value=””>
<!– Add additional hidden fields for other UTM parameters as needed –>

Update the Custom JavaScript Variable:

Modify the custom JavaScript variable created in GTM to set the values of the hidden form fields. For example:

function() {
var urlParams = new URLSearchParams(window.location.search);
document.getElementById(‘utm_source’).value = urlParams.get(‘utm_source’);
document.getElementById(‘utm_medium’).value = urlParams.get(‘utm_medium’);
document.getElementById(‘utm_campaign’).value = urlParams.get(‘utm_campaign’);
// Set values for other UTM parameters as needed

Testing the Implementation:

  • Preview your GTM container and test the form submission. Verify that the hidden form fields are populated with the correct UTM parameter values.


Incorporating Google Tag Manager and custom JavaScript to capture and pass UTM parameters into hidden form fields enhances the depth of data collected from user interactions on your website. This approach provides marketers with valuable insights into the effectiveness of various marketing channels and campaigns. By implementing these steps, you empower your analytics efforts, enabling data-driven decisions that can significantly impact the success of your online initiatives. Stay proactive in adapting and optimizing your tracking strategies to stay ahead in the ever-evolving landscape of digital marketing.

Leave a Reply

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