Performance testing
How to test your site's performance in each consent state, why results differ between unanswered, accepted and rejected, and how to automate the measurement.
Última actualización:
A consent banner changes what a page loads depending on the visitor's choice. Testing performance without accounting for that gives you one slice of the picture and misses the rest. This page covers how to test each consent state and what to watch for in each.
Three states, three different pages
The same URL behaves differently depending on whether the visitor has answered the banner:
- Unanswered. The banner is visible, optional scripts and connections are held back, and the blocking interceptors are active. This is the heaviest state for CookieBeam itself, but the lightest for third-party traffic.
- Accepted. The banner is hidden, blocked resources have been released, and every optional tag is running. This is the heaviest state overall: the page carries CookieBeam's own files plus everything it was holding back.
- Rejected. The banner is hidden, optional resources stay blocked, and only strictly necessary scripts are running. This is usually the lightest state overall.
A Lighthouse run or a WebPageTest test without a consent cookie always measures the unanswered state. That's what PageSpeed Insights reports, and it's what field data from new visitors reflects. But returning visitors who accepted or rejected see a different page, and their Core Web Vitals are different too.
Why the tag is synchronous
The CookieBeam loader runs synchronously in <head> on purpose. It has to execute before third-party scripts so it can intercept and hold them back until consent is resolved. Adding async or defer to the loader, or letting a script optimiser delay it, breaks blocking: tags that load before the loader runs are not caught. See Load order for the full explanation.
When you see the loader in a Lighthouse "Render-blocking resources" list, that's expected. Removing it from that list would remove the consent enforcement.
Lighthouse CLI
CookieBeam reads the consent cookie from document.cookie inside the page. Lighthouse's --extra-headers flag sends an HTTP header to the server, but the page's document.cookie doesn't reflect it, so the banner still sees the unanswered state. Use a browser with a real cookie instead.
Start Chrome with a dedicated profile and a remote debugging port:
chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cb-perf-accept
In that window, open the site, wait for the banner, and click the accept button. Then run Lighthouse against the same Chrome instance:
lighthouse https://example.com \
--port=9222 \
--disable-storage-reset \
--output=html \
--output-path=./report-accepted.html
Lighthouse opens a new tab in that browser. The page's document.cookie holds the consent cookie, so the banner reads it and skips the dialog. --disable-storage-reset is needed because some banners also store consent in localStorage (cb_consent_<bannerId>), and Lighthouse's storage reset would clear that.
For the rejected state, use a fresh profile directory (--user-data-dir=/tmp/cb-perf-reject), open the site, and click the reject button. For the unanswered baseline, run plain lighthouse https://example.com with no port or profile.
You can also use the DevTools Lighthouse panel. Open the site in a tab, click the accept or reject button, then run Lighthouse from the panel with Clear storage unticked.
Compare the three reports side by side. Look at:
- LCP. Accepted pages load more third-party resources. If LCP is much worse after accepting, the released tags are competing with your main content.
- TBT / INP. The blocking interceptors stay installed for the page's lifetime, even after consent. They still check every DOM insertion and network call. After accepting, the released tags add their own main-thread work on top of that.
- CLS. The banner is a fixed-position overlay, so it shouldn't shift your content. If you see CLS, look at the tags that are released after consent, not the banner itself.
- Network requests. Count how many third-party requests appear in each state. The difference between accepted and rejected is your consent-gated traffic.
WebPageTest
WebPageTest can set cookies and click banner buttons through its scripting language.
Set a cookie before the test
Use the setCookie command to inject the consent cookie before the page loads. The command takes the page URL and the cookie as name=value:
setCookie https://example.com cb_cookie=PASTE_ACCEPT_VALUE_HERE
navigate https://example.com
Replace cb_cookie with the banner's cookie name if it was changed from the default. The URL must match the site under test. Run a second test with the reject value for comparison.
setCookie writes only a cookie. Banners that also store consent in localStorage won't pick it up from a cookie alone. If the banner re-shows despite the cookie, use the click flow below instead.
Click the banner during the test
If you want the test to measure what happens when a visitor clicks a button, use execAndWait with JavaScript that clicks the button. The accept button carries data-cb="accept-all" and the reject button carries data-cb="accept-necessary". Both are inside the #cb-main container:
navigate https://example.com
execAndWait document.querySelector('#cb-main [data-cb="accept-all"]').click()
Or for the reject button:
navigate https://example.com
execAndWait document.querySelector('#cb-main [data-cb="accept-necessary"]').click()
This approach measures the transition: the unanswered page loads, the click fires, blocked resources are released (or not), and the test captures the waterfall through the whole sequence. It works regardless of whether the banner uses cookies, localStorage, or both.
Get a cookie value to copy
If you're using the setCookie method, you need the cookie value from a real session:
- Open the site in a browser. Clear cookies for the domain or use a private window.
- Wait for the banner and click the accept button or the reject button.
- Open DevTools, go to Application > Cookies, and find the cookie for the site's domain. The default name is
cb_cookie. If the banner uses a custom cookie name, look for that name instead. - Copy the full value. The core stores a JSON object, URL-encoded when written to the cookie. Don't decode or edit it.
- For the WebPageTest
setCookiecommand, use the same domain as the URL you're testing. The cookie stops working when it expires or when the banner owner uses "Ask visitors again", which bumps the consent revision and invalidates stored cookies.
PageSpeed Insights
PageSpeed Insights runs Lighthouse without a consent cookie, so it always measures the unanswered state. You can't change that. The score it shows reflects a first-time visitor who hasn't answered the banner yet.
That's a valid and important measurement, but it's only one of the three states. Use the Lighthouse CLI or WebPageTest methods above for the other two.
What the preload and preconnect changes mean for results
On banners published after the preload change, latest.js adds <link rel="preload"> hints for the banner runtime and stylesheet while the page is still parsing, so the browser can start fetching them before DOMContentLoaded. The preload is skipped once parsing has finished and for visitors in regions where the banner is hidden. On banners published before the change, the runtime and stylesheet are requested only at DOMContentLoaded. Republishing the banner picks up the preload behaviour.
After the preconnect change ships and a banner is republished or rebuilt, the loader adds a <link rel="preconnect"> to https://cookiebeam.com, the origin that receives impressions and drift and load-order reports. Until then, the loader preconnects to cdn.cookiebeam.com instead. It doesn't need to preconnect to cdn.cookiebeam.com once the change is live, because the loader was already loaded from that origin.
If you're comparing results over time, note when you republished. A test before and after republishing may show a different waterfall for the runtime and stylesheet, even though nothing else changed.
Preconnect hint in your HTML
You can optionally add a preconnect hint to your page's <head>, before the loader tag:
<link rel="preconnect" href="https://cookiebeam.com">
Don't add crossorigin to this hint. The beacons use navigator.sendBeacon, which sends credentialed requests; a crossorigin preconnect opens a separate, uncredentialed connection that the beacons won't reuse.
After the preconnect change ships and the banner is republished, the loader adds this preconnect at runtime, before the impression and load-order beacons fire from latest.js. An HTML hint lets the browser's preload scanner find it a little earlier -- by the time the loader takes to download and execute. The gain is small, so this is optional.
Origins and CSP
If your site uses a Content Security Policy, both CookieBeam origins need to be allowed. See Content security policy for the directives and sources.
| Origin | Used for |
|---|---|
cdn.cookiebeam.com | The loader, banner script, runtime, stylesheet, framework bundle, translations, geo lookup, known findings |
cookiebeam.com | Consent log, impressions, drift reports, load-order reports |
You don't need to preconnect to cdn.cookiebeam.com in your HTML. The browser already has a connection open to it from loading the loader script.
Further reading
- Performance — file sizes, caching, load order and what keeps running after load.
- Load order — why the tag must run first and how load-order problems are detected.
- Content security policy — the CSP sources CookieBeam needs.