On every plan, including Free
Next.js and React
Surveys in Next.js and React.
No npm package and no component to wrap your app in. One loader goes in the root layout with next/script, or in index.html, and the SDK follows your router by itself. A coding agent can do the whole install.
Read the docsHow do I add a survey to a Next.js or React app?
Add UserLoop's loader once, in the document every route shares. In the Next.js App Router that is app/layout.tsx, rendered with next/script and the afterInteractive strategy. In React with Vite it is index.html, inside the head. There is no npm package or component to install. The SDK watches the History API, so page rules and triggers follow route changes without any code in your routes.
- Next.js
- app/layout.tsx, with next/script
- React with Vite
- index.html, inside the head
- Package
- None. No npm install, no component
- Route changes
- Detected from the History API
Install
Three steps in your codebase
01
Copy the loader
In UserLoop, open Settings, then Website, and choose HTML under Your site is built with. It gives the loader, which queues calls made before the SDK has loaded.
02
Add it to the root layout
In Next.js, render it from app/layout.tsx with next/script. In React with Vite, paste it into index.html. One copy, never in a component that renders per route.
03
Check it, then deploy
Run your dev server and type userloop.version in the console. Surveys show on localhost without listing it, but UserLoop's own install check needs the deployed site.
The code
Where the loader goes
The same loader in each case, and only the file changes. The last two tabs are how your components call it and how TypeScript learns about it.
// app/layout.tsx
import Script from "next/script";
const userloopLoader = `!function(w,d,o){if(w.userloop)return;var q=[],u=w.userloop={q:q};"init identify setAttributes track page show close reset setLanguage on off".split(" ").forEach(function(m){u[m]=function(){q.push([m,[].slice.call(arguments)])}});var s=d.createElement("script"),c=d.currentScript;s.async=1;s.src="https://cdn.userloop.io/sdk-3/userloop.js";if(c&&c.nonce)s.nonce=c.nonce;d.head.appendChild(s);u.init(o)}(window,document,{"siteKey":"YOUR_SITE_KEY"});`;
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script id="userloop" strategy="afterInteractive" dangerouslySetInnerHTML={{ __html: userloopLoader }} />
</body>
</html>
);
}<!doctype html>
<html lang="en">
<head>
<!-- index.html at the project root. Never render the snippet from a component. -->
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My app</title>
<script>
!function(w,d,o){if(w.userloop)return;var q=[],u=w.userloop={q:q};"init identify setAttributes track page show close reset setLanguage on off".split(" ").forEach(function(m){u[m]=function(){q.push([m,[].slice.call(arguments)])}});var s=d.createElement("script"),c=d.currentScript;s.async=1;s.src="https://cdn.userloop.io/sdk-3/userloop.js";if(c&&c.nonce)s.nonce=c.nonce;d.head.appendChild(s);u.init(o)}(window,document,{"siteKey":"YOUR_SITE_KEY"});
</script>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>import { useEffect } from "react";
// Opens the survey whose trigger is On an event, named upgrade_clicked
export function UpgradeButton() {
return (
<button onClick={() => window.userloop?.track("upgrade_clicked")}>
Upgrade
</button>
);
}
// Tells UserLoop who is signed in. Render it once, near the root.
export function UserLoopIdentity({ user }: { user: { id: string; email: string; plan: string } | null }) {
useEffect(() => {
if (user) window.userloop?.identify(user.id, { email: user.email, plan: user.plan });
}, [user]);
return null;
}
// In your sign-out handler:
// window.userloop?.reset();// src/userloop.d.ts, in a folder your tsconfig.json includes.
// A short version. The docs have the full declaration, with events and options.
type UserLoopAttributes = Record<string, string | number | boolean | null | string[]>;
interface UserLoop {
identify(userId: string, traits?: UserLoopAttributes): void;
setAttributes(attributes: UserLoopAttributes): void;
track(event: string, properties?: UserLoopAttributes): void;
page(): void;
show(surveyId: string, options?: { force?: boolean; preview?: boolean }): void;
close(surveyId?: string): void;
reset(): void;
}
declare global {
interface Window {
// Optional: it does not exist until the snippet has run
userloop?: UserLoop;
}
}
export {};In your app
What a framework install needs to get right
Components, routes and the agent that writes them.
Components
Call the SDK where something happens
The dashboard decides which survey and which visitors. Your components only say what happened and who is signed in.
track. An event name you set as a survey's trigger. The survey opens for visitors its rules allow.
identify. Your user ID and email go with each answer. Other traits become attributes you can target on, such as plan is pro.
reset. Call it on sign-out, so the next person on that browser is a new visitor.
Routing
Route changes are detected for you
The SDK fetches the site's surveys once per page load and then watches the address. When the path or query changes, it counts a new page view.
Rules are checked again. Each survey's pages and visitor rules are checked for the new address, without fetching again.
Timers start over. After a wait and On scroll begin again from zero. A survey that is open stays open.
Hash routers. The part of the address after # is never a new page view. Use On an event and userloop.track from that route instead.
// Most apps need nothing here. The SDK watches history.pushState,
// history.replaceState, popstate and hashchange by itself.
// Only if your router changes the address in a way the SDK cannot see:
// end the snippet with {"siteKey":"YOUR_SITE_KEY","autoPageViews":false}
// and call page() after each route change, not on the first load.
window.userloop.page();// In the browser console, after moving to another route without reloading
userloop.version
// "3.0.0+1a2b3c4" means the SDK has loaded. undefined means it has not.
// Open any live survey at once, skipping its rules, to see it in your app
userloop.show("SURVEY_ID", { force: true });
// Or try it without recording anything
userloop.show("SURVEY_ID", { preview: true });Agents
Or hand the install to your coding agent
Connect UserLoop's MCP server to Claude Code or Cursor and describe the survey. The agent builds it in UserLoop and edits your layout in the same session.
Builds the survey. create_survey, then set_website_display and set_website_targeting for how it appears and who sees it.
Edits your code. It adds the loader to app/layout.tsx, the same way this page shows.
Checks its own work. check_site_install reports whether UserLoop has seen page loads from your site.
claude mcp add --scope user --transport http userloop https://mcp.userloop.ioset up UserLoop: https://mcp.userloop.io/llms.txtAdd a UserLoop survey to this Next.js app.
Ask visitors on /pricing why they have not upgraded, as a slide-in
after 20 seconds, once per visitor. Put the UserLoop snippet in
app/layout.tsx with next/script, then check the install.Platform facts
UserLoop in a framework, in one table
- Next.js App Router
- app/layout.tsx, with next/script and strategy afterInteractive
- React with Vite
- index.html at the project root, inside the head
- Vue, Svelte, Angular and others
- One copy in the HTML page the app is served from, before the app's own scripts
- Package
- None. No npm package, no React or Vue component, nothing added to your bundle
- Single-page navigation
- history.pushState, history.replaceState, popstate and hashchange are watched. Call userloop.page() only if your router is not seen
- Hash routers
- Every route is the same page. Use On an event and userloop.track
- Targeting
- Routes and up to 50 visitor attributes are matched by rules in the dashboard, so changing who sees a survey is not a deploy
- TypeScript
- Declare window.userloop once in a .d.ts file. The docs have the full declaration
- Local development
- localhost always shows surveys and never counts as installed
- Content Security Policy
- Put a nonce on the snippet's script tag and it is passed on to the scripts it loads
- Page weight
- 13 KB gzipped, loaded asynchronously. A further 24 KB only when a survey shows
- Consent
- Sets no cookies. The storage option can be local, session or memory
Frequently asked questions
Is there a UserLoop npm package or React component?
No. The SDK is one script loaded from UserLoop's CDN, added once in the root layout or index.html. There is no npm package and no React or Vue component, so nothing is added to your bundle.
Where does the script go in the Next.js App Router?
In app/layout.tsx, the root layout, rendered with next/script and the afterInteractive strategy. The root layout stays mounted across navigations, so the snippet runs once per page load.
Do I need to call anything when the route changes?
No. The SDK watches the History API and counts a new page view when the path or query changes. Call userloop.page() yourself only if your router changes the address in a way the SDK cannot see.
Does it work with React Router, Vue, Svelte or Angular?
Yes. Paste the snippet into the HTML page the app is served from, inside the head and before the app's own scripts. Routers that use the History API are followed without any extra code.
How do I type window.userloop in TypeScript?
Declare it once in a .d.ts file your tsconfig.json includes. There are no types to install. The property is optional, because window.userloop does not exist until the snippet has run.
Can I test it in local development?
Yes. localhost and 127.0.0.1 always pass the address check, so published surveys show there. Pages on localhost never count as installed, so UserLoop's Check installation needs the deployed site.
Can I show a survey only to signed-in users on one plan?
Yes. Pass the plan with identify or setAttributes, then add a visitor attribute rule such as plan is pro on the survey's Targeting stage. The rule is checked again whenever attributes change.
Can Cursor or Claude Code install it for me?
Yes. With UserLoop's MCP server connected, a coding agent can create the survey, set its display and targeting, add the snippet to your layout and call check_site_install. The MCP server is on Unlimited and Plus.
Is it free?
Website surveys and the SDK are on every plan, including Free. Free allows 1 live survey with 3 questions and 50 responses a month. Unlimited is $19 a month with unlimited responses.
Keep reading
One loader in your root layout, then no more code
Start on the Free plan: 1 live survey, 3 questions and 50 responses a month.