Skip to documentation content
CookieBeam

Install with a script tag

Add the CookieBeam loader directly to your site's HTML, in the right place, and verify the published banner.

Last updated:

Use the direct script method when you control the site's HTML or its global header template. It is the strongest setup: the loader runs before anything else on the page, so it can hold back every tracker that loads after it. It uses the same banner, published blocking rules, consent storage and reporting as the GTM installation, and it is available on every plan.

Copy the production loader

Publish the banner first. The install page only shows the code once a version has been published.

  1. In CookieBeam, open your banner and go to Deployment → Install.
  2. On the Direct installation tab, select Copy.

The script follows this format:

<script
 id="CookieBeam"
 src="https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js"
 type="text/javascript"
 data-cfasync="false" data-no-optimize="1" data-no-defer="1"
 data-no-minify="1" data-noptimize="1" nowprocket="1" data-pagespeed-no-defer="1"
></script>

Replace PUBLIC_BANNER_ID only when you are writing the tag by hand; the copied code already has your ID. The id, type and optimizer-exclusion attributes (data-cfasync, nowprocket and the rest) are included in the tag the dashboard gives you.

Add it to every page

Place the script once near the start of <head> in the shared site template, before Google tags and other managed scripts. In practice:

  1. Open the template that every page shares, for example a layout file, a header include, or your CMS's "head code" setting.
  2. Paste the loader as the first <script> inside <head>, directly after the <meta charset> and viewport tags.
  3. Make sure it sits above the Google Tag Manager snippet, gtag.js, and any analytics or advertising pixel.
  4. Do not add async or defer. The loader must run as the page is read, before the scripts below it.
  5. Publish or deploy the site change through your normal release process.
<head>
 <meta charset="utf-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1" />
 <!-- Optional: warm the connection for impression and load-order reports -->
 <link rel="preconnect" href="https://cookiebeam.com">
 <!-- 1. CookieBeam first -->
 <script src="https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js" data-cfasync="false" data-no-optimize="1" data-no-defer="1" data-no-minify="1" data-noptimize="1" nowprocket="1" data-pagespeed-no-defer="1"></script>
 <!-- 2. Then Google Tag Manager, gtag.js and other tags -->
</head>

The <link rel="preconnect"> is optional. After the preconnect change ships and the banner is republished, the loader adds the same hint at runtime before the impression and load-order beacons fire. An HTML hint lets the browser's preload scanner start the connection a little earlier -- by the time the loader takes to download and execute. The gain is small. You don't need a preconnect to cdn.cookiebeam.com because the browser already has a connection to it from loading the loader.

Do not paste the tag into a single content page unless that page is intentionally the only place where CookieBeam should run.

Building with Next.js, React, Vue, Nuxt, SvelteKit, Angular, Astro, Remix or Gatsby? See Install in a web framework for where the tag goes in each.

If your site uses a script optimiser, exclude cdn.cookiebeam.com from features that delay, defer, combine or reorder JavaScript. They move the loader behind the scripts it needs to hold back.

Direct installation supports Google Consent Mode V2. Enable Google Consent Mode in your published banner settings when using Google tags, and place the loader before any Google config or event commands. Follow Google's consent setup requirements. See Google Consent Mode for the settings.

Keep hardcoded non-essential scripts inert until consent with type="text/plain", data-category, and data-src for external files:

<script type="text/plain" data-category="analytics" data-src="https://example.com/analytics.js"></script>

See Script blocking for more examples. A strictly necessary script that must always run can carry data-cookiebeam-ignore, which exempts it from consent enforcement. A loader cannot undo a request that has already happened, and Consent Mode alone does not block every third-party tag or cookieless request.

Use one CookieBeam installation per page. If you choose the GTM gallery template, follow that method's Consent Initialization steps instead of adding a second loader.

The script tag sets the Consent Mode default on every page. Do not add the CookieBeam GTM template or your own gtag('consent', 'default', ...) call alongside it. A second default can send Google tags the wrong consent state before the banner loads.

Visitors need a way to change their choice later. Add a link to your footer that opens the preferences:

<a href="#" data-cb="show-preferencesModal">Cookie settings</a>

You can also call CookieBeam.showPreferences() from your own code. See the Browser API.

Staging and other environments

The default loader always serves what is published to production. If you use environments, a staging site can load the version published to that environment instead:

https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/env/staging/loader.js

Never put an environment loader on your live site.

Verify the installation

Open a private browsing window and load the site with browser storage cleared. Check that:

  1. The consent banner appears without a console error.
  2. Accepting consent enables the expected optional services.
  3. Rejecting consent keeps non-essential services blocked.
  4. Reopening preferences lets you change the stored choice.

If the banner does not appear, confirm that the published Banner ID matches the loader URL, that the site's hostname is listed under Banner domains, and that your content-security policy permits CookieBeam (see Content security policy). For the full checklist, see Verify your installation and Common mistakes.

Next.js, React and static HTML

Place the loader as the first executable code inside the server-rendered <head>. Do not add async or defer, and keep any build-tool exclusions so the bundler does not transform it.

Static HTML and React SPAs: Put the script tag in the HTML shell before the application bundle. A React effect that injects the script runs too late; the loader must be in the initial HTML response.

Next.js App Router: Place the loader in the root layout's head. Pages Router: Place it in the custom document's head. Use a native synchronous <script> tag, not the Next.js Script component. The beforeInteractive strategy gives framework-level ordering but does not prove precedence over all platform code; afterInteractive runs after page execution begins, which is too late.

After deploying, check the server response source (not just the Elements panel) and the Network panel on a fresh visit and during client-side navigation. Disable optional hosting analytics until they are gated behind consent.

Sources: Next.js Script component. Verify: Installation checks.

AltStyle によって変換されたページ (->オリジナル) /