wide-cartoon-5349
09/24/2024, 2:49 PMorange-terabyte-33302
09/24/2024, 2:49 PMorange-terabyte-33302
09/24/2024, 2:50 PMsetAttributes
method is used to specify attributes about the current user and request in GrowthBook. These attributes are used for feature targeting and assigning persistent variations in A/B tests. Here's how you can set it up: javascript import { useEffect } from "react"; import { GrowthBook, GrowthBookProvider } from "@growthbook/growthbook-react"; // Create a GrowthBook instance const gb = new GrowthBook({ apiHost: "<https://cdn.growthbook.io>", clientKey: "sdk-abc123", // Only required for A/B testing // Called every time a user is put into an experiment trackingCallback: (experiment, result) => { console.log("Experiment Viewed", { experimentId: experiment.key, variationId: result.key, }); }, }); gb.init() export default function App() { useEffect(() => { // TODO: Set user attributes for targeting gb.setAttributes({ id: user.id, company: user.company, }); }, [user]) return ( <GrowthBookProvider growthbook={gb}> <OtherComponent /> </GrowthBookProvider> ); }
In this example, user.id
and user.company
are the attributes being set. You would replace user.id
and user.company
with the actual user id and company you want to set. The useEffect
hook is used to set the attributes when the user
changes. Please note that setAttributes
completely overwrites all existing attributes. If you want to do a partial update, you can use the following pattern: javascript gb.setAttributes({ // Only update the `id` attribute, keep the rest the same ...gb.getAttributes(), id: user.id })
In this case, only the id
attribute is updated, and all other attributes remain the same. For more information, you can refer to the [GrowthBook documentation](https://docs.growthbook.io/lib/react-native).wide-cartoon-5349
09/24/2024, 2:53 PMorange-terabyte-33302
09/24/2024, 2:53 PMorange-terabyte-33302
09/24/2024, 2:54 PMjavascript import { useEffect } from "react"; import { GrowthBook, GrowthBookProvider } from "@growthbook/growthbook-react"; // Create a GrowthBook instance const growthbook = new GrowthBook({ trackingCallback: (experiment, result) => { // tracking code here }, }); export default function MyApp({ Component, pageProps }) { useEffect(() => { // Load feature definitions from API fetch(process.env.NEXT_PUBLIC_GROWTHBOOK_FEATURES_URL) .then((res) => res.json()) .then((json) => { growthbook.setFeatures(json.features); }); // Set attributes growthbook.setAttributes({ // other attributes here id: getAnonymousId(), // replace getAnonymousId() with your function to get the anonymous id }); }, []); return ( <GrowthBookProvider growthbook={growthbook}> <Component {...pageProps} /> </GrowthBookProvider> ); }
In this example, getAnonymousId()
is a function that retrieves the anonymous ID from a cookie or local storage. You would replace this with your actual function to get the anonymous ID. For more information, you can refer to the [GrowthBook documentation](https://docs.growthbook.io/guide/rudderstack-and-nextjs-with-growthbook#4-integrate-the-growthbook-react-sdk-into-our-nextjs-app-part-2).wide-cartoon-5349
09/24/2024, 3:05 PMorange-terabyte-33302
09/24/2024, 3:05 PMorange-terabyte-33302
09/24/2024, 3:05 PMuser.id
attribute is specific to your application and depends on how you handle user identification. GrowthBook provides the tools and methods to set these attributes, but it doesn't know the specifics of your application, such as how you generate or store user IDs. In the examples provided in the GrowthBook documentation, a placeholder user.id
is often used to demonstrate how you might set this attribute. However, in your actual application, you would replace user.id
with the method or variable you use to retrieve the user's ID. Here's an example from the documentation: javascript growthbook.setAttributes({ id: user.id, company: user.company, });
In this case, user.id
and user.company
are placeholders. You would replace these with the actual methods or variables you use to retrieve the user's ID and company. For anonymous users, you might generate a random ID or use a session ID. This would also be set using the setAttributes
method: javascript growthbook.setAttributes({ id: generateAnonymousId(), // replace generateAnonymousId() with your function to generate an anonymous id });
In this case, generateAnonymousId()
is a placeholder for your function that generates an anonymous ID. For more information, you can refer to the [GrowthBook documentation](https://docs.growthbook.io/guide/rudderstack-and-nextjs-with-growthbook#4-integrate-the-growthbook-react-sdk-into-our-nextjs-app-part-2).wide-cartoon-5349
09/24/2024, 3:06 PMorange-terabyte-33302
09/24/2024, 3:06 PMorange-terabyte-33302
09/24/2024, 3:06 PMclientId
(for anonymous users) or userId
(for logged-in users) as the identifier. These identifiers should be set when initializing the GrowthBook SDK. Here's an example: javascript const growthbook = new GrowthBook({ user: { id: "123", // replace with the actual method to retrieve the GA4 clientId or userId }, trackingCallback: (experiment, result) => { // tracking code here }, });
In this example, "123"
is a placeholder. You would replace this with the actual method to retrieve the GA4 clientId
or userId
. For more information, you can refer to the [GrowthBook documentation](https://docs.growthbook.io/guide/GA4-google-analytics#sdk-integration-for-ga4).