docs
/
AI skills

Site cookies from the address, with consent

First-party cookies the site sets on every visit, such as the UTM source, a gclid or a referral code taken from the URL, kept first-touch or last-touch, held back until the visitor accepts cookies, and read back by the page's own code.

The site can set cookies of its own on every visit, either fixed text or a part of the page address such as ?utm_source=. Use this to remember how a visitor first arrived, or to keep a campaign code until they fill in a form. You configure it as a list, and the site writes the browser code for you.

For Google Analytics and the Meta Pixel themselves, see Google Analytics, the Facebook Pixel and site verification. GA already reads UTM tags from the address, so it needs no cookie for them. The consent banner is a site notice: Site notice: banner, popup, age gate or cookie consent.

Affiliate links already work without this. Any page opened with ?ref=<code> asks the API whether the code belongs to an active affiliate program. If it does, the site saves it in its own affiliateCode cookie for 30 days, records the click, and attaches the code to the order at checkout (base-app components/storefront/AffiliateTracker.tsx 18–42, lib/affiliate-tracking.ts, lib/storefront-api.ts 467–472). Don't make a cookie named affiliateCode here. It would store codes nobody checked, and checkout would send them.

Ask first

  • Which values, from where? For each cookie, ask for its name and where its value comes from: fixed text, or one or more address parameters (utm_source, utm_campaign, gclid, fbclid, ref).
  • First touch or last touch? Ask whether the first value should be kept for good ("Keep the first value"), or each new visit with the parameter should replace it.
  • How long? Ask for the number of days. The default is 30.
  • Whole site or one page? One page can add its own cookies, or replace one of the site's by name.
  • Consent? Ask whether these cookies must wait until the visitor accepts cookies. If yes, the site needs a cookie-consent notice (step 2).
  • What reads them? Ask whether page code, a form, or something else reads them. The engine doesn't see them by itself (step 3).

Steps

In Studio, open /app/welcome › Site Config › Tracking & Social › Cookies, then Add cookie once per cookie, and Save changes (websitemint site-config-form.tsx 951–1003). The list is saved as site.data.tracking.cookies.

Field (Studio label)KeyDefaultWhat it does
Namename(none)The cookie's name. Trimmed. A row with no name is skipped.
Valuevalue''Fixed text, or text with {{query.<param>}} in it, e.g. {{query.utm_source}} or {{query.utm_campaign}}/{{query.utm_medium}}. Only {{query.…}} is filled in. Any other {{…}} is written literally. The value is URL-encoded.
Keep for (days)days30Becomes max-age. 0 or empty means 30, so a session-only cookie isn't possible. Fractions work (0.5 = 12 hours).
Pathpath/The cookie's path.
DomaindomainemptyEmpty: this host only. .example.com: every subdomain, which works only on the site's own domain.
SameSitesameSiteLaxLax, Strict or None. None always adds Secure.
Secure (https only)secureonAdds Secure unless it is switched off. A browser drops a Secure cookie on a plain-http site (localhost is the exception).
Keep the first valuekeepFirstoffWrites it only when the visitor doesn't have this cookie yet (first touch).
Only after cookie consentafterConsentoffWaits until the visitor accepts cookies (below).

The schema is in base-app lib/models/site.ts 584–614 and lib/models/page.ts 164–196. The browser code is in app/[[...slug]]/page-assets.ts 131–167.

How it behaves (every row tested on a local site):

SituationResult
{{query.utm_source}} with ?utm_source=springst_src=spring
Same cookie, a later visit with no utm_sourcethe value is empty, so the cookie is left as it was, not cleared
Keep the first value on, a later visit with ?utm_source=summerstays spring
Keep the first value off, a later visit with ?utm_source=summerbecomes summer (last touch)
{{query.utm_campaign}}-{{query.utm_medium}} with neither in the address-: the text around the placeholders makes it non-empty, so it overwrites a good value. For a combined value, use one placeholder per cookie.
days: 0kept 30 days
Only after cookie consent, before acceptingnot set
Only after cookie consent, at the moment of Accept Allset at once, if the address at that moment has the parameter; otherwise on the next visit that has it
Only after cookie consent, visitor pressed Rejectnever set (site_cookie_consent is 'minimal', not 'true')

Consent is read from localStorage.site_cookie_consent === 'true', and the cookies wait for the site-cookie-consent event on window. Both come from the cookie-consent notice's Accept All (base-app components/site-notice/useNotices.ts 102–108).

One page's own cookies. A page's tracking.cookies is added to the site's list. A page cookie with the same name replaces the site's on that page. Set it by API, not in Build Studio: Page settings › Page Config saves page tracking under a key the site never reads. Use repo_update on page with the whole tracking object, or POST /repository/update-partial/page/<sk> with { "data.tracking.cookies": [ … ] }. See Google Analytics… step 3.

For the AI: repo_find_one on site, then repo_update on site with patch: { "tracking": { …everything already under tracking…, "cookies": [ … ] } }. The merge is one level deep: a tracking without googleAnalytic, pixel and the verification codes deletes them.

{
  "tracking": {
    "googleAnalytic": "G-ABC123XYZ",
    "cookies": [
      { "name": "first_source", "value": "{{query.utm_source}}", "days": 90, "keepFirst": true, "afterConsent": true },
      { "name": "last_campaign", "value": "{{query.utm_campaign}}", "days": 30, "afterConsent": true },
      { "name": "gclid", "value": "{{query.gclid}}", "days": 90, "afterConsent": true }
    ]
  }
}

The cookies only get set when the page's script runs cleanly. They are written by the same single <script> that carries the page's, root's and the site's JavaScript, near its end (page-assets.ts 44–60, 135). One syntax error, or one error thrown at the top level of that code, and no cookie is set. Tested: a page whose code threw a TypeError set none.

The cookies are written on a full page load only. A client-side route change doesn't run the script again. They run on every ordinary page and Site Feature page. On /login, /account, /checkout and /order they run only while the site has a My Account layout page or account-layout. The values come from the URL in the browser (location.search), so they work behind any link: an ad, an email, a QR code.

Hands on: the cookie names, exactly as saved. Check: repo_find_one on the site shows data.tracking.cookies with every row, and the rest of tracking intact.

Stop: the cookies are set on the next visit to the live site. If any are for tracking or advertising and the business has visitors who must consent first, they need Only after cookie consent and a consent notice. Say so, and wait for a yes.

Only needed when a cookie has Only after cookie consent, or when GA or the Pixel wait for consent. Make one notice with the category Cookie Consent, set it Active, and leave its audience as My website. The full steps are in Site notice: banner, popup, age gate or cookie consent.

What it does for cookies:

  • Accept All sets localStorage.site_cookie_consent = 'true', fires site-cookie-consent, and the waiting cookies are set at once.
  • Reject sets 'minimal', and they are never set.
  • Either answer hides the bar for good in that browser.

Consent doesn't stop Google Analytics or the Pixel. The consent notice only releases the cookies marked Only after cookie consent. The GA and Pixel tags fire for everyone unless the site gates them in code (Google Analytics… step 5). Tested: _ga and a Pixel PageView before anyone pressed Accept.

Hands on: the notice's id. Check: in a private window, the bar shows. Before you accept, DevTools › Application › Cookies has none of the waiting cookies. After Accept All, they are there (when the address has their parameter).

Stop: an active notice shows to every visitor of the site at once. Get a yes on its wording first.

What can read the cookies:

ReaderSees the cookie?
The page's own JavaScriptYes: document.cookie, same host and path
data-bind / data-showNo. The runtime's state holds query.* and path.*, not cookies. Copy the value into state yourself (below)
The Appmint engine (forms, orders, automations)No. The site's /api proxy doesn't forward the browser's Cookie header. It passes on only its own chatDeviceId and storeLocation (base-app lib/activity-tracker.ts 309–317). Send the value in the request yourself, e.g. a hidden field in the form or a field in the fetch('/api/…') body
Google Analytics, MetaNo, and they don't need it

This page code reads the cookie, puts it in runtime state so data-bind and data-show can use it, fills a hidden form field, and sets one more cookie of its own only after consent. It is tested: the bound text showed spring, data-show revealed the block, the hidden field had spring, and the extra cookie appeared only after Accept All. Put it in the page's Page settings › Style › JavaScript (page_write), or in root's for the whole site. Everything the page runtime offers (window.appmint.state, data-*) is in The Appmint JS engine on a page.

<main id="offer-app">
  <p>You came from: <span data-bind="attribution.source"></span></p>
  <section data-show="attribution.source === 'spring'">Spring offer…</section>
  <form>…<input type="hidden" name="lead_source">…</form>
</main>
(function boot() {
  if (document.body && document.body.getAttribute('data-design-mode') === 'true') return;
  var app = document.getElementById('offer-app');
  if (!app || !(window.appmint && window.appmint.state)) return void setTimeout(boot, 30);
  if (app.dataset.bound) return;
  app.dataset.bound = '1';
  function readCookie(name) {
    var hit = document.cookie.split('; ').find(function (p) { return p.indexOf(name + '=') === 0; });
    return hit ? decodeURIComponent(hit.slice(name.length + 1)) : '';
  }
  var source = readCookie('first_source');
  window.appmint.state.set('attribution', { source: source });
  var hidden = app.querySelector('input[name="lead_source"]');
  if (hidden) hidden.value = source;
  // A cookie of the page's own, only once the visitor has accepted cookies.
  function setOwn() {
    document.cookie = 'landing=' + encodeURIComponent(location.pathname) + '; max-age=' + 90 * 86400 + '; path=/; samesite=Lax; secure';
  }
  var ok = false;
  try { ok = localStorage.getItem('site_cookie_consent') === 'true'; } catch (e) {}
  if (ok) setOwn(); else window.addEventListener('site-cookie-consent', setOwn);
})();
  • Read inside a boot loop like this one, not at the top of the script. The site's cookie code runs after the page's JavaScript in the same script. Code that reads document.cookie straight away, on the visitor's first visit, runs before the cookie exists. The setTimeout wait above lets it be set first.
  • data-show can test the address directly too, data-show="query.utm_source === 'spring'", but only on the page that has the parameter. The cookie is what carries it to the next page.
  • A hidden field reaches a CRM form only if the form has a field of that name. See Registration that creates the customer step 4 for posting a form from page code.

A "Cookie settings" link that lets a visitor change their mind (tested: the bar came back after reload). Write it as an inline handler, because a <script> in page HTML doesn't run:

<a href="#" onclick="localStorage.removeItem('site_cookie_consent');['first_source','last_campaign','gclid','landing'].forEach(function(n){document.cookie=n+'=; max-age=0; path=/'});location.reload();return false;">Cookie settings</a>

List every cookie the site sets in that array. Removing the consent key doesn't delete cookies already set, and it doesn't undo GA's own _ga cookies.

Hands on: nothing new. Check: open https://<site host>/<page>?utm_source=spring in a private window, accept, and reload. DevTools › Application › Cookies shows the cookie, and the page shows the value.

When it's done

Tell the person:

  • each cookie's name, where its value comes from, first or last touch, how many days, and whether it waits for consent;
  • which page code reads it, and how the value reaches their CRM, if it does.

Say plainly that GA and the Pixel are not held back by the consent notice unless step 5 of the analytics skill was done.

What it can't do yet

  • Only {{query.<param>}}. There is no referrer, landing path, date or random ID in a value. Set those from page code.
  • No session cookies. days of 0 or empty becomes 30.
  • No "clear this cookie" option. A value that comes out empty leaves the old cookie in place. Clearing one takes page code (max-age=0).
  • The engine never sees these cookies. Pass the value in the form or the request.
  • The server doesn't record consent. It lives in each browser's localStorage. It can't be proven later, and it doesn't follow the visitor to another device.
  • Page Config in Build Studio can't set page cookies. Use the API.

Don'ts

  • Don't name a cookie affiliateCode, chatDeviceId, storeLocation or token. The platform uses those names.
  • Don't put personal data (emails, names) in a cookie value.
  • Don't combine several placeholders with text in one value when any of them may be missing. It overwrites good data with the leftover text.
  • Don't send a tracking patch without everything already in it.
  • Don't mark advertising cookies as consent-free for an EU or UK audience to make the numbers look better. Say what the setting does and let the owner decide.