GTAG/GA4 Hit "empty": I set up using the recommen...
# ask-questions
b
GTAG/GA4 Hit "empty": I set up using the recommended code here, experiment is working, however when looking at this in GTM looking at the actual "HIT" the dataLayer for the experiment_viewed event is empty. We verified the set up with GA4/BQ and GB is all lined up. If you look here: www.lapolicegear.com and check the dataLayer in the console you will see it is not empty. So the issue is clearly that the data is being sent to GTM before the dataLayer actually contains the correct info. Setup looks like this:
Copy code
onReady() {
        const { cartId, secureBaseUrl } = this.context;
        let $body = $('body');
        let $dropdown = $('#cart-preview-dropdown');

        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);
    }
  }
I'm seeing two events in GTM. 39 and 38. This is for 39.
hit gets sent with nothing in the dataLayer
w
hey! maybe the plugin handles this automatically but something I noticed is that you didn't code any
trackingCallback
when initializing Growthbook. I think you need to pass the callback function for experiment exposures and in there send the event details to your datalayer
b
i believe the plug is supposed to handle it automatically. When GB support helped me set this up we removed the trackingCallback
w
ah I see, that's weird because the way the documentation is written it looks like it is expecting ithttps://docs.growthbook.io/guide/GA4-google-analytics#sdk-integration-for-ga4 (in the yellow note)
b
checking via GB debugger the tracking callback is working fine
the issue is that GTM isnt firing a hit for the dataLayer
s
With GTM, you'll want to add it to the list of trackers for plugin options. See the docs for an example: https://docs.growthbook.io/lib/js#third-party-tracking
b
last time i asked about this the code i was given specific to use "gtag" but ill add "Gtm" and see if that fixes it
s
Right. From the discussion, it seemed like you were using GA4, which is why
gtag
was suggested. But if you're using
gtm
, then you'll want to use that instead. Sorry for any confusion.
b
its sending from GTM to GA4
i thought that was required, i wasn't aware it could go straight to GA4
ill check back in if we hit another snag
( i am primarily a javascript developer, I do not know much about GTM/GA4, etc)
s
It's a bit of a minefield, tbh. It can work via GA4 directly—however, you'll need to ensure ga4 is firing on the site. Then, for GA4 or GTM, you'll need to ensure that you have BigQuery, too, which is the database.
1