https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
i

important-tailor-75460

12/13/2023, 10:16 AM
hi GrowthBook team, I have implemented GrowthBook SDK in NextJs APP router structure I followed this documentation but the problem is that this part is not executed while I need experiment.key and result.key to save in my db and then I use it for metrics, can someone help me why trackingCallback is not executed.
Copy code
trackingCallback: (experiment, result) => {
    // TODO: Use your real analytics tracking system
    console.log("Viewed Experiment", {
      experimentId: experiment.key,
      variationId: result.key,
    });
  },
f

famous-bird-26809

12/13/2023, 10:20 AM
I have the same problem
I can see changes configured in expirement on my site, but no trackingCallback is being executed
i

important-tailor-75460

12/13/2023, 10:23 AM
exactly same problem, I am trying from last night to get it work but no success, I also tried this example https://github.com/esauri/growthbook-next-app-router but it doesn’t work
f

fresh-football-47124

12/13/2023, 10:30 AM
do you have an experiment rule being passed in?
i

important-tailor-75460

12/13/2023, 10:38 AM
I have create A/B testing from GrowthBook dashboard this is my SDK: https://cdn.growthbook.io/api/features/sdk-xqzBZG2b1EnS5L8a
@fresh-football-47124 is there any API Endpoint like this https://cdn.growthbook.io/api/features/ where I can fetch experiments and then I can pass to GrowthBook SDK
@fresh-football-47124 I managed to fetch my experiments and pass it in new GrowthBook initialization but when I console.log(growthbook) I get this result check trackingCallback:
Copy code
TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
    at Function.invokeGetter (<anonymous>:3:28)
f

famous-bird-26809

12/13/2023, 4:07 PM
same here
I have very same issue on NuxtJS app - JS SDK is injected on site correctly, I can fetch and use feature flags and experiments with isOn()/isOff() functions, but once used - it did not trigger provided trackingCallback function. I tried with enabled and disabled devMode, but it didn't changed anything.
i

important-tailor-75460

12/13/2023, 4:44 PM
I found a way that works I’ll explain shortly
👍 1
f

famous-bird-26809

12/14/2023, 11:32 AM
@important-tailor-75460 have u got any tips about this trackingCallback? 😄 i have uploaded some more info in this thread https://growthbookusers.slack.com/archives/C01T6Q1SVFV/p1702449481072899
i

important-tailor-75460

12/15/2023, 10:29 AM
Hi @famous-bird-26809 I’ll explain how I have implemented in NextJS APP router structure, in GrowthBook documentation it has 2 ways to implement GrowthBook in NextJs one is server side and the other one is client side I have implemented the client side. First step fetch manually Features:
Copy code
const getFeatures = await fetch(`<https://cdn.growthbook.io/api/features/${process.env.NEXT_PUBLIC_GROWTH_BOOK_FEATURES_SDK}>`);
const { features } = await getFeatures.json();
return { features };
You can find your Features endpoint in GrotwhBook dashboard by going SDK configuration -> SDK Connections -> and then click in your SDK Connection and copy Full API Endpoint After fetching your Features then you can Initialize your GrowthBook: Note: do not forget to pass features that you fetched in new GrowthBook({features}) check example beneath
Copy code
const gb = new GrowthBook({
            enableDevMode: true,
            features,
            trackingCallback: (experiment, result) => {
                if (process.env.NEXT_PUBLIC_ENVIRONMENT === ENVIRONMENT_MODE.DEV) {
                    console.log("Viewed Experiment", {
                        experimentId: experiment.key,
                        variationId: result.variationId,
                    });
                }
            },
        });
        gb.setAttributes({
            id: user.id,
        });
NOTE: also it is important to provide id
Copy code
gb.setAttributes({
            id: user.id,
        });
Now whenever you use feature flag the experiment that is connected to that feature flag will be called automatically and should trigger trackingCallback --------------------------------------------------- if this doesn’t work for you then you can try to play manually with experiments by fetching experiments from the API https://api.growthbook.io/api/v1/experiments/ this one requires Bearer token more info you can get here: https://docs.growthbook.io/api/#tag/experiments/operation/listExperiments also you can fetch experiments for one specific project that you want by providing projectId query param https://api.growthbook.io/api/v1/experiments/?projectId= and then try to create GrowthBook with experiments param
Copy code
const gb = new GrowthBook({
            enableDevMode: true,
            features,
            experiments,
            trackingCallback: (experiment, result) => {
                if (process.env.NEXT_PUBLIC_ENVIRONMENT === ENVIRONMENT_MODE.DEV) {
                    console.log("Viewed Experiment", {
                        experimentId: experiment.key,
                        variationId: result.variationId,
                    });
                }

               
            },
        });
        gb.setAttributes({
            id: user.id,
        });
b

brief-honey-45610

12/15/2023, 4:41 PM
@important-tailor-75460 Hi again, we are still looking into this. Could you please tell me: 1. Which unique attribute are you using per user? Something like user_id or anonymous_id? 2. Have you been running into this issue while using preview links? 3. Are you certain that the user for whom the trackingCallback is not firing should be included in the experiment? 4. Are you getting your very first experiment setup with GrowthBook, or have you already been running experiments successfully and this is happening now all of a sudden? 5. Are visual experiments enabled for the SDK key (client key) you're using for this experiment? This is something you would have had to toggle on when creating the key. 6. Which specific SDK are you using? Is it an official GrowthBook SDK or one created by a 3rd party? 7. Which guide or tutorial are you following to get the experiment set up? Your response to each of these questions will be very helpful in narrowing down what is causing the issue. It’s like not with the trackingCallback itself, since that code hasn’t changed in a long time, but it could be something related to the SDK payload and that’s the angle we’re currently investigating.
i

important-tailor-75460

12/17/2023, 6:03 PM
@brief-honey-45610 I have fixed the problem by fetching my features
Copy code
const getFeatures = await fetch(`<https://cdn.growthbook.io/api/features/${process.env.NEXT_PUBLIC_GROWTH_BOOK_FEATURES_SDK}>`);
const { features } = await getFeatures.json();
return { features };
and then:
Copy code
const growthbook = new GrowthBook({
        enableDevMode: true,
        features,
        trackingCallback: (experiment, result) => {
                console.log("Viewed Experiment", {
                    experimentId: experiment.key,
                    variationId: result.variationId,
                });
        },
    });
    growthbook.setAttributes({
        id: user.id,
    });
id: user.id -> this is id that I generate with firebase SDK but It could be any unique id then I wrap my app with GrowthBookProvider:
Copy code
<GrowthBookProvider growthbook={growthbook}>{children}</GrowthBookProvider>;
and after this whenever I use my feature flag that is connected with an experiment in GrowthBook dashboard it returns me feature flag value true or false but also it triggers trackingCallback
f

famous-bird-26809

12/18/2023, 7:26 AM
thanks a lot @important-tailor-75460 for your time
4 Views