https://www.growthbook.io/ logo
#announcements
Title
# announcements
n

numerous-army-92497

03/14/2023, 10:37 AM
Another SSR question (next.js or JVM based approches): the
experiment viewed
event for e.g. Snowplow requires us to send
experiment_viewed
with it's name, and
variation_id
where as the variation_id is the index of the variation, and not its name. F.e. if I set up Feature
button-color
with variants
A
and
B
the variation_ids will be 0 and 1. However, if my GrowthBook instance lives on the server, and I call
getFeatureValue
I only get
A
or
B
and not the variant_id required for the tracking call. Right now we tried to solve this in a very convoluted way: SERVER (nextjs):
Copy code
const { status, features } = await getExperimentsFromCache();
  if (status !== 200) return { props: {} };

  // create a GrowthBook instance
  const growthBook = new GrowthBook({ features, attributes: userAttributes });

  // fetch all features and assign the user based on targeting Attributes
  Object.keys(growthBook.getFeatures()).map((feature) =>
    growthBook.getFeatureValue(feature, 'A')
  );

  // get all assignment results (we need it to grab the variation id)
  const allResults = growthBook.getAllResults();

  // construct our experiments object which contains keys, values and the GrowthBook variation id
  const experiments: Experiments = {};
  allResults.forEach((feature, name) => {
    if (feature?.result.inExperiment) {
      experiments[name] = {
        variant: feature.result.value,
        variationId: feature.result.variationId
      };
    }
  });

  return {
    props: { experiments }
  };
... and then the CLIENT (nextjs)
Copy code
const trackViewedExperiment = (name: string, variationId: number) => {
  const eventObj = {
    event: 'growthbook_experiment_viewed',
    version: '1-0-0',
    owner: 'com.acme.experimentation',
    data: {
      experiment_viewed: name,
      variant_viewed: variationId
    }
  };
  // Send the viewed experiment to the data lake via snowplow (just logging here for clarity)
  console.log(`datalayer.push(${JSON.stringify(eventObj, null, 2)})`);
};
... is there any "easier" way to doing this?
👀 1
Hello GrowthBook team - are there any tips for this problem? Did any other user encounter this / solve this?
16 Views