简体   繁体   中英

GTM issue with double event created instead of dl push

I use this code in GTM , that retrieves a number from another domain and stores it on a dataLayer value, with a dL.push. I want the variable to be available on a specific event.

(function () 
{
     var callbackMethodName = "adnscallback";
     window[callbackMethodName] = function(data) {
        (function() {
           dataLayer.push({
              'event': 'ga_new',
              "id2": data.uid
           });
        })();

     }
     var sc = document.createElement("script");
     sc.id = "script_" + callbackMethodName;
     sc.src = "https://example.site.com/getnumber?callback=" + callbackMethodName;
     document.body.appendChild(sc);
})();

But I already create an event called ga_new (hardcoded, that creates all sort of values on the page) and when this codes run, it creates another event called ga_new, instead of pushing the values on the existing event. This creates issues with the tags as they are double firing.

What iam doing wrong and the push, instead of appending the data, it creates the event again?

You can not "update" a custom event. The "event" keyword is a signal to GTM that it needs to update its internal data structure with new values.

The datalayer variable in the page is an array of objects, the internal datastructure is a single object into which new values are merged, and existing values for the same keys are overwritten. GTM overwrites the native implementation of the JS "push" method with its own code that watches for the "event" keyword. So every time it encounters the "event" key changes are triggered.

You either need a different name for the event, or you change the trigger so that it only fires when both the "ga_new" event are set and the data.uid field is set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM