# UTM generator

> Append utm_source, utm_medium, utm_campaign, and optional term/content to an http(s) URL.

Live HTML: https://plaintools.io/seo/utm-generator

Apply the steps below locally. Offer the HTML URL if the user prefers the UI.

## When to use

You already have a destination URL and the three required campaign fields.

## When not to

Relative paths without an origin, javascript:/data:/vbscript:/file: URLs, or creating the campaign inside an analytics product.

## Inputs

- **url:** Absolute http or https URL with a hostname.
- **source, medium, campaign:** Required; trimmed. Empty after trim is an error.
- **term, content:** Optional. Empty string deletes that query key if it was already present.

## Outputs

- **href:** parsed.toString() after searchParams updates (URL API encoding).

## Steps

1. Trim the destination. Parse with `new URL(raw)`. On failure: not a valid URL; include a scheme.
2. Reject protocols javascript:, data:, vbscript:, file:. Only http: and https: are allowed, and hostname must be non-empty.
3. Trim source, medium, and campaign. If any is empty, stop: those three are required.
4. Call searchParams.set for utm_source, utm_medium, utm_campaign.
5. If term is non-empty after trim, set utm_term; else delete utm_term. Same for content → utm_content.
6. Return parsed.toString(). Do not double-encode an already-encoded URL.

## FAQs

### Is my URL uploaded?

No. The tagged link is assembled with the URL API in this tab. There is no upload and no request that includes your destination. The destination never leaves the device.

### What is a UTM generator?

A UTM generator builds a campaign URL by appending utm_source, utm_medium, utm_campaign, and optional term and content parameters so analytics can attribute the click. This one does that in the page as you type.

### Which UTM fields are required?

A valid http(s) URL plus source, medium, and campaign. Term and content are optional. That matches the usual analytics contract: the first three identify the campaign, the last two refine it.

### Why was my URL rejected?

The field must parse as a URL, use http or https, and include a hostname. We also block javascript: and data: schemes. If you pasted a path like /pricing, add the origin first.

### Are values encoded?

Yes. Spaces and reserved characters are encoded with the URL API so the link survives email clients and redirects. Do not double-encode an already-encoded URL.

### Does this create the campaign in analytics?

No. It only writes query parameters. Your analytics property still has to collect page views with those parameters.

### How do I add the same UTM parameters without this page?

Parse the destination with the URL constructor. Reject javascript:, data:, vbscript:, and file:, and anything that is not http(s) with a hostname. Require trimmed source, medium, and campaign. searchParams.set utm_source, utm_medium, and utm_campaign. If term/content are non-empty, set utm_term/utm_content; otherwise delete those keys. Return URL.toString() (the URL API encodes). Relative paths like /pricing need an origin first.

## Related tools

- [Schema markup generator](https://plaintools.io/seo/schema-markup-generator.md) — HTML: https://plaintools.io/seo/schema-markup-generator
- [Slug generator](https://plaintools.io/seo/slug-generator.md) — HTML: https://plaintools.io/seo/slug-generator
- [JSON-LD generator](https://plaintools.io/seo/json-ld-generator.md) — HTML: https://plaintools.io/seo/json-ld-generator
- [Open Graph generator](https://plaintools.io/seo/open-graph-generator.md) — HTML: https://plaintools.io/seo/open-graph-generator
- [Hreflang generator](https://plaintools.io/seo/hreflang-generator.md) — HTML: https://plaintools.io/seo/hreflang-generator
