Why a clean address bar can erase campaign source
A marketing link lands on `/pricing?utm_source=newsletter&utm_medium=email&utm_campaign=november`. The page quickly removes those parameters with `history.replaceState` so the address looks cleaner. That can be fine for navigation, but timing matters: if the analytics tracker has not recorded its first page view yet, it may never see the campaign values. The result is a session that appears under Unknown even though the original link had valid UTMs.
WhoVisited's tracker is loaded asynchronously in the standard installation snippet. When it creates a page-view event, it reads `utm_source`, `utm_medium` and `utm_campaign` from the current address. Ingestion uses the first accepted event to create the session and store its acquisition fields; later events update the session activity but do not replace its original source. Removing the query before that first event is constructed can therefore leave the session without the campaign attribution.
This is different from a redirect dropping the tags before a visitor reaches your page. A redirect problem must be fixed in the redirect chain; a client-side cleanup race happens after the final page has loaded. In both cases, the useful check is the final landing URL and the campaign values on the first accepted page-view event.
Remove only the tracked parameters after the tracker runs
If your app controls tracker loading and analytics is allowed to run immediately, attach a load listener before setting the script URL. A script element's load event fires after its resource has loaded and executed successfully. The tracker creates its initial page-view event during execution, so the listener can then remove the three campaign keys from the visible URL while preserving unrelated parameters and the fragment.
function loadWhoVisitedThenCleanUrl(siteId) {
const script = document.createElement("script");
script.async = true;
script.dataset.siteId = siteId;
script.addEventListener("load", () => {
const url = new URL(window.location.href);
["utm_source", "utm_medium", "utm_campaign"].forEach((key) => {
url.searchParams.delete(key);
});
history.replaceState(
history.state,
"",
url.pathname + url.search + url.hash
);
}, { once: true });
script.src = "https://whovisited.net/tracker.js";
document.head.append(script);
}
loadWhoVisitedThenCleanUrl("YOUR_SITE_ID");Registering the listener before assigning `src` avoids missing a fast load. The sample removes only the three lowercase keys the current tracker reads; it keeps other query parameters and the hash. Adapt the loader to your framework, allowed origin and Content Security Policy. Do not add a second copy if the installation snippet already loads the tracker.
This sequence captures the campaign values in the page-view event; it does not guarantee that the network request will be accepted. Check the event request and the resulting session in WhoVisited. Collection can still be blocked by consent, a browser privacy signal, origin settings, an inactive subscription, a blocker or a network failure.
Keep the URL intact while consent is pending
If the tracker uses `data-consent="required"`, loading the JavaScript does not create a page-view event while consent is denied or undecided. In that case, a script load listener is too early to clean the URL: when your consent manager later calls `window.whovisited.consent(true)`, the tracker creates the first page view from the URL that exists at that moment. Keep the UTM values until analytics is actually allowed and that call has run.
Do not turn a campaign tag into an excuse to start analytics before the visitor's choice. If analytics remains denied, leave the URL as it is or remove only the tags after your own application has captured whatever non-analytics state it legitimately needs. WhoVisited does not provide a public method to inject campaign attribution later, and the analytics script's load event is not a consent signal.
Verify the first event in a fresh session
- Use a clean test browser profile and a synthetic campaign URL with all three supported lowercase UTM parameters.
- Open DevTools Network before navigation. Confirm the final page keeps the UTMs until the tracker executes; inspect the first `/api/v1/events` request and its first page-view event for the expected campaign object.
- After the URL cleanup runs, confirm unrelated query parameters and the fragment remain. Verify that the page did not reload and that no second tracker snippet was added.
- Check the event response and the session's source in the dashboard. A script load or a page-view request alone does not prove the session was accepted with the expected campaign fields.
- Repeat with `data-consent="required"`: before consent, expect no collection; after the consent manager grants analytics, make sure it calls `consent(true)` while the UTM values still remain in the address.
Use a new session for each test. WhoVisited stores acquisition data when a session is first created, so revisiting a URL with different tags during an existing session will not rewrite that session's original source.
Limits and references
Removing UTMs from the browser address does not remove them from browser history, server logs or other tags that already read the URL. It also cannot recover campaign information that was dropped by a redirect or blocked before the first page view. Use the cleanup only when a cleaner address is useful, and preserve the values until the measurement you rely on has been captured.
MDN documents the script element's load event and the History API's `replaceState` method. For campaign setup and consent behavior, consult the WhoVisited UTM redirect tutorial and consent integration article. Product details above reflect the current tracker and ingestion code; a successful browser-side capture is still best-effort analytics, not an authoritative transaction record.