Skip to content

Install the SDK

Copy your site's snippet from UserLoop, add it once to every page, list your site's addresses, and confirm the SDK has loaded.

The Web SDK shows UserLoop’s website surveys on any site. You add one snippet to every page, once; after that, every website survey published in UserLoop reaches the site without another code change, and your own code can call window.userloop.

Before you start

  • The organisation has a website set up in UserLoop. It’s set up the first time someone saves a website survey, or by an organisation admin in Settings → Website with Set up your website.
  • You can change the HTML every page of the site shares, such as a layout, template or index.html.

On Next.js, React or another single-page app, read Install on Next.js, React and single-page apps for where the snippet goes, then come back for the addresses and the check.

Get the snippet and site key

  1. Open Settings → Website

    In UserLoop, open Settings, then Website. The first card is Add UserLoop to your site. The same snippet is in a website survey’s Launch stage, under Add UserLoop to your site.

  2. Choose HTML

    Under Your site is built with, choose HTML. It gives the loader snippet, which works on any site. The site builders in the list (WordPress, Webflow, Framer, Squarespace, Wix) give the plain script tag instead; Google Tag Manager gives the loader.

    Settings, Website: the Add UserLoop to your site card, with the Live install status, Your site is built with set to HTML, the snippet with its copy button, and the Site key below it with its own copy button
  3. Copy the code

    Select the copy button on the code. The Site key under it, which starts site_, is the only part that differs from one site to another.

The site key is public: it is in the page source of every site that has the snippet, and it grants nothing. The pages on this site write it as SITE_KEY.

If someone sent you UserLoop’s message from Send to your developer, the code in it is the same loader snippet.

Add the snippet

This is the loader snippet, with your site key in place of SITE_KEY:

<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":"SITE_KEY"});
</script>

Put it once on every page, inside <head> or before </body>, in the layout or template every page shares. Never add it per component or per route: a second copy on the same page does nothing.

What it does:

  • It defines window.userloop at once, as a stand-in that queues calls. Your code can call userloop.track(), userloop.setAttributes() and the other methods straight after it, before the SDK has loaded.
  • It loads userloop.js asynchronously, so it doesn’t hold up the page.
  • The object at the end, {"siteKey":"SITE_KEY"}, is passed to userloop.init(). Add other options to it, such as "debug":true while you test. SDK reference lists them.

When the SDK loads, it replaces the stand-in, runs the queued calls in order and fetches the site’s live website surveys.

If your site sends a Content-Security-Policy

Allow these sources:

Directive Sources Why
script-src https://cdn.userloop.io userloop.js, and the file that draws surveys, which it loads from the same place the first time a survey opens.
script-src the snippet’s nonce or hash, or 'strict-dynamic' The snippet is an inline script. Put nonce="…" on its <script> tag: the snippet passes the nonce on to the scripts it loads. The plain tag needs no inline allowance.
connect-src https://s.userloop.io https://data.userloop.io https://survey-views.userloop.io The site’s surveys, the answers and the count of times a survey was shown.
img-src https: data: Logos and pictures in surveys.

No style-src, font-src or frame-src entry is needed in current browsers. In a browser without constructable stylesheets, the SDK adds a <style> tag with the snippet’s nonce.

Or use the plain script tag

Where you can only add a <script src> tag, use this instead of the loader:

<script async src="https://cdn.userloop.io/sdk-3/userloop.js" data-site-key="SITE_KEY"></script>

The SDK reads data-site-key and starts itself. But window.userloop exists only once the script has loaded, so page code must wait for the UserLoopReady event before calling it:

<script>
  window.addEventListener("UserLoopReady", function () {
    userloop.setAttributes({ plan: "pro" });
  });
</script>
<script async src="https://cdn.userloop.io/sdk-3/userloop.js" data-site-key="SITE_KEY"></script>

To call methods before the tag loads, or to pass init options with it, define the stand-in first, without the part that loads the script. A queued init call then replaces the tag’s own, with all its options:

<script>
  !function(w){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)])}})}(window);
  userloop.init({ siteKey: "SITE_KEY", debug: true });
  userloop.track("signed_up");
</script>
<script async src="https://cdn.userloop.io/sdk-3/userloop.js" data-site-key="SITE_KEY"></script>

UserLoop’s older script defined window.UserLoop, with a capital L. It is a different SDK; this one never touches it, so a page can carry both.

Add your site’s addresses

Until the site has an address listed, surveys show wherever the snippet runs. List every address the site uses, so a copied snippet shows nothing on anyone else’s site.

  1. Open Your site’s addresses

    In Settings → Website, find Your site’s addresses. Only organisation admins can change it; everyone else can see it.

  2. Add each address

    Type an address, such as example.com or https://shop.example.com, and select Add. A site has at most 20.

How the SDK matches the page’s origin (scheme, host and port) against the list:

You add Matches Doesn’t match
example.com https://example.com https://www.example.com, http://example.com
www.example.com https://www.example.com https://example.com
*.example.com https://www.example.com, https://shop.example.com https://example.com
http://example.com:8080 http://example.com:8080 https://example.com

An address without a scheme is saved as https://. A path, query or trailing slash is dropped. To cover both example.com and its subdomains, add both. localhost, 127.0.0.1 and [::1] on any port are always allowed, so you can test on your own computer without listing them.

Check it worked

Open a page of the site with the snippet, then open the browser’s developer console.

  • Type userloop.version. You should see the SDK’s version, such as "3.0.0+1a2b3c4". undefined means the SDK hasn’t loaded: the stand-in has no version.
  • In the network panel, you should see GET https://s.userloop.io/api/site/SITE_KEY?language=en answer 200, with the site’s live website surveys under surveys (an empty list when none is published). The language is the page’s <html lang>, else the browser’s.

To check from code, listen for UserLoopReady, which fires once on window when the SDK has loaded. Add the listener before the snippet, or check userloop.version first in case it has already fired:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>UserLoop install check</title>
    <script>
      window.addEventListener("UserLoopReady", function (event) {
        console.log("UserLoop SDK loaded:", event.detail.version, "visitor", userloop.getVisitorId());
      });
    </script>
    <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":"SITE_KEY"});
    </script>
  </head>
  <body>
    <h1>UserLoop install check</h1>
  </body>
</html>

Save it as index.html, serve the folder (for example npx serve or python3 -m http.server 8000) and open it at http://localhost:…. The console shows UserLoop SDK loaded: 3.0.0+… visitor …. Opening the file straight from disk (file://) loads the SDK but never shows a survey.

UserLoop’s own check

UserLoop also checks whether the snippet is on the site. In Settings → Website, or the survey’s Launch stage, select Check installation, or I’ve added it to have it check every few seconds for five minutes. Once a page with the snippet has loaded from one of the site’s addresses, it shows Live and Installed on that address. Pages on localhost never count, so this check needs the real site. Every other message is explained in The survey isn’t showing on your website.

If it doesn’t load

You see What to do
userloop.version is undefined, and no request to cdn.userloop.io The snippet isn’t on this page, or the page wasn’t published. Check the page source.
A blocked request to cdn.userloop.io or s.userloop.io in the console Add the sources in the table above to the page’s Content-Security-Policy.
The site request answers 404 The site key is wrong or the site was deleted. Copy the snippet again from Settings → Website.
The SDK loads, but no survey shows See Test and debug an install.

Type to search…

↑↓ navigate↵ selectEsc close