Framework & CMS Guides
Recommended installation patterns for Next.js, React, Astro, Vue/Nuxt, WordPress, Shopify, Webflow, and Content Security Policy (CSP) headers.
How does TrimStats integrate with Next.js, Astro, WordPress, and Shopify?
TrimStats works across React, Next.js App Router, Astro View Transitions, Vue, WordPress, Shopify, and Webflow using one deferred 1.96 KB script tag. Because t.js wraps history.pushState and listens to popstate natively, single-page frameworks need zero npm SDKs or route-change hooks.
Why SPAs and strict CSP headers work differently
Traditional websites load a fresh HTML document on every click, so basic analytics scripts only fire once on initial load. Modern frameworks like Next.js, React Router, Vue, and Astro View Transitions swap page content in JavaScript using history.pushState without reloading the document, causing naive trackers to miss every click after the landing page.
TrimStats intercepts history.pushState and browser popstate transitions natively inside t.js, recording each pathname change and flushing active engagement time from the previous route automatically. Copy your pre-configured tag directly from Site settings before pasting it into your framework layout:
<script defer> tag and pk_live_... site key shown in Site settings inside any SPA framework or CMS header.JavaScript frameworks & SPAs
TrimStats automatically intercepts history.pushState and listens for browser popstate events, so client-side navigation in Next.js, React Router, Vue Router, SvelteKit, and Astro View Transitions is tracked with zero extra router hooks.
1import Script from 'next/script';2import type { ReactNode } from 'react';34export default function RootLayout({ children }: { children: ReactNode }) {5 return (6 <html lang="en">7 <body>8 {children}9 <Script10 src="https://collect.trimstats.com/t.js"11 data-site={process.env.NEXT_PUBLIC_TRIMSTATS_SITE_KEY}12 strategy="afterInteractive"13 />14 </body>15 </html>16 );17}Always include is:inline on the <script> tag in Astro layouts so Astro preserves the data-site attribute instead of bundling or rewriting the external script.
CMS & website builders
If your site runs on a hosted builder or CMS, add the script tag inside the global custom <head> code injection area so it loads once across every template.
| Platform | Where to paste the script | Notes |
|---|---|---|
| WordPress | Theme header.php before </head> or a header-code plugin (wp_head hook) | Ensure caching/minification plugins do not strip data-site attributes from <script> tags. |
| Shopify | Online Store -> Themes -> Edit code -> `layout/theme.liquid` inside <head> | Pair with our Custom Webhooks guide to attribute order revenue. |
| Webflow | Site Settings -> Custom Code -> Head Code | Publish your site after saving. Custom code does not run inside the Webflow Designer canvas. |
| Framer | Site Settings -> General -> Custom Code -> Start of `<head>` tag | Works natively with Framer SPA transitions. |
| Ghost | Settings -> Code Injection -> Site Header | Automatically tracks every post, tag archive, and membership page. |
TypeScript global declarations
Add this ambient declaration file anywhere in your TypeScript project (src/types/trimstats.d.ts) to get full autocomplete and type safety for window.trimstats.
1export interface TrimStatsAttribution {2 vid: string;3 uid: string;4 ref: string;5 lp: string;6 utm_source: string;7 utm_medium: string;8 utm_campaign: string;9}1011export interface TrimStatsFn {12 (13 cmd: 'event',14 name: string,15 props?: Record<string, string | number | boolean | null>,16 ): void;17 (cmd: 'identify', userId: string): void;18 q?: Array<unknown[]>;19 attribution?: () => TrimStatsAttribution;20}2122declare global {23 interface Window {24 trimstats?: TrimStatsFn;25 }26}Content Security Policy (CSP)
A Content Security Policy (CSP) is an HTTP response header that tells browsers which origins are allowed to execute JavaScript (script-src) or receive background API requests (connect-src). If your app sends strict CSP headers, allow the TrimStats tracker origin in script-src and the collector origin in connect-src. TrimStats never uses eval() or inline script execution.
Content-Security-Policy: script-src 'self' https://collect.trimstats.com; connect-src 'self' https://collect.trimstats.com;Common technical questions
Why does Astro require is:inline on the TrimStats <script> tag?
What Content Security Policy (CSP) directives does TrimStats require?
Related guides
Add TrimStats to any website in under 60 seconds with a single deferred script under 2 KB gzipped.
What TrimStats captures automatically out of the box, plus how referrers, Direct traffic ("dark social"), and UTM campaign tags work.
Confirm events are reaching ClickHouse and diagnose common setup issues like 403 domain blocks, ad blockers, or missing data attributes.