Skip to content
SaaSFunnelsHelp

Developers

Install code reference

Add the SaaSFunnels script to your site or app, identify signed-in users, track events and control where Funnels open.

Where to find itSettingsInstall code

On this page

The install code is one script tag. It loads the current SaaSFunnels runtime, shows your published Funnels and sends browser events. Your own snippet is on Settings → Install code, with your client key already in it. Select Copy in its corner.

The script tag

Paste it once on every page, inside <head>, before your own scripts:

HTML
<script>
  window.saasfunnels = window.saasfunnels || [];
  window.saasfunnels.push(["init", {
    clientKey: "<web_sdk_client_key>",
    sharedInstallation: true,
    consent: "granted"
  }]);
  (function (d) {
    var s = d.createElement("script");
    s.async = true;
    s.src = "https://app.saasfunnels.com/sdk/saasfunnels.js";
    d.head.appendChild(s);
  })(document);
</script>

Keep it as one script. In React or Next.js, render it as one inline script in your root layout, not as a separate <script src> tag: React moves async script tags to the top of <head>, so the loader could run before init and nothing would start.

The script URL never changes. It always loads the current release, so there is nothing to upgrade.

The client key only works on the sites listed for that install code. Add every production, staging and local address (for example http://localhost:3000) under Settings → Install code.

Init options

OptionWhat it does
clientKeyYour install code's client key. Required.
sharedInstallationKeep true. Lets published Funnels open on page load, clicks, scroll, time on page and track events without extra code.
consent"granted", "pending" or "denied". See Consent.
runtimeTokenProviderA function that returns a token for the signed-in user. See Identify signed-in users.
onFallbackCalled when a Funnel can't show, so you can keep your own UI.
styleNonceYour CSP nonce. See Content Security Policy.
debugtrue logs install problems to the browser console.

React and Next.js

React apps use the same script tag. There's no npm package to install.

  • Vite or other single-page apps: paste the snippet into index.html.
  • Next.js: add the snippet to <head> in your root layout as one inline script (dangerouslySetInnerHTML). Don't split the loader into its own <script src> tag.

Then call the runtime from any component through window.saasfunnels.

Identify signed-in users

Funnels that use plans, payments or account details need to know who is signed in. Set this up in Settings → Install code → Signed-in visitors → App users → Set up:

  1. Click Create server key and save the value on your server as SAASFUNNELS_FUNNELS_SERVER_KEY.
  2. Add this line to the snippet's init options:

    JavaScript
    runtimeTokenProvider: () =>
    fetch("/api/saasfunnels", { method: "POST" })
    .then((res) => (res.ok ? res.json() : {}))
    .then((data) => data.runtimeToken),
  3. Add the sign-in route to your server. The app gives you a Next.js or Express version; this is the Next.js one:

    TypeScript
    // app/api/saasfunnels/route.ts
    import { getUser } from "@/lib/auth"; // your sign-in
    
    export async function POST() {
    const user = await getUser();
    if (!user) return new Response(null, { status: 401 });
    const response = await fetch("https://app.saasfunnels.com/api/funnels/v1/runtime/sessions", {
    method: "POST",
    headers: {
    "content-type": "application/json",
    "x-saasfunnels-server-key": process.env.SAASFUNNELS_FUNNELS_SERVER_KEY,
    },
    body: JSON.stringify({
    apiVersion: 1,
    requestId: crypto.randomUUID(),
    kind: "verified",
    subject: { key: user.id },
    accountKey: user.accountId, // the account_id you send with events
    actor: { role: user.role, userId: user.id, purchaseAuthority: "execute" },
    user: { email: user.email, name: user.name },
    }),
    });
    if (!response.ok) return new Response(null, { status: 502 });
    const { data } = await response.json();
    return Response.json({ runtimeToken: data.runtimeToken });
    }

Tokens last at most 15 minutes; the runtime asks for a new one when it needs it. Don't store tokens or put them in URLs or logs.

To tag browser events with the user and account, also call:

JavaScript
window.saasfunnels.push(["group", YOUR_ACCOUNT_ID, { plan: YOUR_PLAN_NAME }]);
window.saasfunnels.push(["identify", YOUR_USER_ID]);

Track events

JavaScript
window.saasfunnels.push(["track", "checkout_started", { plan: "growth", value: 1 }]);

A track call records the event and opens any published Funnel that starts on it. Use lower_snake_case names that don't change. For events your server knows about, such as a paid invoice, use the Server events API instead.

Placements and buttons

Overlay Funnels (modals, drawers, banners) need no code. For two other cases, Studio gives you the exact code to copy:

  • A button that opens a Funnel. Add the attribute to your own button. If no Funnel opens, the button does what it normally does.

    HTML
    <button data-saasfunnels="upgrade_clicked">Upgrade</button>
  • An inline Funnel inside your page. Add an empty element where it should appear. Reserve its height in your CSS.

    HTML
    <div data-saasfunnels-placement="pricing_inline"></div>

Funnels show only after consent is "granted". If your site has a cookie banner, start with consent: "pending" and call this when the visitor accepts:

JavaScript
window.saasfunnels.funnels.setConsent("granted");

"denied" closes any open Funnel and clears what the runtime stored in the browser.

Content Security Policy

If your site sends a Content Security Policy, allow the SaaSFunnels host:

HTTP
script-src 'self' https://app.saasfunnels.com;
style-src 'self' https://app.saasfunnels.com;
font-src 'self' https://app.saasfunnels.com;
connect-src 'self' https://app.saasfunnels.com;
img-src 'self' data: https://kuwwcbzjj6mtbzva.public.blob.vercel-storage.com;
frame-src https://www.youtube-nocookie.com https://player.vimeo.com;

The img-src host serves built-in Funnel images. Add any other image hosts you use. frame-src is only needed for YouTube or Vimeo videos. With a nonce-based policy, put the nonce on the loader script and pass the same value as styleNonce.

What's next