broad-continent-77439
04/20/2022, 1:14 PM@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
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(() => {});
}, []);
future-teacher-7046
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(...)
})
}, []);
}
broad-continent-77439
04/20/2022, 2:02 PM