Skip to content

Commit

Permalink
Add useEffect to GrowthBookTracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorn committed May 3, 2024
1 parent 2b00970 commit 74d7113
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions next-js/src/lib/GrowthBookTracking.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";
import { TrackingCallback, TrackingData } from "@growthbook/growthbook";
import { useEffect } from "react";

export const onExperimentView: TrackingCallback = (experiment, result) => {
// TODO: track with Google Analytics, Segment, etc.
Expand All @@ -11,9 +12,11 @@ export const onExperimentView: TrackingCallback = (experiment, result) => {

// Helper component to track experiment views from server components
export function GrowthBookTracking({ data }: { data: TrackingData[] }) {
data.forEach(({ experiment, result }) => {
onExperimentView(experiment, result);
});
useEffect(() => {
data.forEach(({ experiment, result }) => {
onExperimentView(experiment, result);
});
}, [data]);

return null;
}

0 comments on commit 74d7113

Please sign in to comment.