Skip to content

UTM and query string handling

Query parameters (UTM tags, referral codes, etc.) must survive locale redirects. This is handled in server/middleware/geo-locale.ts by splitting pathname and search from getRequestURL(event) before any path matching or redirect target is constructed.

Why this requires explicit handling

In Nitro, getRequestPath() returns the raw request path including the query string. Without parsing the URL first, /?utm_source=google would not match the root check, and query parameters would be silently dropped when building redirect targets.

Behaviour by visitor type

Visitor type Entry URL Redirect handled by Query params preserved
First-time (no locale cookie) /?utm_source=google geo-locale.ts ✅ Always
First-time (no locale cookie) /da-dk/?utm_source=google geo-locale.ts ✅ Always
Returning (locale cookie set) /?utm_source=google nuxt-i18n Depends on nuxt-i18n
Returning (locale cookie set) /da-dk/?utm_source=google No redirect issued ✅ No redirect needed

For returning visitors hitting the root, geo-locale.ts skips (they have a stored locale cookie) and nuxt-i18n handles the redirect using that cookie. This is the same behaviour as before geo-locale.ts existed.

Test URLs

Use these on a Cloudflare Pages environment (staging or production) where CF geolocation headers are present. Clear cookies before each test and verify in the Network tab that the redirect target URL includes the full query string.

First-time visitor, root with UTM

/?utm_source=google&utm_medium=cpc&utm_campaign=test
Expected: → /{cf-locale}/?utm_source=google&utm_medium=cpc&utm_campaign=test

First-time visitor, wrong locale prefix with UTM

/da-dk/products?utm_source=facebook&utm_medium=social
Expected: → /{cf-locale}/products?utm_source=facebook&utm_medium=social

Returning visitor with confirmed locale, root with UTM

/?utm_source=email&utm_campaign=spring
Expected: → /da-dk/?utm_source=email&utm_campaign=spring (nuxt-i18n uses stored locale cookie)

Correct locale, no redirect

/en-it/products?utm_source=email&utm_campaign=spring
Expected: page renders directly, no redirect, query params remain in URL

Note: geo-locale.ts only responds to the real CF context. The ?cfcountry=XX dev override only affects the client-side modal in useLocaleDetection — it does not trigger the Nitro middleware.