Most marketing sites need analytics, and most of them now need a consent banner too. Usually the two are bolted together late in a project: a third-party banner script, a tag manager rule or two, and a hope that the analytics tag waits for the visitor’s answer.
That hope is hard to check. A banner can look correct, store the visitor’s choice, and still never tell Google Analytics about it. Nothing errors. The data is just wrong.
Velocity 0.4.5 (February 15) and 0.5 (March 7) build consent into the starter, wired to Google Consent Mode v2 from the first page load.
The consent manager
Set one environment variable and the banner appears on every page:
PUBLIC_CONSENT_ENABLED=trueBaseLayout renders the ConsentBanner component, which offers Accept All, Decline All and Customize. Customize opens a settings panel built on Velocity’s own Dialog, Switch and Button components, so it matches your theme. After a choice is made, a small reopener button lets visitors change their mind.
Everything else lives in src/config/consent.config.ts:
| Category | Default | Google Consent Mode signals |
|---|---|---|
necessary | Always on | security_storage |
analytics | Off | analytics_storage |
marketing | Off | ad_storage, ad_user_data, ad_personalization |
preferences | Off | functionality_storage, personalization_storage |
Categories are plain config, so you can rename them, change the labels and descriptions, or add your own. Preferences are stored in localStorage with a version number. Bump it when your categories change and every visitor is asked again.
Two modes
The config has a mode setting with two options:
consent_mode_v2(the default): GA4 or Google Tag Manager loads on every page, but with every non-required signal set todeniedfirst. Google receives cookieless pings until the visitor agrees.strict: the analytics scripts aren’t loaded at all until the analytics category is granted.
In both modes, the Analytics component sets the consent defaults before any Google script loads. If the visitor has already decided, it reads their stored choice and sets the defaults from that instead, so a returning visitor isn’t treated as denied on page load. When the visitor changes their choice, the banner sends a consent update and dispatches a consent-updated event that your own components can listen for.
The first one to use it is a new GoogleMap component on the contact page. It has three states: a setup prompt when no API key is configured, a placeholder when cookies are declined, and the live map when they’re granted.
The arguments bug
This is the one worth knowing about even if you never use Velocity.
Google’s documented gtag snippet looks like this:
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}Modern lint rules don’t like it. ESLint’s prefer-rest-params flags arguments and suggests a rest parameter, which looks like a harmless cleanup:
function gtag(...args){window.dataLayer.push(args);}That’s what happened in Velocity. A lint fix during the consent work changed every gtag definition to rest params, and 0.4.5 shipped that way. Nothing broke visibly.
But the two versions push different things. arguments is an Arguments object. args is a regular Array. GA4’s gtag.js reads the dataLayer and treats Arguments objects as gtag commands, and it silently ignores Arrays. So gtag('consent', 'update', ...) went into the dataLayer and was never applied. No console error, no failed request, just consent that GA4 never received.
Velocity 0.5 goes back to arguments everywhere, in both Analytics and ConsentBanner, with an eslint-disable comment on each line explaining why, so the next lint fix doesn’t undo it. If you’ve written your own gtag helper with rest params, check it.
Inline scripts that work with CSP
The analytics scripts have to run inline in the <head>, before anything else loads. In 0.4.5 they were rewritten so a Content Security Policy can allow them:
- Inline scripts no longer use
define:vars. They’re static, so their hash never changes, and they read runtime values from a<script type="application/json">element. Header,ConsentBannerandNpmCopyButtonbecame regular bundled scripts that readdata-*attributes.Avatarlost its inlineonerrorhandler in favor of a script listener.- Several components stopped setting
element.styledirectly and toggle CSS classes instead.
The Analytics component also documents the CSP directives GA4 and GTM need. The important one is https://*.google-analytics.com in connect-src. GA4 sends data to regional endpoints such as region1.google-analytics.com, and allowing only www.google-analytics.com drops tracking without any console errors.
One honest note: the CSP config itself came out of astro.config.mjs in 0.4.5. Astro hashes <style> blocks, and under the CSP spec a hash cancels 'unsafe-inline', which broke every inline style="" attribute. Rather than fight the framework, we kept checkOrigin and the security headers in vercel.json, and made the scripts ready for CSP once Astro 6’s API settles. If you followed CSP by default, that’s what changed.
Bugs fixed along the way
Consent code runs on every page and every client-side navigation, which is where the bugs were:
- A show/hide race. A pending hide could fire after the banner had been shown again. The hide timeout is now tracked and cleared.
- Returning visitors. The banner now restores stored consent when it starts, and in strict mode it injects the analytics scripts for visitors who had already said yes.
- Duplicate listeners. Re-running setup after navigation added another set of click handlers each time. Listeners are now bound with an
AbortControllerand torn down before rebinding. - Duplicate observers. The
MutationObserverwatching the settings dialog is disconnected before a new one is created. - Hardcoded mapping. Strict mode assumed the analytics category was called
analytics. It’s now derived from whichever category maps toanalytics_storage.
Also in 0.5
- Astro 6 beta.20, up from beta.11
ClientRouterinBaseLayout, and responsive images with theconstrainedlayout- React versions of 15 core UI components
- A floating header variant and a new
MarketingLayout - Typography, shadow and radius tokens in the theme system
nav.config.tsreplacesroutes.ts, with a migration for upgrades
Get it
Start a new project:
pnpm create velocity-astro my-projectThen set PUBLIC_CONSENT_ENABLED=true and your GA4 or GTM ID in .env. Google’s consent mode documentation explains what each signal controls. The code is on GitHub.