1

I'm currently learning how to use and apply a dataLayer to a website. From everything that I've read, best practice is to use .push functionality when updating your dataLayer.

I know that .push will not usually update an object's value that is inside of an array when you use the .push functionality, it will simply append another object with the same key and new value to the end of it. Google Tag Manager has patched the .push functionality to allow it to see if the key exists and change it's value, or if it doesn't exist, add a new object to the array.

How does GTM accomplish this, and can I write code to do this myself? Secondary question, as I am using Adobe Analytics on this website, does AA/Launch have this same functionality? I may want to update something in the dataLayer and won't know it's index within the array.

Thanks!

  • Nothing complicated, it just overwrites the `push` method on the array with its own implementation – Lennholm Oct 21 '19 at 16:26
  • How would I go about writing a function to do this myself though? Does Adobe Launch carry that same ability to do it as GTM does? – Randy Martin Oct 21 '19 at 20:27

1 Answers1

1

Ended up finding the function that GTM uses to override the push function. Posting it here in case someone has the question in the future. Now to see if I can understand it lol

a.push = function() {
            var d;
            if (0 < Kc.SANDBOXED_JS_SEMAPHORE) {
                d = [];
                for (var e = 0; e < arguments.length; e++)
                    d[e] = new rg(arguments[e])
            } else
                d = [].slice.call(arguments, 0);
            var g = c.apply(a, d);
            wg.push.apply(wg, d);
            if (300 < this.length)
                for (Xc("GTM", 4); 300 < this.length; )
                    this.shift();
            var h = "boolean" !== typeof g || g;
            return Cg() && h
        }