Recursive AI

Remove UTM parameters from a URL in C#

OneContact is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

Terminal
dotnet add package RecursiveAI.OneContact

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneContact;
 
var url = WebAddress.Parse(
    "https://example.com/?id=42"
    + "&utm_source=news");
Console.WriteLine(url.Canonical);

The URL keeps id=42:

Text
https://example.com/?id=42

Canonical also drops any #fragment and normalizes the host.

Unzip the example, then dotnet run.

Full program

C#
using RecursiveAI.OneContact;
 
var url = WebAddress.Parse(
    "https://example.com/?id=42&UTM_SOURCE=mail"
    + "&tag=a&tag=b#details");
Console.WriteLine(url.Canonical);
Console.WriteLine(url.Flags);

dotnet run prints:

Text
https://example.com/?id=42&tag=a&tag=b
TrackingParametersRemoved, FragmentRemoved

If the URL can’t be parsed, Parse throws ContactParseException; use TryParse and check IsWebsite when handling user-entered HTTP or HTTPS links.

Variations

When all query parameters are removed, OneContact also removes the ?:

C#
using RecursiveAI.OneContact;
 
var url = WebAddress.Parse(
    "https://example.com/?utm_source=mail");
Console.WriteLine(url.Canonical);
Text
https://example.com/

The short answer

Parse the URL with WebAddress.Parse and read Canonical. It removes known tracking parameters such as utm_source, utm_medium, and utm_campaign, while keeping parameters like id=42. The full program also shows that both tag entries survive. Don’t use Key as the cleaned URL: that property is for comparing addresses, not for sharing them.

What are UTM parameters used for?

UTM parameters are campaign labels carried in the query string. For example, utm_source=news can tell an analytics system that a visit came from a newsletter. Google’s campaign URL guide describes the labels and what they measure. By contrast, id=42 might tell the site which item to show. Erasing the whole query string would remove both.

OneContact matches names from a documented list, not every name beginning with utm_. The match ignores ASCII case, so UTM_SOURCE is removed too. The parser checks the spelling as written, without percent-decoding it: %75tm_source and a custom name such as utm_custom both stay. The list also includes known click identifiers such as gclid and fbclid.

Use Canonical when you want the normalized address and don’t need to keep a section anchor. It drops the fragment, such as #details, even if no tracking parameters were removed. It also normalizes the host and omits a default port. This is not a query-only edit. If the link needs to scroll to a specific section, keep the original text; the parsed value doesn’t store the fragment. See the URL and fragment behavior before replacing a saved link.

A signed URL may depend on its exact query or serialization. Keep those links as supplied rather than passing them through canonicalization. Likewise, removing known tags doesn’t remove identifiers in the path or under names outside the list, and it doesn’t prevent the destination from tracking a visit.

All onecontact recipes