https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
m

mysterious-hospital-86275

04/03/2024, 3:32 PM
Hi team, We integrated GrowthBook SDK using GTM tag and used the snippet provided in documentation. After integration we noticed a huge spike in organic search traffic in GA4(Screen Attached). We paused the Growthbook tag and spike got dissolved. Just to verify we enabled the same tag and noticed that spike is occurring again. Can anyone help and explain why is this happening in our case? I have also shared the GTM tag snippet below.
Copy code
<script id="growthbook-sdk" src="<https://cdn.jsdelivr.net/npm/@growthbook/growthbook/dist/bundles/index.min.js>" defer></script>
<script>
	

(function() {

    window.getUUID = function() {
        var COOKIE_NAME = "gbuuid";
        var COOKIE_DAYS = 3 ; // 400 days is the max cookie duration for chrome

        // use the browsers crypto.randomUUID if set
        function generateUUID() {
            if (window.crypto != undefined && window.crypto.randomUUID != undefined && typeof window.crypto.randomUUID == 'function') {
                return window.crypto.randomUUID();
            } else {
                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                    var r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0,
                        v = c === 'x' ? r : (r & 0x3 | 0x8);
                    return v.toString(16);
                });
            }
        }

        function setCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
        expires = '; expires=' + date.toGMTString();
    } else {
        expires = '';
    }
    var cookieName = '' + name + '';
    var cookieStr = cookieName + '=' + value + expires + ';' + 'domain=' + '.' + removeDomainPrefix(
        window.location.hostname
   ) + ';path=/';
    typeof window !== 'undefined' ?
        (document.cookie = cookieStr) :
        '';
}


        // get the existing UUID from cookie if set, otherwise create one and store it in the cookie
        if (window.readCookie(COOKIE_NAME)) return window.readCookie(COOKIE_NAME);

        var uuid = generateUUID();
        setCookie(COOKIE_NAME, uuid,COOKIE_DAYS);
        return uuid;
    }

    
    window.getCurrentEpochTime = function() {
        // Create a new Date object and use the getTime() method to get the current timestamp in milliseconds.
        var currentTimeMillis = new Date().getTime();

        // Convert the timestamp from milliseconds to seconds by dividing it by 1000.
        var currentEpochTime = Math.floor(currentTimeMillis / 1000);

        return currentEpochTime;
    }
    window.readCookie = function(cookieName) {
        var name = cookieName + "=";
        var decodedCookie = decodeURIComponent(document.cookie);
        var cookieArray = decodedCookie.split(';');

        for (var i = 0; i < cookieArray.length; i++) {
            var cookie = cookieArray[i];
            while (cookie.charAt(0) === ' ') {
                cookie = cookie.substring(1);
            }
            if (cookie.indexOf(name) === 0) {
                return cookie.substring(name.length, cookie.length);
            }
        }

        // Return false if the cookie is not found
        return false;
    }

    // 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;
        console.log(window.growthbook, "window.growthbook")

        var exp = dataLayer[0].all_experiment_objects[0]


        window.gbuuid = window.getUUID();
        window.gb = new growthbook.GrowthBook({
            apiHost: "<https://cdn.growthbook.io>",
            clientKey: "sdk-YSiLEOQJVJKOggX",
            // TODO: Add decryptionKey if using encryption
            attributes: {
                id: window.gbuuid,
                expValue: dataLayer[0].optimize_all_experiment_experiment_id
                //add any other attributes here
            },
            debug: true,
            enableDebugMode: true,

            enableDevMode: true,
            subscribeToChanges: true,
            trackingCallback: function(experiment, result) {
                // track with GA4, for example:
 //               console.log("experiment-result", experiment, result)
                dataLayer.push({
                    event: 'experiment_viewed',
                    type: 'experiment',
                    experiment_id: experiment.key,
                    variation_id: result.variationId,
                    user_id: window.gbuuid,
                })


            }
        });
        window.gb.loadFeatures().then(function() {
            // if you want to do anything after GB loads
            // Object.keys(window.gb.getFeatures()).map(function(feature) {
            //     console.log(feature + " variation assigend feature call -> " + window.gb.getFeatureValue(feature, "fallback"))
 
                
            // })
          var exp = dataLayer[0].all_experiment_objects;
                if (exp && exp.length) {
                    exp.map(function(ele) {
                      var featureValue = window.gb.getFeatureValue(ele.google_exp_id.toLowerCase(), "fallback");
                       // console.log(ele.google_exp_id + " variation assigend exp call -> " + featureValue)
                       
                      if(featureValue !== "fallback" && featureValue !== -1){
                        dataLayer.push({
                            event: 'experiment_viewed',
                            type: 'experiment',
                            experiment_id: ele.name,
                            variation_id: featureValue,
                            user_id: window.gbuuid,
                        })
                      }

                    })

                }
        });
    }

})();

</script>
Hi any update here?
f

fresh-football-47124

04/04/2024, 4:58 AM
Well, The bottom end of your script doesn't need the dataLayer.push
but those are all custom event types
m

mysterious-hospital-86275

04/04/2024, 5:01 AM
For business logic we have implemented force bucketing. This way we can push data for experiment viewed into GA4.
It could be the reason for spike in organic search spike?
3 Views