rhythmic-psychiatrist-74694
03/29/2022, 12:59 PMexport const useSetGrowthbookAttributes = (
attributes: Record<string, string | number | boolean>
) => {
const gb = useGrowthBook();
useEffect(() => {
gb?.setAttributes(attributes);
}, []);
};
But would love to hear other ideas!future-teacher-7046
setAttributes
completely overwrites all existing attributes. If you want to be able to set partial attributes, you can do:
gb.setAttributes({
...gb.getAttributes(),
...attributes
})
In a lot of cases, attributes can be set at a higher level, like in _app.tsx
or in your auth handler. For example, this in _app.tsx will keep a URL attribute up to date as you navigate the app:
const router = useRouter()
useEffect(() => {
const update = (url) => {
gb.setAttributes({
...gb.getAttributes(),
url: url
})
}
router.events.on('routeChangeStart', update)
return () => router.events.off('routeChangeStart', update)
}, [])
rhythmic-psychiatrist-74694
03/29/2022, 1:47 PMid
in growthbook, but if I have a page that is shown to both anonymous and logged in users, I want them to have the same experience when logging in. If I generate a random ID when they first land on the page, how do I make sure that they see the same variant when they log in (I would like to tie the userId to id
when logged in)?future-teacher-7046
id
would always be the logged-in user id (or null or empty string when anonymous). And a second sessionId
or similar would always be set for everyone and based on a cookie or local storage value.id
and it's an anonymous user, they will be excluded from the experiment since that attribute will be null. If you want to run an experiment for everyone, you would choose to split on sessionId
insteadrhythmic-psychiatrist-74694
03/29/2022, 1:57 PMid
in the attributes after they log in while using the sessionId
for assigning variants. Thanks!