Hi, Is there a way to assign a variation yourself ...
# ask-questions
f
Hi, Is there a way to assign a variation yourself to a new user, when initializing the GrowthBook instance on the client side? Some background: We use A/B testing on static pages with Next.js. When a static page is requested, the user is sent to the correct variation based on the attribute.id that is sent in the request headers with a cookie. When there is no cookie, then we let GrowthBook pick a variation in the middleware and sent the new attribute.id to the user with the cookies. Only we have a problem that when a user lands on our site for the first time, they first have to consent for cookies before we are allowed to use the cookies. We can still choose a random variant in the middleware. In the client-side we know the experiment variation that the user sees, but we don't have the cookie, so we need a way to assign the variation in the GrowthBook instance ourselfs instead of GrowthBook choosing for us.
f
hrmm, you dont need to use the growthBook SDK to do that - you could just fire the event yourself if you have already assigned them
f
We could fire an "experiment viewed" tracking event manually, but if the visitor refreshes the page or visits it again later, we of course want to give the same variation. And that is only possible if this variation is linked to the attribute.id
s
If I understand correctly, if the users do not accept cookies, we cannot guarantee the same variation if they refresh the page. For users who do eventually accept the cookie, but initially request without one, what you can do is generate an ID in the middleware (at the same step as assigning the variation). Then send it to the client, and once they accept cookies, store that ID for continued use. Does that make sense?
f
We know the variation based on the URL. For example, a user navigates to /somePage and we redirect that in the middleware to /pregenerated/somePage/variant=1/ Then we know from the URL that the user sees variant 1. I understand the idea of providing an ID from the middleware, but the page is generated during the build and the only way I know to provide something is with a cookie, and I don't want that. We had some other issues with the static pages, so we decided to refactor it and we are now going to use streaming SSR.
s
Ok, that makes sense. Thank you sharing