We’ve all seen it. You open Google Analytics, head into your traffic acquisition reports, and find a mess. Capitalization has fragmented your mediums into “Social”, “social”, and “social-media”. Or worse, an important partner link is generating traffic, but the utm_medium is completely missing because someone forgot to append it to the URL.
The standard way to deal with this in Google Tag Manager can be messy. To solve this properly at the architecture level, I built a open-source Server-Side GTM variable template: The Dynamic UTM Resolver.
Why Handle This Server-Side?
Moving your tracking stack to Server-Side GTM (sGTM) gives you a single point of control. If you fix a broken UTM parameter inside the server container, you don’t just fix it for GA4, you fix it for any other downstream vendor at the exact same time.
The Dynamic UTM Resolver sits at the entry point of your server data stream. It intercepts the incoming request, checks if the UTM parameters are valid, and if they aren’t, it uses a set of clean fallback rules to resolve them before handing the data over to your tags.

What It Does (The Functionality)
The variable template operates on a simple, top-to-bottom rules table. Here is what it can do out of the box:
1. Map Referrers to UTMs
If a user arrives from linkedin.com but the link lacks UTM tags, the template can identify the referrer and automatically output utm_medium=social and utm_source=linkedin.
2. Translate Custom Query IDs (RegEx Extraction)
Many affiliate networks and regional ad platforms use proprietary URL parameters (like at_gd=12345) instead of standard UTMs. The template uses server-safe RE2 regular expressions to extract these dynamic IDs using capture groups ($1) and instantly map them to your utm_id.
3. Handle Behavior & Casing Safely
- No Overwrite by Default: It respects correct tagging. If a UTM parameter already exists in the URL, it leaves it alone, unless you explicitly set the row behavior to “Always override.”
- Smart Case-Insensitivity: Standard text matching (Equals/Contains) is automatically case-insensitive so “Domain” and “domain” match identical rules. However, the RegEx engine remains case-sensitive by default to protect proprietary, case-sensitive click IDs (though you can easily override this inline with
(?i)). - Global Lowercasing: You can force the final output string to lowercase to ensure your GA4 reports stay completely clean.
Getting Started
The template is fully self-contained and designed to be utilized within an Server-side GTM Augment Event Transformation, keeping your individual tags clean and uniform.
You can find the full documentation, permission requirements, and the source code ready to be imported into your container on the GitHub repository:

Be the first to comment on "Dynamic UTM Resolver for sGTM: Fix Broken Attribution"