Hello, I have an experiment set up, and it is corr...
# ask-questions
r
Hello, I have an experiment set up, and it is correctly putting people in different sides of the experiment as I have been able to test and confirm. However, no data is actually recording in GB for the experiment. The error now says "No data, make sure your experiment is tracking properly" Here is the code for my experiment
Copy code
let visitor_id = localStorage.getItem("visitor_id");
        console.log(this.context);
        if (!visitor_id) {
            visitor_id = Math.random().toString(36).substring(2, 15); 
            console.log('visitor_id didnt exist', visitor_id);
            // or any other method to generate a random ID
            localStorage.setItem("visitor_id", visitor_id);
        }

        let pageType = this.context.page_type;
        async function initializeGrowthBook() {
            try {
            // Manually fetch features
            const response = await fetch(`<https://cdn.growthbook.io/api/features/sdk-z2eQdvHSoRomlSFH>`);
            const { features } = await response.json();
            
            // Initialize GrowthBook with the features
            const gb = new GrowthBook({
                enableDevMode: true,
                features,
                apiHost: "<https://cdn.growthbook.io>",
                clientKey: "sdk-z2eQdvHSoRomlSFH",
                subscribeToChanges: true,
                attributes: {
                id: visitor_id, // Use the anonymous ID for user identification
                },
                trackingCallback: (experiment, result) => {
                console.log("Experiment Viewed", {
                    experimentId: experiment.key,
                    variationId: result.key,
                });
                },
            });
                    
            let experiment_set = localStorage.getItem("experiment_key");
            if (!experiment_set) {
                let experimentKey = gb.isOn("groove-mini-cart");
                localStorage.setItem("experiment_key", experimentKey);
                if(pageType === 'product') {
                    window.location.reload();
                }
            }
            if (gb.isOn("groove-mini-cart")) {
                console.log("Feature enabled!");
                $body.addClass('in-experiment');
                $dropdown.addClass('cart-dropdown-gb');
                cartPreviewGb(secureBaseUrl, cartId);
                
                
            } else {
                console.log("Feature disabled");
                cartPreview(secureBaseUrl, cartId);
            }
            
            return gb; // Return the instance if needed elsewhere
            } catch (error) {
            console.error("Error initializing GrowthBook:", error);
            }
        }
        
        initializeGrowthBook().then(gb => {

        });
s
The trouble is in your
trackingCallback
. This function is what sends the experiment exposure data to your db, but right now it's just logging to the console.
Copy code
console.log("Experiment Viewed", {
                    experimentId: experiment.key,
                    variationId: result.key,
                });
We need to ensure that the data is getting to the right place. Which event tracker/db are your using (e.g., like GA4/BigQuery)?
r
@strong-mouse-55694 BigQuery
s
Thanks for the reply. I imagine you're using GTM/GA4 for event tracking. Is that right? With JS SDK, you can use our
thirdPartyTrackingPlugin
to automatically track your experiment exposure data: https://docs.growthbook.io/lib/js#third-party-tracking
r
@strong-mouse-55694 I tried to implement that and it still isn't sending the data, what's missing.
Copy code
// Initialize GrowthBook with the features
      const pluginOptions = {
        // By default, it will attempt to send to all 3 of these
        trackers: ["segment", "gtag", "gtm"],
        // Additional custom tracking callback
        additionalCallback: (experiment, result) => {
          console.log("Experiment Callback", {
            experimentId: experiment.key,
            variationId: result.key,
          })
        }
      }
      const gb = new GrowthBook({
        enableDevMode: true,
        features,
        apiHost: "<https://cdn.growthbook.io>",
        clientKey: "sdk-z2eQdvHSoRomlSFH",
        subscribeToChanges: true,
        plugins: [
          thirdPartyTrackingPlugin(pluginOptions),
        ],
        attributes: {
          id: visitor_id, // Use the anonymous ID for user identification
        },
        trackingCallback: function(experiment, result) {
        }
      });
s
Is the page live where this is implemented? Are you using GA4 or GTM with BigQuery?
r
@strong-mouse-55694 Yes the page is live
s
Thanks for sharing. I think there could be a few things. First, let's update your initialization code:
Copy code
const pluginOptions = {
  // By default, it will attempt to send to all 3 of these
  trackers: ["gtag"],
}
Copy code
const gb = new GrowthBook({
        enableDevMode: true,
        clientKey: "sdk-z2eQdvHSoRomlSFH",
        plugins: [
          thirdPartyTrackingPlugin(pluginOptions),
        ],
        attributes: {
          id: visitor_id, // Use the anonymous ID for user identification
        },
      });
r
the experiment doesnt work without "features"
FYI
b
So i updated my code to this.
Copy code
async function initializeGrowthBook() {
    try {
      // Manually fetch features
      const response = await fetch(`<https://cdn.growthbook.io/api/features/sdk-z2eQdvHSoRomlSFH>`);
      const { features } = await response.json();
      
      // Initialize GrowthBook with the features
      const pluginOptions = {
        trackers: ["gtag"],
      }
      const gb = new GrowthBook({
        enableDevMode: true,
        features,
        apiHost: "<https://cdn.growthbook.io>",
        clientKey: "sdk-z2eQdvHSoRomlSFH",
        plugins: [
          thirdPartyTrackingPlugin(pluginOptions),
        ],
        attributes: {
          id: visitor_id, // Use the anonymous ID for user identification
        }
      });
@strong-mouse-55694
s
On your site, it looks like you're using the script tag and then loading this additional script in your site's JS, which didn't look like it updated but that could be caching. I think there is likely some conflicts between the two. I'd try a more minimal approach, using the script like you have, along with the code snippet we provide for using feature flags in JS (see below or here).
Copy code
<script>
// Wait for the GrowthBook SDK to load before running
window.growthbook_queue = window.growthbook_queue || [];
window.growthbook_queue.push((gb) => {
  // Function that uses feature flags to make changes to the page
  const applyFeatureFlags = () => {
    if(gb.isOn("dark-mode")) {
      document.documentElement.classList.add("dark");
    } else {
      document.documentElement.classList.remove("dark");
    }
  }

  // Call your function initially plus whenever new data is received
  applyFeatureFlags();
  document.addEventListener("growthbookdata", applyFeatureFlags)
});
</script>
b
that would be a pretty big overhaul of my current implementation through the theme files I'm working inside
In fact, it isn't functional because all of the code I have creating a different pullout cart drawer is inside of our theme files, so the one in our script manager is the one that has to be removed
Here is the full implementation
Copy code
import { GrowthBook } from "@growthbook/growthbook";
import { thirdPartyTrackingPlugin } from "@growthbook/growthbook/plugins";

... 

 let visitor_id = localStorage.getItem("visitor_id");
        if (!visitor_id) {
        visitor_id = Math.random().toString(36).substring(2, 15); 
        // or any other method to generate a random ID
        localStorage.setItem("visitor_id", visitor_id);

... 

let pageType = this.context.page_type;
async function initializeGrowthBook() {
    try {
      // Manually fetch features
      const response = await fetch(`<https://cdn.growthbook.io/api/features/sdk-z2eQdvHSoRomlSFH>`);
      const { features } = await response.json();
      
      // Initialize GrowthBook with the features
      const pluginOptions = {
        trackers: ["gtag"],
      }
      const gb = new GrowthBook({
        enableDevMode: true,
        features,
        apiHost: "<https://cdn.growthbook.io>",
        clientKey: "sdk-z2eQdvHSoRomlSFH",
        plugins: [
          thirdPartyTrackingPlugin(pluginOptions),
        ],
        attributes: {
          id: visitor_id, // Use the anonymous ID for user identification
        }
      });
      
      
      let experiment_set = localStorage.getItem("experiment_key");
      if (!experiment_set) {
        let experimentKey = gb.isOn("groove-mini-cart");
        localStorage.setItem("experiment_key", experimentKey);
        if(pageType === 'product') {
            window.location.reload();
        }
    }


      if (gb.isOn("groove-mini-cart")) {
        console.log("Feature enabled!");
        $body.addClass('in-experiment');
        $dropdown.addClass('cart-dropdown-gb');
        cartPreviewGb(secureBaseUrl, cartId);
        
        
      } else {
        console.log("Feature disabled");
        cartPreview(secureBaseUrl, cartId);
      }
      
      return gb; // Return the instance if needed elsewhere
    } catch (error) {
      console.error("Error initializing GrowthBook:", error);
    }
  }
  
  // Call the async function
  initializeGrowthBook().then(gb => {
Copy code
});
my next step i am considering is setting up the callback function to send data directly to the dataLayer, instead of the plugin. Though the documentation I was looking through doesn't make that entirely clear either
@strong-mouse-55694
s
It looks like it's working. I see that the experiment_viewed data is already in GTM:
b
@strong-mouse-55694 thats what I thought too, but the experiment isnt seeing any data.
I think i found the issue tho. The experiment id in that event_callback is groove-mini-cart-exp, the id i want to send it to is groove-mini-cart-test
I'm not seeing where to change that
s
This isn't something that should need fixing, because the tracking callback should fire with the appropriate ID automatically. I'm wondering how this happened? Do you have a sense? If you're looking to bring this data into GrowthBook as an experiment, there is a workaround. Instead of creating a new experiment, you can import one. Then, use the experiment ID you want.
b
@strong-mouse-55694 it looks like the other experiment was also linked to the feature flag set up for this. So it was looking at the wrong experiment and never seeing the second experiment (which ends in -test) I started a new phase on the -test experiment and removed everything else frp, the attached feature flag. Didn't think this would be a disruptions because the experiment was still running, but hopefully we see some data coming in now
Screenshot 2025-04-01 at 9.50.30 AM.png
do you know how this works with regards to Big Query?
should this be correctly sending data to BQ?
s
You need to configure this in GTM. It should be set up with the custom event and then use GA4, which will send the data to BigQuery. We have a guide here: https://docs.growthbook.io/guide/google-tag-manager-and-growthbook