This message was deleted.
# ask-questions
w
This message was deleted.
r
Hi Giorgio, we can definitely help with this! Your request is on my list for Wednesday and I'll follow up as soon as I can.
g
Thank you August! I’m making good progress on other elements of the setup this week 🙂
Hi again @brief-honey-45610, I was wondering if you had a chance to look at this request yet? Thank you!
f
hi Giorgio
can you share the code as you have it now from GTM?
g
Hey Graham, I see you got my message! Yes one moment
The SDK is simple enough:
However I haven’t added the second tag to our unpublished GTM container because I was unsure how to combine the Javascript as we’re using GA4. In this thread above, I pasted two screenshots of the code from Growthbook’s resources, but was not confident on how I should be combining them. I tried using AI to help with that. As a side note, it was incredibly useful for me navigating bigquery and building custom metrics such as revenue, AOV, and conversion rate, but I wasn’t getting a great answer regarding how to combine the code for the initialization tag in GTM.
f
okay
g
Would this be the correct way to adapt the initialization code since I’m using GA4 connected to bigquery? <script> (function() { // Function to start GrowthBook after SDK is loaded function startGrowthbook() { if (window.growthbook) { // Get GA4 client ID gtag(‘get’, ‘G-XXXXXXX’, ‘client_id’, function(cid) { // Initialize GrowthBook with GA4 client ID window.growthbook = new GrowthBook({ apiKey: “YOUR_API_KEY”, attributes: { id: cid }, // ... additional configuration }); // Load features and run experiments window.growthbook.loadFeatures().then(function() { // ... implement experiment logic }); }); } } // Wait for the SDK to load before starting GrowthBook if (window.growthbook) { startGrowthbook(); } else { document.querySelector(“#growthbook-sdk”).addEventListener(“load”, startGrowthbook); } })(); </script>
f
pretty close
are you looking to just use the visual editor?
in which case that should work when you replace in the SDK values and GA4 ids
g
Yes, I’m aware I’ll need to upgrade plans at that point but that’s the use-case for me at this time. It took about 8 attempts with GPT4 prompts to get that code so but I’m glad to get your validation on it.
Would you recommend “window loaded” or something else as the trigger for both GTM tags?
Once I’m up and running and have experience with the platform, I’ll be more than glad to send you feedback that I hope will be helpful for others with my background
f
that would be very helpful
what did you set for the other GrowthBook javascript in GTM?
loaded would be okay - but perhaps a bit slow, what are your options?
g
I haven’t published the container yet but i have window loaded as the trigger on that tag currently
I’ll show you my options
f
loaded would work - but may cause unnecessary flashing. The way you have it now it shouldn’t matter which is loaded first. Doing it on Page View may be faster
do you have a way to test this out or are you going to YOLO on production?
g
GA4 fires on Initialization. I could in theory use that trigger type here too and add a tag sequencing rule for to wait for the google tag to fire. But there’s no documentation around triggers in detail so I wasn’t sure the best route. I was going to go yolo, but I know that can be risky whenever javascript is involved. They don’t really have a development environment
f
I dont think it will be catastrophic - may just throw some JS errors
g
I’ll make sure to get the closest thing to all hands on deck upon publishing
So pageview triggers for both tags is your advice?
Also, should I set up a tag sequencing rule to ensure the this tag fires after the “Growthbook SDK” tag?
I think GB has a ton of potential to capture the Google optimize Users as you continue to dumb down these instructions for noobs like me🤓
f
ideaily, yes, you want the script import to go first
1
g
Thanks for your help Graham. I’ll coordinate with my backup to publish this tomorrow.
actually as I’ve looked closer at that code, I realize it has “apiKey” which isn’t referenced in the setup documentation, it also left out apiHost and clientKey. So I think GPT hallucinated and combined elements and now I’m back to being unsure about installing it.
I also realize there was no section regarding the trackingCallback. Here’s my 10th attempt. Would you take a look when you can @fresh-football-47124? Thank you in advance:
f
so there are two events that have to happen for GrowthBook as configured to work correctly - GA4 has to load (to get the client ID) and GrowthBook has to work so that the new growthbook line works
this code assumes that the Google/gtag will be there - which might be true, but I haven’t tested that
the trackingCallback looks correct
g
if you have any suggestion, I’m happy to implement whatever code you provide. Again, I’m way outside of my technical knowledge here and relying heavily on AI to get this far. Here’s the raw code: <script> (function() { // Function that starts GrowthBook with the GA4 client ID function startGrowthbookWithClientId(clientId) { var gb = new growthbook.GrowthBook({ apiHost: “https://cdn.growthbook.io”, clientKey: “XXX”, attributes: { id: clientId }, trackingCallback: function(experiment, result) { // Tracking experiment impressions in GA4 gtag(“event”, “experiment_viewed”, { event_category: “experiment”, experiment_id: experiment.key, variation_id: result.variationId, // Add other parameters as needed }); } }); // Instrument DOM with AB test logic, if needed gb.loadFeatures().then(function() { // Implementation details for feature flagging }); } // Check if GrowthBook SDK is loaded, then retrieve GA4 client ID and start GrowthBook if(window.growthbook) { // If GrowthBook is already defined, retrieve the GA4 client ID and start GrowthBook gtag(‘get’, ‘G-XXX’, ‘client_id’, function(cid) { startGrowthbookWithClientId(cid); }); } else { // If GrowthBook is not defined, wait for it to load then retrieve the GA4 client ID and start GrowthBook document.querySelector(“#growthbook-sdk”).addEventListener(“load”, function() { gtag(‘get’, ‘G-XXX’, ‘client_id’, function(cid) { startGrowthbookWithClientId(cid); }); }); } })(); </script>
f
taking a look
that code looks good to me
g
Thank you!
150 Views