Docsllms.txtSign inGo to your sites

Updated

View as Markdown

Install the script

The script is one tag. It is about 5 KB gzipped, loads with defer, and does not block the page. Put it in the <head> so it loads on every page.

Ask your coding agent

Paste this into Claude Code, Codex or Cursor in your project folder. It holds no key: the agent gets one with the CLI, and you approve the sign-in in the browser. To do it yourself, follow the steps after it.

Prompt for your coding agent · holds no key
Set up twelvebit analytics in this project. Before you do anything else, tell me: "A browser tab opens in a few seconds. Approve the connection there. One approval is enough." 1. Run this first, from the project root, before you read the rest of the project or the guide: npx -y twelvebit@latest init <production domain> (Ask me for the production domain if you cannot find it at once in package.json, the README or the deploy config. Do not use localhost.) It prints a link and a code and opens the browser. I approve it once. The sign-in is then saved on this computer, and every later command uses it: do not start another sign-in. The command waits for me (up to 10 minutes), so give it a long timeout or run it in the background. If it stops before I approve, run the same command again: it continues with the same code and does not open a new tab. It saves the site key in .env by itself: do not print the key or put it in chat. It also writes analytics.yaml and a twelvebit section in AGENTS.md. 2. Follow the twelvebit section in AGENTS.md: add the script tag that init printed to the shared layout, track the 3 to 8 actions that matter, and describe them in analytics.yaml. 3. Run npx -y twelvebit@latest codegen, then npx -y twelvebit@latest check --strict. 4. Verify on production only. twelvebit does not track local development, by design: the script ignores localhost and 127.0.0.1, and the server accepts events only from the production domain and its subdomains. Do not start a local copy, add data-allow-localhost or set up local tracking, unless I ask for it. Deploy the change to the production domain (or tell me what to deploy and wait for me), then run npx -y twelvebit@latest status until it shows the first event from the production domain. 5. Optional, at the end: connect the MCP server so that you can read the data later. Claude Code: npx -y twelvebit@latest mcp claude --run (it uses the site key in .env: no new approval). Other agents: MCP server (streamable HTTP, OAuth sign-in): https://twelvebit.app/api/mcp. This opens a second, separate approval page in the browser, so ask me before you run it. Full guide, if you need more: https://twelvebit.app/setup.md. At the end, tell me whether the first event from the production domain has arrived (that is when the setup is complete), and list the events you added and where.

Do it yourself

HTML
<script defer data-website-id="tb_YOUR_SITE_ID" data-domain="example.com" src="https://twelvebit.app/js/script.js"></script>

Find your site id on the site's setup page in the dashboard. It starts with tb_.

Plain HTML

Paste the tag into the <head> of every page, or of your shared template.

Next.js (App Router)

Use next/script in the root layout. afterInteractive loads the script after hydration.

app/layout.tsx
// app/layout.tsx
import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="https://twelvebit.app/js/script.js"
          data-website-id="tb_YOUR_SITE_ID"
          data-domain="example.com"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}

Next.js (Pages Router)

pages/_app.tsx
// pages/_app.tsx
import type { AppProps } from "next/app";
import Script from "next/script";

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Script
        src="https://twelvebit.app/js/script.js"
        data-website-id="tb_YOUR_SITE_ID"
        data-domain="example.com"
        strategy="afterInteractive"
      />
      <Component {...pageProps} />
    </>
  );
}

You can also put a plain <script defer …> tag inside <Head> in pages/_document.tsx.

React with Vite (or Create React App)

Add the tag to index.html in the project root (public/index.html for Create React App):

index.html
<!-- index.html -->
<head>
  <script defer data-website-id="tb_YOUR_SITE_ID" data-domain="example.com" src="https://twelvebit.app/js/script.js"></script>
</head>

The script follows client-side navigation by itself (pushState, replaceState and the back button).

Vue and Nuxt

Vue with Vite: add the tag to index.html, as for React.

Nuxt 3 and 4:

nuxt.config.ts
// nuxt.config.ts
export default defineNuxtConfig({
  app: {
    head: {
      script: [
        {
          src: "https://twelvebit.app/js/script.js",
          defer: true,
          "data-website-id": "tb_YOUR_SITE_ID",
          "data-domain": "example.com",
        },
      ],
    },
  },
});

Svelte and SvelteKit

SvelteKit: add the tag to src/app.html, inside <head>, before %sveltekit.head%. Svelte with Vite: add it to index.html.

src/app.html
<!-- src/app.html -->
<head>
  <script defer data-website-id="tb_YOUR_SITE_ID" data-domain="example.com" src="https://twelvebit.app/js/script.js"></script>
  %sveltekit.head%
</head>

Astro

Add the tag to the <head> of your base layout. is:inline tells Astro to leave the tag as it is.

Astro
---
// src/layouts/Layout.astro
---
<html lang="en">
  <head>
    <script is:inline defer data-website-id="tb_YOUR_SITE_ID" data-domain="example.com" src="https://twelvebit.app/js/script.js"></script>
  </head>
  <body><slot /></body>
</html>

With view transitions (<ClientRouter />), the script still counts each page, because Astro changes the URL with pushState.

Remix and React Router 7

app/root.tsx
// app/root.tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router"; // "@remix-run/react" in Remix 2

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <Meta />
        <Links />
        <script defer data-website-id="tb_YOUR_SITE_ID" data-domain="example.com" src="https://twelvebit.app/js/script.js" />
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

export default function App() {
  return <Outlet />;
}

Gatsby

gatsby-ssr.jsx
// gatsby-ssr.jsx
import * as React from "react";

export const onRenderBody = ({ setHeadComponents }) => {
  setHeadComponents([
    <script
      key="twelvebit"
      defer
      data-website-id="tb_YOUR_SITE_ID"
      data-domain="example.com"
      src="https://twelvebit.app/js/script.js"
    />,
  ]);
};

WordPress

Use a header code plugin (for example WPCode: Code Snippets → Header & Footer → Header) and paste the tag. Or add it from your theme's functions.php (use a child theme, so theme updates keep it):

functions.php
// functions.php
add_action('wp_head', function () {
  echo '<script defer data-website-id="tb_YOUR_SITE_ID" data-domain="example.com" src="https://twelvebit.app/js/script.js"></script>';
});

You can also paste the tag into header.php, before </head>.

For a WooCommerce store, use the twelvebit plugin instead: it adds the tag, the cart and checkout goals, and the orders.

Shopify

Install the twelvebit app from the Shopify App Store. There is no code to paste, no id to copy and no theme file to edit.

  1. Install the app. Open twelvebit in the Shopify App Store and click Install. Shopify opens the app in your admin.
  2. Link or create the account.
    • New to twelvebit: click Create my account. twelvebit makes the account and a site for the store. The account pays through Shopify, with a 14-day free trial.
    • You use twelvebit already: click Link to twelvebit, sign in on the new tab and pick the site. Your twelvebit plan covers the store, and Shopify never bills you for twelvebit.
  3. Turn on the app embed. Click Open the theme editor. Under App embeds, turn on twelvebit analytics and click Save.

The setup page in the app has four steps: account linked, app embed on, first visit, first order. Each step turns green by itself.

The app embed loads the script on every storefront page in Shopify mode. The script follows the store's consent banner: it sets no cookie until the visitor allows analytics, and counts without cookies until then. It reads the site id and the cookieless setting from a setting that twelvebit writes into the store, so a change in twelvebit (for example cookieless mode) needs no theme edit. The app also adds a web pixel, which joins each order to its visit. See Revenue → Shopify.

Stores that connected before the app

Stores with the theme tag and a custom app keep working. To move, install the app. See Shopify: legacy setup.

Webflow

Site settings → Custom code → Head code. Paste the tag, save, and publish the site. Custom code needs a paid site plan.

Framer

Site Settings → General → Custom Code → "End of <head> tag". Paste the tag and publish.

Google Tag Manager

GTM can drop data-* attributes from a plain script tag. Use a Custom HTML tag that creates the script element, with the trigger All Pages:

HTML
<script>
  (function () {
    var s = document.createElement("script");
    s.defer = true;
    s.src = "https://twelvebit.app/js/script.js";
    s.setAttribute("data-website-id", "tb_YOUR_SITE_ID");
    s.setAttribute("data-domain", "example.com");
    document.head.appendChild(s);
  })();
</script>

After you install

  • Open the site and check the dashboard, or run the install check in Quickstart.
  • To send events before the script has loaded, add the queue stub from the Script reference.
  • If ad blockers are common with your visitors, serve the script from your own domain. See Proxy.