How to Persist UTM Parameters Across Pages: Cookie vs sessionStorage vs localStorage
UTM values sit on the landing URL only, so they are gone by the form page. This guide compares cookies, sessionStorage and localStorage by tab scope, subdomain scope, lifetime and Safari behaviour, and gives a storage spec to hand to your developer.
In short
Save the UTM values when the visitor lands, because the query string exists only on that first page. Use a first-party cookie set on your parent domain with path=/ and a max-age if visitors may open new tabs, switch subdomains or return later. sessionStorage dies with the tab, and localStorage stays on one host.
UTM parameters live in the address of the page a visitor lands on and nowhere else. The moment they click through to your pricing page, the query string is gone, so a script that reads the URL on the form page finds nothing. To persist UTM parameters across pages, you copy them into browser storage on arrival and read them back later. There are three places to put them: sessionStorage, localStorage and a first-party cookie. They differ in which tabs, which subdomains and how many days they cover, and Safari shortens two of them. This guide compares the three and works through the cases where the choice decides whether the source survives, so you can tell whoever edits your site exactly what to build.
Why the values disappear after the first page
A URL such as example.com/landing?utm_source=meta&utm_medium=paid_social belongs to one page load. Your navigation links point to clean URLs, so the next page has an empty query string. Analytics tools cope because their own script records the campaign when the session starts. Your forms, chat widgets and booking tools have no such memory unless you give them one. Adding the UTMs to every internal link is not the fix: it rewrites the source in your analytics, as explained in why you should never put UTM parameters on internal links. Storage is the fix, and the only real decision is which kind.
Cookie vs sessionStorage vs localStorage at a glance
| Question | sessionStorage | localStorage | Cookie with domain=example.com |
|---|---|---|---|
| Shared between tabs? | No. Each tab has its own store, and a new tab starts empty | Yes, every tab on the same origin | Yes, every tab |
| Readable on other subdomains? | No. www.example.com and shop.example.com are separate origins | No, for the same reason | Yes. Naming a domain includes all of its subdomains |
| Survives closing the tab? | No. Closing the tab clears it | Yes | Yes, if max-age or expires is set. Without either it is a session cookie |
| Lifetime in Chrome and Firefox | Until the tab closes | No expiry until the visitor or browser clears it | Whatever you set, capped at 400 days in Chrome |
| Lifetime in Safari | Until the tab closes | Deleted after 7 days of Safari use without interaction on your site | Capped at 7 days when written by JavaScript, 24 hours on some landings from tracked links |
| Sent to your web server? | No | No | Yes, with requests to matching hosts |
Two points sit behind the table. First, an origin is the protocol, host and port together, so web storage written on www.example.com is invisible on example.com without the www and on app.example.com. Cookies use a looser rule: a page can set a cookie for its own host or a parent domain. Second, no storage crosses to a different registered domain. If your landing pages run on brandpages.com and the form sits on brand.com, the values have to travel in the link that crosses between them, the same problem GA4 cross-domain tracking solves with its _gl parameter.
Case 1: the visitor opens a second tab
People compare before they enquire. A visitor lands from a Google ad on a service page, opens the pricing page in a new tab and fills in the quote form there. With sessionStorage, that new tab starts with an empty store: MDN states that opening a page in a new tab or window creates a new session. There is a narrow exception for a page opened by script that keeps a reference to the page that opened it, which starts with a copy. Ordinary links with target=_blank behave as if rel=noopener had been set, which removes that reference, so treat every new tab as empty. localStorage and cookies are shared by all tabs on the same host, so the form tab reads the values the landing tab wrote.
Case 2: the form lives on another subdomain
A common setup: landing pages are built in a page builder on go.example.com, the form sits on www.example.com, or checkout runs on shop.example.com. Web storage written on go.example.com cannot be read on www.example.com, because each host is its own origin. Only a cookie written with domain=example.com and path=/ is visible on both, because MDN states that when a domain is specified, subdomains are always included. Leave the domain attribute out and the cookie belongs to the exact host that wrote it, which leaves you with the same single-host limit as web storage. Make sure the capture script also runs on every subdomain that can be a landing page, because a cookie can only be written by a page the visitor actually loads.
Case 3: the visitor comes back next week
sessionStorage is gone as soon as the tab closes, so it cannot serve a lead who returns days later. localStorage has no expiry in Chrome and Firefox, and a cookie lasts as long as its max-age. Safari is where plans break, and the two remaining options break differently. Take a visitor on an iPhone who clicks a Meta ad on day 1. The landing URL carries utm_source=meta, and your script writes a first-touch cookie with a 90-day max-age that, by your first-touch rule, it never touches again.
- Day 1: Safari stores the cookie, but because JavaScript created it, the expiry is capped at 7 days rather than 90. If the click came through a domain that ITP has classified as having cross-site tracking capabilities and the landing URL has a query string, which the UTM tags alone provide, WebKit caps JavaScript cookies created on that landing page at 24 hours.
- Day 4: the visitor returns from a Google search. Nothing is tagged, so nothing is written. The cookie is still there if the 7-day cap applied, and already gone if the 24-hour cap did.
- Day 10: the visitor returns from your newsletter. The cookie expired on day 8, the script finds empty storage, and the newsletter is saved as first touch. The lead record says email when a Meta ad made the introduction.
The same values in localStorage fare differently. WebKit deletes script-writeable storage, localStorage included, after seven days of Safari use without user interaction on your site. A visitor who clicked around on day 4 restarted that count, so on day 10 the original values are still there. Two habits limit the damage for cookies: rewrite the stored values, unchanged, on every page view so each visit sets a fresh expiry, and keep a copy in localStorage as a fallback on the host that wrote it. Neither survives a Safari visitor who stays away for more than a week. Both caps are explained in detail in Safari ITP and cookie lifetimes.
What to ask your developer to build
For most lead generation sites the right answer is a first-party cookie on the parent domain with a localStorage copy as a backup. sessionStorage only fits a site on a single host where the form is reached in the same tab and the same visit, which describes few real journeys. Hand over this specification:
- 1On every page, read the query string. If it contains any utm_ parameter or a click ID such as gclid, fbclid, ttclid or msclkid, treat the page view as a new tagged arrival.
- 2Keep two sets of values: a first-touch set, written only if it does not exist yet, and a last-touch set, replaced on each tagged arrival. Never replace a stored value with an empty one. Why both sets matter is covered in first-touch vs last-touch attribution.
- 3Write each set as a cookie with domain=example.com (your parent domain, without www), path=/, max-age=7776000 (90 days in seconds, or the length of your sales cycle), SameSite=Lax and Secure.
- 4Store the landing page path and the arrival date with each set, so any value can be checked against the campaign calendar later.
- 5On every page view, rewrite both cookies with their current values to restart the expiry, and mirror them to localStorage. When reading, use the cookie first and fall back to localStorage.
- 6Run the script only when the visitor's consent choice allows it, if your banner treats attribution storage as marketing.
- 7Read the stored values wherever they are needed. Writing them into a form is covered in capturing UTM parameters in hidden form fields.
How to test that the values survive
Build a test link with obviously fake values, for example utm_source=storage-test and utm_campaign=persist-check, using the UTM builder. Then run these checks in a Chrome Incognito window, so earlier visits do not interfere:
- Land on the tagged URL and open the browser's developer tools. In Chrome, the Application panel lists Cookies and Local storage under Storage: the cookie should show your parent domain and the copy should appear under the landing host.
- Click two internal links and confirm the address bar shows clean URLs while storage still holds the test values.
- Open the form page in a new tab and confirm the values can be read there.
- Visit a second subdomain, if you have one. The cookie should be present and the localStorage copy absent, which is expected.
- Return through a different tagged link. The last-touch values should change and the first-touch values should not.
- Return through an untagged visit. Nothing should change.
- Repeat the journey on an iPhone in Safari with fresh test values, where the storage limits above are most likely to show up. Use a normal tab, not Private Browsing: Safari gives each private tab its own ephemeral session, so the new-tab check would fail there for reasons that do not apply to real visitors.
Consent applies to all three
Moving campaign values from a cookie into localStorage does not change your consent position. EU and UK rules cover storing information on a visitor's device in general, not cookies alone, and the UK regulator's guidance on storage and access technologies names local storage explicitly. Storage for marketing attribution is unlikely to count as strictly necessary, so the capture script should follow the visitor's choice in your consent banner, and your privacy notice should mention it. What the banner itself has to offer is set out in GDPR cookie banner requirements. Check the final setup with your own counsel.
Frequently asked questions
How long should a UTM cookie last?
Match it to how long your leads take to convert. A lead generation site where most enquiries arrive within a fortnight gains little from a year-long cookie, while a considered purchase may need 90 days or more. Chrome caps any requested lifetime at 400 days, and Safari caps cookies written by JavaScript at seven days whatever you ask for, so the setting mostly matters for visitors on other browsers.
Is localStorage more private than a cookie?
Not in any way that matters for consent. Both are information stored on the visitor's device, and both can be read by any script running on your pages, because cookies written with JavaScript cannot be marked HttpOnly. The real difference is that a cookie travels to your web server with matching requests and localStorage does not, so localStorage keeps campaign values out of server logs but not out of reach of third-party scripts.
Should the cookie be set by the server instead of JavaScript?
It can help in Safari. WebKit's seven-day cap from Intelligent Tracking Prevention 2.1 applies to cookies created through document.cookie, not to cookies your own web server sets in its response. WebKit does cap server-set cookies at seven days when they come from a third-party server behind a CNAME record or a third-party IP address, so this only works if the server answering is genuinely yours. It needs a developer and a change to your hosting.
Sources
- 1.Window: sessionStorage property - MDN Web Docs
- 2.Document: cookie property - MDN Web Docs
- 3.Tracking Prevention in WebKit - WebKit
- 4.Intelligent Tracking Prevention 2.1 - WebKit
- 5.What are storage and access technologies? - ICO
- 6.Cookie Expires and Max-Age attributes now have upper limit - Chrome for Developers
Try EndFrame
Add a server-side path for your conversions.
EndFrame collects events first-party and forwards conversions server-side to Meta, GA4, TikTok, and webhooks with event-ID deduplication — then credits each conversion to its campaign, with the visit-to-conversion path you can audit.
No credit card required