6 min read

Framework & CMS Guides

Recommended installation patterns for Next.js, React, Astro, Vue/Nuxt, WordPress, Shopify, Webflow, and Content Security Policy (CSP) headers.

Imtiaz Rayhan
Imtiaz RayhanCo-Founder & Lead Engineer, DotCamp & TrimStats
Verified against live TrimStats APIs
Quick reference

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:

Site settings / Tracking snippet
Use the exact <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';
3
4export default function RootLayout({ children }: { children: ReactNode }) {
5 return (
6 <html lang="en">
7 <body>
8 {children}
9 <Script
10 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}
Astro requires is:inline

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.

PlatformWhere to paste the scriptNotes
WordPressTheme 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.
ShopifyOnline Store -> Themes -> Edit code -> `layout/theme.liquid` inside <head>Pair with our Custom Webhooks guide to attribute order revenue.
WebflowSite Settings -> Custom Code -> Head CodePublish your site after saving. Custom code does not run inside the Webflow Designer canvas.
FramerSite Settings -> General -> Custom Code -> Start of `<head>` tagWorks natively with Framer SPA transitions.
GhostSettings -> Code Injection -> Site HeaderAutomatically 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.

src/types/trimstats.d.tsts
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}
10
11export 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}
21
22declare 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.

HTTP Response Headerhttp
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?
Astro compiles and bundles regular <script> tags as ES modules and can strip data-* attributes. Adding is:inline preserves the deferred tag and data-site attribute verbatim in your rendered HTML head.
What Content Security Policy (CSP) directives does TrimStats require?
Allow https://collect.trimstats.com in script-src and connect-src (or route beacons through your own first-party reverse proxy). TrimStats never calls eval() and needs no unsafe-eval directive.

Related guides