Unlocking the potential of UTMs in your analytics

In this post we will build a solution to strengthen the consistency of your campaign tagging and marketing efforts so that they can transcend Google Analytics 4 and enable acquisition analysis from your backend tools.

Why this is worth doing

If you work with digital analytics, this will ring a bell: hours trying to explain why GA4 data doesn’t match your company’s backend data.

It is a structural issue in which there are some elements that can make your job as an analyst harder, such as:

  • Differences in attribution.
  • Loss of parameters during navigation.
  • Consent and cookie blocking.
  • Rewriting or disappearance of UTMs.

The result is also familiar: long and unproductive discussions with business, CRM, product or BI, generating frustration and loss of credibility of web analytics data as a basis for decision making.

While Google is addressing part of the problem by deploying Google Tag Gateway to use first-party cookies, this may not be enough to improve the quality of acquisition data, as:

  • It depends on consent.
  • It has duration limitations.
  • It may conflict with the auto-tagging of some platforms, such as Google Ads and the “gclid” parameter.
  • It is not intended to reliably power your backend.

If you need consistency between frontend and backend, you need to build something in-house that doesn’t rely on third-party tools. You never know when you might switch analytics tools or CMS/forms.

The focus: capturing and persisting the traffic source

In essence, the idea is simple: capture acquisition information on the first page the user views during their session and persist it with a first-party cookie that is 100% controlled by you.

To do this, we are going to use a script as a base that retrieves the information as follows, and in this order:

  1. If the landing page URL contains UTMs, then retrieve the parameters utm_medium, utm_source and utm_campaign, creating a concatenated string like this: “myMedium|mySource|myCampaign”.
  2. If the URL does not contain UTMs but contains the auto-tagging parameters gclid o dclid, then it will assign the values “cpc|google|not set” and “display|dbm|not set”, respectively.
  3. If the URL does not contain the above parameters, then it will search in the referrer of the landing page. We will do this in two steps:
    • First, look for recognized search engine domains, such as Google, Yahoo, Bing, etc. With this we will search for the organic search channel, generating values like this: “organic|google|not set”.
    • Second, search for domains that are not traditional search engines, obtaining values of the type “referral|referrerDomain|not set”.
  4. Finally, if none of the above is found, we will set the value “none|direct|not set” to classify it as direct traffic.
UTMs capturer script for GTM
UTM capturer script for GTM

Next, we generate a first-party cookie called mqr_msc (“msc” stands for medium, source, campaign), which we will set on the root domain of our website. The goal is that this cookie:

  • Work across subdomains: www.mydomain.com, blog.mydomain.com, or shop.mydomain.com. Here we will have the auxiliary functions getCookie, getRootDomain and setCookie that we will define before the main cookie creation script.
UTMs capturer. Auxiliary functions
UTMs capturer. Auxiliary functions
  • Survive multi-tab browsing, very frequent in eCommerce websites or with comparison between products / versions, as we did to measure the previous page in GA4.
  • Persist for a period of time that we can establish at our discretion. In this case, I have specified 1 day, but it can be 30 minutes, 1 hour, or whatever you consider more appropriate. I recommend here this article by Raúl Fernández Molina on data persistence.
UTMs capturer. Setting the cookie duration
UTMs capturer. Setting the cookie duration

This tag can be inserted as a custom HTML tag in Google Tag Manager or hardcoded so as not to depend on GTM. It is not a heavy tag.

Pushing UTMs to the backend: what really matters

Once the capture process is complete, it is time to “push” the captured value to our backend environment. Here it will depend on which CMS platform and forms you are using. The common idea would be to “inject” the captured value into the cookie when the form in question has loaded. So, if a user completes it successfully, the information will be passed to the backend in hidden fields. Some examples:

WooCommerce

To properly incorporate it into WooCommerce, the WordPress eCommerce plugin, we will need the WP Code plugin to be able to inject code efficiently. This should be done in three steps:

First, let’s add a CSS snippet that incorporates a hidden html element called my_source_field.

WooCommerce. CSS hidden field creation
WooCommerce. CSS hidden field creation

Secondly, we will inject the value of the mqr_msc cookie in that hidden field we created in the previous step with a JavaScript snippet.

WooCommerce JavaScript snippet to inject hidden fields values
WooCommerce JavaScript snippet to inject hidden fields values

And finally, we will add a PHP snippet that will take the information from that hidden field to a WooCommerce custom field.

WooCommerce PHP snippet for custom field injection
WooCommerce PHP snippet for custom field injection

Thus, it will become part of the meta information of your orders generated with WooCommerce.

UTMs in WooCommerce order meta information.
UTMs in WooCommerce order meta information.

Hubspot

Here the process is simple. Considering the different events of a Hubspot form, we would select the onFormReady to inject those hidden fields. In this screenshot, the GTM tag splits the cookie into 3 different fields previously defined in that form:

GTM custom html tag for Hubspot injection.
GTM custom html tag for Hubspot injection.

What this really means: from web analytics to backend analytics

Underlying this brief process is a model that is already present in many companies: extending the web analytics framework to all enterprise data analytics.

The web analytics framework needs this type of boost to overcome its limitations. It is, by definition, a partial analytics, since it is based on cookies and client-side measurement, depends on acceptance, is subject to blocking and can be subject to sampling in GA4.

Instead, bringing this web analytics to the backend would allow us to go back to work with near-population-level data, without the obstacle of adblockers or other limiting technologies such as ITP/ETP, much closer to business reality.

The idea is not to replace or eliminate GA4, but to reduce reliance on tools like it to reduce the noise in conversations about data and focus on what really matters: how to improve our business based on data.

Before we close: a note on GDPR and consent

This type of solution is not outside the scope of regulation. The initial intuition about this is “since it is a first-party cookie and does not contain personal data, no consent is needed”. It may not be that simple.

The AEPD in Spain, aligned with the European Data Protection Committee, makes it clear that:

  • Analytics cookies are not automatically exempt from consent, although they may be in very specific scenarios.
  • Even so, they should always be informed.

In any case, always document in your cookie policy the use you are going to give to this cookie that we have generated in this post, and always consult your company’s legal team beforehand about the approach to adopt in terms of consent.

Closing and files to download

These are just a couple of examples I wanted to share. And you? How do you transfer UTM parameters to your backend? I’m looking forward to seeing your solution.

You can download the code snippets in this file.

Leave a Comment