One of the features of Storyteller is the ability to set custom attributes for requests. These custom attributes can be used to provide additional information about a request, such as the user's location or the device they are using. They can also be used for audience targeting, personalization, A/B testing, and analytics segmentation.
To set a custom attribute, you can use the setCustomAttribute() method of the StorytellerSdk object. This method takes two parameters: the key of the custom attribute and its value. For example, to set a custom attribute for the user's location, you can use the following code:
To retrieve all custom attributes, you can use the customAttributes() method. This method returns a Promise that resolves to an object containing all custom attributes:
try{constattributes=awaitStorytellerSdk.customAttributes();console.log(attributes);// { location: "New York", age: "25", isPremium: "true" }}catch(error){console.error("Failed to get custom attributes:",error);}
To remove a custom attribute, you can use the removeCustomAttribute() method of the StorytellerSdk object. This method takes one parameter: the key of the custom attribute to remove. For example, to remove the custom attribute for the user's location, you can use the following code:
To set a followed category, call the addFollowedCategory() function for a singular category or addFollowedCategories() for a collection. Access these methods through the StorytellerSdk object. For instance, to designate a followed category attribute for the user, implement the following code:
To remove a followed category, utilize the removeFollowedCategory() method of the StorytellerSdk object. This method requires a single parameter—the key of the followed category to be removed. For example, to remove a followable category related to the user's location, utilize the following code:
StorytellerSdk.removeFollowedCategory('location')
This function accepts a singular string parameter, removing the specified category from the locally stored list.
To remove multiple followed categories at once, use removeFollowedCategories():
To get a list of currently followed categories, utilize the followedCategories() method. This method returns a Promise that resolves to an array of followed category strings:
try{constcategories=awaitStorytellerSdk.followedCategories();console.log(`Followed categories: ${categories}`);}catch(error){console.error("Failed to get followed categories:",error);}
To fetch the backend-backed category catalog with current follow state, use getFollowableCategories(). This is useful when building your own category management UI:
importtype{StorytellerFollowableCategories,StorytellerFollowableCategory,StorytellerFollowableCategoryPlacement,}from'@getstoryteller/react-native-storyteller-sdk';try{const{categories}:StorytellerFollowableCategories=awaitStorytellerSdk.getFollowableCategories();categories.forEach(category=>{console.log(category.id,category.displayTitle??category.name,category.isFollowed);});}catch(error){console.error("Failed to get followable categories:",error);}
Each category can include id, name, displayTitle, externalId, type, placement, thumbnailUrl, and isFollowed.
StorytellerFollowableCategory and StorytellerFollowableCategoryPlacement are exported for apps that store or render this metadata. The category string fields are optional because the native iOS and Android models have different nullability guarantees; always use id as the stable required identifier.
Followed categories can be updated as a result of user actions from within the Storyteller SDK. You can listen to these event updates through the categoryFollowActionTaken event emitter:
import{useEffect}from'react';importStorytellerSdkfrom'@getstoryteller/react-native-storyteller-sdk';functionMyComponent(){useEffect(()=>{constsubscription=StorytellerSdk.categoryFollowActionTaken((event)=>{console.log('Category:',event.category.name);console.log('Is Following:',event.isFollowing);// Handle the follow/unfollow action});return()=>{subscription.remove();};},[]);return(/* ... */);}
{"slug": "user-customization", "page_title": "User Customization", "page_url": "UserCustomization/", "canonical_url": "/react-native/UserCustomization/", "markdown": "# Custom Attributes\n\nOne of the features of Storyteller is the ability to set custom attributes for requests. These custom attributes can be used to provide additional information about a request, such as the user's location or the device they are using. They can also be used for audience targeting, personalization, A/B testing, and analytics segmentation.\n\n## How to Use\n\n### Set custom attribute\n\nTo set a custom attribute, you can use the `setCustomAttribute()` method of the `StorytellerSdk` object. This method takes two parameters: the key of the custom attribute and its value. For example, to set a custom attribute for the user's location, you can use the following code:\n\n```js\nStorytellerSdk.setCustomAttribute(\"location\", \"New York\");\n```\n\nThis will add a custom attribute to all requests made by the user, with the key \"location\" and the value \"New York\".\n\nCustom attribute values must be strings. Convert non-string values to strings before setting them.\n\n```js\n// String values only\nStorytellerSdk.setCustomAttribute(\"location\", \"New York\");\nStorytellerSdk.setCustomAttribute(\"age\", String(25));\nStorytellerSdk.setCustomAttribute(\"isPremium\", String(true));\n```\n\n> Setting custom attributes should be done after the Storyteller SDK is initialized.\n\n### Replace custom attributes\n\nTo replace the full custom attribute map, use `setCustomAttributes()` with string keys and values:\n\n```js\nStorytellerSdk.setCustomAttributes({\n location: \"New York\",\n tier: \"premium\",\n});\n```\n\n### Get custom attributes\n\nTo retrieve all custom attributes, you can use the `customAttributes()` method. This method returns a Promise that resolves to an object containing all custom attributes:\n\n```js\ntry {\n const attributes = await StorytellerSdk.customAttributes();\n console.log(attributes); // { location: \"New York\", age: \"25\", isPremium: \"true\" }\n} catch (error) {\n console.error(\"Failed to get custom attributes:\", error);\n}\n```\n\n### Remove custom attribute\n\nTo remove a custom attribute, you can use the `removeCustomAttribute()` method of the `StorytellerSdk` object. This method takes one parameter: the key of the custom attribute to remove. For example, to remove the custom attribute for the user's location, you can use the following code:\n\n```js\nStorytellerSdk.removeCustomAttribute(\"location\");\n```\n\n## Best Practices\n\n- **Timing**: Set custom attributes after SDK initialization and before loading any content views\n- **Privacy**: Ensure custom attributes comply with your privacy policy and relevant regulations (GDPR, CCPA, etc.)\n- **Performance**: Custom attributes are included with every request, so avoid setting excessive or large values\n\n## Followable Categories\n\nStoryteller provides a mechanism for managing followed categories associated with clips.\n\n### How to Use\n\n#### Set followed categories\n\nTo set a followed category, call the `addFollowedCategory()` function for a singular category or `addFollowedCategories()` for a collection. Access these methods through the `StorytellerSdk` object. For instance, to designate a followed category attribute for the user, implement the following code:\n\n```js\n StorytellerSdk.addFollowedCategory('location')\n```\n\nor\n\n```js\n StorytellerSdk.addFollowedCategories(['location', 'city', 'country'])\n```\n\nThese categories are stored locally and used to decide whether the plus or tick icon is displayed in the clips UI.\n\n> Setting followable categories attributes should be done after Storyteller SDK is initialized.\n\n#### Remove followable category\n\nTo remove a followed category, utilize the `removeFollowedCategory()` method of the `StorytellerSdk` object. This method requires a single parameter\u2014the key of the followed category to be removed. For example, to remove a followable category related to the user's location, utilize the following code:\n\n```js\n StorytellerSdk.removeFollowedCategory('location')\n```\n\nThis function accepts a singular string parameter, removing the specified category from the locally stored list.\n\nTo remove multiple followed categories at once, use `removeFollowedCategories()`:\n\n```js\nStorytellerSdk.removeFollowedCategories(['location', 'city']);\n```\n\n#### Check if a category is followed\n\nTo check whether a category is currently followed, use `isCategoryFollowed()`:\n\n```js\nconst isFollowing = StorytellerSdk.isCategoryFollowed('location');\n```\n\n#### Get followed categories\n\nTo get a list of currently followed categories, utilize the `followedCategories()` method. This method returns a Promise that resolves to an array of followed category strings:\n\n```js\ntry {\n const categories = await StorytellerSdk.followedCategories();\n console.log(`Followed categories: ${categories}`);\n} catch (error) {\n console.error(\"Failed to get followed categories:\", error);\n}\n```\n\n#### Get followable categories\n\nTo fetch the backend-backed category catalog with current follow state, use `getFollowableCategories()`. This is useful when building your own category management UI:\n\n```typescript\nimport type {\n StorytellerFollowableCategories,\n StorytellerFollowableCategory,\n StorytellerFollowableCategoryPlacement,\n} from '@getstoryteller/react-native-storyteller-sdk';\n\ntry {\n const { categories }: StorytellerFollowableCategories =\n await StorytellerSdk.getFollowableCategories();\n categories.forEach(category => {\n console.log(category.id, category.displayTitle ?? category.name, category.isFollowed);\n });\n} catch (error) {\n console.error(\"Failed to get followable categories:\", error);\n}\n```\n\nEach category can include `id`, `name`, `displayTitle`, `externalId`, `type`, `placement`, `thumbnailUrl`, and `isFollowed`.\n\n`StorytellerFollowableCategory` and `StorytellerFollowableCategoryPlacement` are exported for apps that store or render this metadata. The category string fields are optional because the native iOS and Android models have different nullability guarantees; always use `id` as the stable required identifier.\n\n#### Get updates about followed categories\n\nFollowed categories can be updated as a result of user actions from within the Storyteller SDK. You can listen to these event updates through the `categoryFollowActionTaken` event emitter:\n\n```js\nimport { useEffect } from 'react';\nimport StorytellerSdk from '@getstoryteller/react-native-storyteller-sdk';\n\nfunction MyComponent() {\n useEffect(() => {\n const subscription = StorytellerSdk.categoryFollowActionTaken((event) => {\n console.log('Category:', event.category.name);\n console.log('Is Following:', event.isFollowing);\n\n // Handle the follow/unfollow action\n });\n\n return () => {\n subscription.remove();\n };\n }, []);\n\n return (/* ... */);\n}\n```\n\nFor more details, see [Storyteller Event Handlers](Storyteller.md#event-handlers).\n", "copy_markdown_include_header": false, "base_path": "", "ai_dir": "ai", "missing_payload_behavior": "empty"}