https://www.growthbook.io/ logo
b

broad-continent-77439

04/20/2022, 1:14 PM
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

future-teacher-7046

04/20/2022, 1:30 PM
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

broad-continent-77439

04/20/2022, 2:02 PM
Thanks @future-teacher-7046 for your help. I'll give it a try 👋
2 Views