Skip to content

Identify visitors and set attributes

Tell UserLoop who is signed in with identify(), target surveys on your own data with setAttributes(), clear it all with reset(), and see what the SDK keeps in the browser.

Your site knows things UserLoop can’t see: who is signed in, their plan, how many projects they have. identify() and setAttributes() hand those facts to the SDK, so they go with the visitor’s answers and a survey can be shown only to the visitors they describe. reset() forgets them when the visitor signs out.

Before you start

Identify a signed-in visitor

Call identify() with your own ID for the user, and any traits:

userloop.identify("user_1234", {
  email: "ada@example.com",
  plan: "pro",
  projects: 3,
});
  • The user ID is kept in the browser and sent with each of the visitor’s answers, from now until reset(). It must be a non-empty string.
  • email is the address the answers belong to, sent with each answer. It is never an attribute, so a targeting rule can’t read it. It is kept in memory only, never stored in the browser, so call identify() on every page load while the visitor is signed in.
  • Every other trait becomes a visitor attribute, exactly as setAttributes() would set it.

identify() can be called straight after the snippet: calls made before the SDK has loaded are queued.

Set attributes

userloop.setAttributes({ plan: "pro", projects: 3, beta: true, teams: ["design", "growth"] });
  • Attributes are merged into what is already set. Setting a key again replaces its value; null removes it:
userloop.setAttributes({ beta: null }); // removes beta, keeps plan, projects and teams
  • A value is a string, a number, true or false, null, or a list of strings. Anything else is ignored.
  • They are kept in the browser, so they hold across pages and visits until you change them or call reset().
  • They are sent with each of the visitor’s answers.
  • Limits: 50 keys (new keys past 50 are dropped), keys of 1 to 64 characters, strings cut to 500 characters, lists cut to 50 strings.
  • A key named email is treated as in identify(): the address the answers belong to, not an attribute.

Show a survey only to some visitors

On the survey’s Targeting stage, under Who, Visitor attributes holds rules on the attributes your site sets. Select Add an attribute rule, type the attribute’s name, choose how to compare and type the value. Every rule must hold for the survey to show.

Rule Holds when
is The value equals the rule’s. Two numbers are compared as numbers (3 and "3.0" are equal); anything else ignores capitals and the spaces around it.
is not is doesn’t hold, including when the attribute isn’t set.
contains The value contains the rule’s text, ignoring capitals.
is set The attribute has a value that isn’t empty.
isn’t set The attribute is missing, null or empty, including when it isn’t set.

true and false compare as the text true and false. On a list, is and contains hold when any item matches.

For example, with the rule plan is pro, the survey shows to this visitor and not to one whose site sets plan: "free".

The SDK checks the rules again every time attributes change. A survey waiting for its trigger can start waiting when an attribute makes it eligible, and stops waiting when one makes it ineligible; a survey that is already open stays open. Set attributes as early as you can, straight after the snippet, so they are in place before a trigger such as After a wait fires.

Sign out with reset()

When the visitor signs out, call reset():

userloop.reset();

It gives the browser a new visitor ID and forgets the user ID, the email, every attribute, the record of which surveys this browser has seen and answered, and the visit. To UserLoop, the next person on that browser is a new visitor: surveys they’d see as new visitors can show again. Answers still waiting to be sent are kept and sent.

The visitor ID

Each browser gets a visitor ID the first time the SDK runs there: a random UUID, sent with every answer. It is the same on every page and visit until reset(), or until the browser’s storage is cleared.

window.addEventListener("UserLoopReady", function () {
  console.log(userloop.getVisitorId()); // "0f8d3a52-6c1e-4b7a-9d2f-3e5a7c9b1d04"
});

getVisitorId() exists only once the SDK has loaded, so call it after UserLoopReady. It returns null until init has run.

What the SDK stores in the browser

The SDK sets no cookies. With the default storage, it keeps these keys:

Key Where What
ul3.visitor localStorage The visitor ID, and when the browser first and last visited.
ul3.user localStorage The user ID from identify().
ul3.attrs localStorage The visitor’s attributes.
ul3.surveys localStorage When each survey was last shown, answered, completed or dismissed in this browser, for How often. At most 100 surveys.
ul3.queue localStorage Answers waiting to be sent, so they survive a page change. At most 50.
ul3.session sessionStorage This visit: its landing page’s referrer and utm_ values, and the surveys shown and answered in it.

A visit lasts as long as the tab’s sessionStorage. A visitor is “returning” when their first visit was in an earlier one.

Choose where it stores things

Set storage in the snippet’s options:

<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","storage":"memory"});
</script>
storage Keeps Then
"local" (default) Everything in localStorage, the visit in sessionStorage The visitor, their attributes and How often hold across visits.
"session" Everything in sessionStorage Each tab is a new visitor. How often counts only within the tab.
"memory" Nothing Each page load is a new visitor: How often, new or returning and attributes start again on every page. Answers still waiting when the visitor leaves aren’t retried on the next page.

init runs once per page, so the choice holds until the next page load. A site that needs consent before storing anything can start with "memory" and use "local" from the first page load after consent.

If the browser refuses storage (blocked site data, a full quota, some private windows), the SDK keeps working in memory for the rest of the page and emits an error event with code storage.

Check it worked

In the browser console on a page of your site:

userloop.identify("user_1234", { plan: "pro" });
JSON.parse(localStorage.getItem("ul3.attrs")); // { values: { plan: "pro" }, v: 1 }
JSON.parse(localStorage.getItem("ul3.user"));  // { id: "user_1234", v: 1 }

A survey with the rule plan is pro now waits for its trigger on this page. After userloop.reset(), both keys are gone and userloop.getVisitorId() returns a new ID.

Type to search…

↑↓ navigate↵ selectEsc close