Can anyone assist with the process for GTM installation? Again from a non-technical background which...
g

Giorgio Roosta

over 1 year ago
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.