Can anyone assist with the process for GTM install...
# ask-questions
g
Can anyone assist with the process for GTM installation? Again from a non-technical background which is likely why I’m hung up. I’m following this documentation: https://docs.growthbook.io/guide/google-tag-manager-and-growthbook Where I’m hung up is what the SDK initialization code should look like for me using GA4 (specifically regarding user IDs). I’m not confident in my ability to merge code and while thorough, this resource isn’t catering much to someone like me. 1. I know the default custom HTML for my tag will be the following: <script> (function() { // Wait for the SDK to load before starting GrowthBook if (window.growthbook) { startGrowthbook(); } else { document.querySelector(“#growthbook-sdk”).addEventListener(“load”, startGrowthbook); } function startGrowthbook() { if (!window.growthbook) return; var gb = new growthbook.GrowthBook({ apiHost: “https://cdn.growthbook.io”, clientKey: “sdk-abcd1234", // TODO: Add decryptionKey if using encryption attributes: { id: “u1234” // TODO: Read user/device id from a cookie/datalayer }, trackingCallback: function(experiment, result) { // TODO: track experiment impression } }); // TODO: Instrument DOM with AB test logic gb.loadFeatures().then(function() { // NOTE: We may wish to remove
gb.loadFeatures()
and instead manually implement experiment logic }); } })(); </script> clientKey will be updated with my own, apiHost already matches my own When referencing the user id for GA4, I apparently need to “nest the SDK instantiation inside your gtag callback function”. (second screenshot and code posted below): gtag(‘get’, ‘G-XXXXXX’, ‘client_id’, function(cid) { var gb = new growthbook.GrowthBook({ apiHost: “https://cdn.growthbook.io”, clientKey: “sdk-abcd1234", attributes: { id: cid }, // etc... }); // TODO: Instrument DOM with AB test logic }); So I am understanding this as I need to alter the first code based on the fact that I’m using GA4, and of course also switch out my measurement ID in addition to the clientKey. Is that correct? Could someone assist me on what the final code would look like if it’s just one snippet for my custom HTML tag in GTM? Thanks a ton! p.s. bonus question: What’s the ideal trigger to use? I have window loaded selected currently since that should in theory ensure GA4 fires first on initialization.
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
image.png
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!