The SDK keeps a user ID in the browser to remember what each user has read,
liked, voted on, and answered. This page shows how to use your own user IDs,
change users, set user attributes for personalization, and set the Clips
language.
If you don't pass an externalId the first time you call initialize, the SDK
creates an anonymous user ID and stores it in the browser's local storage. With
the default privacy options, the SDK uses this ID to
keep track of:
which Pages the user has read
which Polls the user has voted in
which Quizzes the user has answered
which Clips the user has liked or viewed
You don't need extra code for this. Initialize the SDK:
With the default privacy options, initialize loads the viewing history that
Storyteller saved for this ID: read Pages, Clip likes and views, and Poll and
Quiz answers. The history follows the user to every browser and device where
they sign in with the same externalId.
Use an ID that is unique to the user and never changes, such as your account
ID. Avoid values that can change, such as an email address.
Call initialize as soon as you know the externalId, for example on page
load or when a user signs in.
Note
The SDK hashes the externalId before it stores or sends it, to support
Video Privacy Protection Act (VPPA) compliance.
When a user signs out, or another user signs in, call initialize again with
the new externalId. Pass null when the user continues anonymously. A call
without externalId keeps the current user ID, so it doesn't sign a user out.
clears the read status, likes, and answers of the previous user
loads the viewing history that Storyteller saved for the new user ID; with
null, the user starts with an empty history
Every initialize call also resets Storyteller.sharedInstance.theme to its
defaults. Set the global theme again after the call resolves. A theme set in a
view's configuration is kept.
User attributes let you personalize rows and grids and target Stories to groups
of users. For more information, see
Personalization
and
Audience Targeting
in the Storyteller User Guide.
The SDK adds the attribute, with the key location and the value New York,
to its requests to Storyteller. Use it to personalize or target Stories in the
Storyteller CMS.
To set more attributes, call setUserAttribute once for each key. Keys and
values must be non-empty strings; otherwise the method throws an error. When
enablePersonalization is false, the SDK doesn't store attributes.
Note
Set user attributes after initialize resolves. A user change during
initialize clears them.
{"slug": "users", "page_title": "Identify and Personalize Users", "page_url": "Users/", "canonical_url": "/web/Users/", "markdown": "# Identify and personalize users\n\n<!-- markdownlint-disable MD033 -->\n<span id=\"working-with-users\"><\/span>\n<!-- markdownlint-enable MD033 -->\n\nThe SDK keeps a user ID in the browser to remember what each user has read,\nliked, voted on, and answered. This page shows how to use your own user IDs,\nchange users, set user attributes for personalization, and set the Clips\nlanguage.\n\n## User IDs\n\nIf you don't pass an `externalId` the first time you call `initialize`, the SDK\ncreates an anonymous user ID and stores it in the browser's local storage. With\nthe default [privacy options](PrivacyAndTracking.md), the SDK uses this ID to\nkeep track of:\n\n- which Pages the user has read\n- which Polls the user has voted in\n- which Quizzes the user has answered\n- which Clips the user has liked or viewed\n\nYou don't need extra code for this. Initialize the SDK:\n\n```javascript\nawait Storyteller.sharedInstance.initialize('demo-api-key');\n```\n\n!!! note\n\n Replace `demo-api-key` with your Storyteller Web SDK API key. To request\n one, email [hello@getstoryteller.com](mailto:hello@getstoryteller.com).\n\n### Set a user ID {#setting-a-user-id}\n\nIf your site has user accounts, pass your own user ID as `externalId` in the\nsecond `initialize` argument:\n\n```javascript\nawait Storyteller.sharedInstance.initialize('demo-api-key', {\n externalId: 'your-user-id',\n});\n```\n\nWith the default privacy options, `initialize` loads the viewing history that\nStoryteller saved for this ID: read Pages, Clip likes and views, and Poll and\nQuiz answers. The history follows the user to every browser and device where\nthey sign in with the same `externalId`.\n\nUse an ID that is unique to the user and never changes, such as your account\nID. Avoid values that can change, such as an email address.\n\nCall `initialize` as soon as you know the `externalId`, for example on page\nload or when a user signs in.\n\n!!! note\n\n The SDK hashes the `externalId` before it stores or sends it, to support\n Video Privacy Protection Act (VPPA) compliance.\n\n### Change users {#changing-users}\n\nWhen a user signs out, or another user signs in, call `initialize` again with\nthe new `externalId`. Pass `null` when the user continues anonymously. A call\nwithout `externalId` keeps the current user ID, so it doesn't sign a user out.\n\n```javascript\nasync function onSignIn(userId) {\n await Storyteller.sharedInstance.initialize('demo-api-key', {\n externalId: userId,\n });\n}\n\nasync function onSignOut() {\n await Storyteller.sharedInstance.initialize('demo-api-key', {\n externalId: null,\n });\n}\n```\n\nWhen the user ID changes, the SDK:\n\n- clears the stored user attributes\n- clears the read status, likes, and answers of the previous user\n- loads the viewing history that Storyteller saved for the new user ID; with\n `null`, the user starts with an empty history\n\nEvery `initialize` call also resets `Storyteller.sharedInstance.theme` to its\ndefaults. Set the global theme again after the call resolves. A theme set in a\nview's `configuration` is kept.\n\n### Sample code for user IDs {#sample-code-for-user-ids}\n\nThe Storyteller Web Showcase's [`persistUserIdAndReload`](https://github.com/getstoryteller/storyteller-showcase-web/blob/11.0.0/nextjs/src/components/pages/showcase/account/accountPersistence.ts#L39)\nhelper shows how to store, clear, and apply a user ID.\n\n## Personalization and targeted Stories {#personalization-and-targeted-stories}\n\nUser attributes let you personalize rows and grids and target Stories to groups\nof users. For more information, see\n[Personalization](https://www.getstoryteller.com/user-guide/stories-and-scheduling/personalization)\nand\n[Audience Targeting](https://www.getstoryteller.com/user-guide/stories-and-scheduling/audience-targeting)\nin the Storyteller User Guide.\n\n### Set user attributes {#setting-user-attributes}\n\nTo set a user attribute, call `setUserAttribute` on `Storyteller.User` with the\nattribute key and its value. For example, to set the user's location:\n\n```javascript\nStoryteller.User.setUserAttribute('location', 'New York');\n```\n\nThe SDK adds the attribute, with the key `location` and the value `New York`,\nto its requests to Storyteller. Use it to personalize or target Stories in the\nStoryteller CMS.\n\nTo set more attributes, call `setUserAttribute` once for each key. Keys and\nvalues must be non-empty strings; otherwise the method throws an error. When\n`enablePersonalization` is `false`, the SDK doesn't store attributes.\n\n!!! note\n\n Set user attributes after `initialize` resolves. A user change during\n `initialize` clears them.\n\n### Remove user attributes {#removing-user-attributes}\n\nTo remove a user attribute, call `removeUserAttribute` on `Storyteller.User`\nwith the attribute key. For example, to remove the user's location:\n\n```javascript\nStoryteller.User.removeUserAttribute('location');\n```\n\n!!! note\n\n When a user signs out and you don't call `initialize` again, remove each\n attribute you set.\n\n### Update the Clips locale {#updating-the-clips-locale}\n\nTo set the language for Clips, call `setLocale` on `Storyteller.User` with a\nlanguage code. For example, to set the language to Spanish:\n\n```javascript\nStoryteller.User.setLocale('es');\n```\n\nThe SDK stores the language code as the `stLocale` user attribute, so it\nfollows the same rules as other user attributes.\n\n### Sample code for user attributes {#sample-code-for-user-attributes}\n\nThe Storyteller Web Showcase's [`persistAndApplyAttributeValues`](https://github.com/getstoryteller/storyteller-showcase-web/blob/11.0.0/nextjs/src/components/pages/showcase/account/accountPersistence.ts#L16)\nhelper shows how to use these user attributes.\n", "copy_markdown_include_header": false, "base_path": "", "ai_dir": "ai", "missing_payload_behavior": "empty"}