Listening to pushes to Google Data Layer

Kai Omonijo

The Data Layer is Google’s solution to an event driven data layer. The JavaScript Array serves as a queue to helpfully avoid race conditions.

Sometimes it is useful to listen in to any items that have been pushed into the queue so you can forward those on to another system, without having to use the official google extensions.

You can push a listener on to the data layer. Google will process your callback function whenever a message is pushed onto the data layer.

Unlike Adobe’s Data Layer you can obtain the full data layer state for historic pushes alongside the current message pushed into the queue.

import 'node_modules/data-layer-helper/dist/data-layer-helper';

const = listener(model, message)  =>{
  console.log(model, message);
}

const helper = new DataLayerHelper(dataLayer, {
  listener: listener,
  listenToPast: true,
});

Full documentation is available in the data-layer-helper repository.