Hello everyone :wave: Appreciate a bit of help. I ...
# ask-questions
b
Hello everyone 👋 Appreciate a bit of help. I am currently using
@growthbook/growthbook-react
with Next.js; current issue is that 👉 How can I get the experiment inside
getServerSideProps
func and track it to segment too (
window.analytics
) ? Obv. I cannot use
useExperiment
Current setup is as easy:
Copy code
useEffect(() => {
    // Load feature definitions from GrowthBook API
    loadGrowthbookDefinitions()
      .then((res) => {
        // Set features to the provider
      growthbook.setFeatures(res?.data?.features);

        // Set user attributes for targeting (from segment analytics)
        const segmentId = getCookie("ajs_anonymous_id");
        growthbook.setAttributes({
          id: segmentId,
        });
      })
      .catch(() => {});
  }, []);
f
I haven't tested this specifically, but I think something like this would work:
Copy code
export function getServerSideProps() {
  // Need a separate GrowthBook instance for server-side code
  const growthbook = new GrowthBook({
    features: ...,
    attributes: ...
  });

  // Get your features
  const feature = growthbook.isOn("my-feature");

  // Return feature plus tracking data
  return {
    props: {
      feature,
      trackingData: Array.from(growthbook.getAllResults().values())
    }
  } 
}

// In your page, track to segment in a useEffect hook
export function MyPage({feature, trackingData}) {
  useEffect(() => {
    trackingData.forEach(({experiment, result}) => {
      window.analytics.track(...)
    })
  }, []);
}
b
Thanks @future-teacher-7046 for your help. I'll give it a try 👋