Listener on a datalayer events

1.3k Views Asked by At

I am trying to add a trigger on adobe launch to fire a rule when the site has a specific data layer event.

I did it in the past to fire a rule when someone clicked three times on the site:

window.addEventListener('click', function (event) { 
  If (event.detail === 3) {
     trigger();
});

, but I'm lost in archiving it with data layer events. I will intend to create a listener to fire the rule on the event "show" from this data layer object:

window.adobeDataLayer = window.adobeDataLayer || [];
window.adobeDataLayer.push({
    "event": "show",
    "eventInfo": {
        "reference": "component.accordion-1-item-2"
    },
    "component": {
        "accordion-1": {
            "shownItems": [
                "component.accordion-1-item-1",
                "component.accordion-1-item-2"
            ]
        }
    }
});
1

There are 1 best solutions below

2
On BEST ANSWER

First of all, writing custom listeners for DL is very rarely a good idea. Because it requires quite solid knowledge of nuances in TMSes operations.

I would suggest using the extension. That would likely be either ACDL (Adobe Client Data Layer) or DM (Datalayer Manager). I suggest ACDL as native to Launch. In fact, I think you're using ACDL since what you have is ACDL's default name for the DL.

So, let's do exactly what you want without having to touch the DL listener. Here we're using ACDL and making a new trigger: enter image description here

Note that we have it firing on all events.

Now we do a simple hack in a condition (Core -> Custom Code), having a regex there exactly as you want it:

return /myevent/i.test(event.message.event);

Finally, testing our work:

enter image description here

If you still want to have your DL listener (you should not), you can do that, certainly. TMSes and extensions do that through overriding the DL's push method, here:

y.push = function(t) {
  var e = arguments
    , r = arguments;
  if (Object.keys(e).forEach((function(t) {
      var o = a(e[t]);
      switch (o.valid || (p(o),
      delete r[t]),
      o.type) {
      case s.itemType.DATA:
      case s.itemType.EVENT:
          n(o);
          break;
      case s.itemType.FCTN:
          delete r[t],
          n(o);
          break;
      case s.itemType.LISTENER_ON:
      case s.itemType.LISTENER_OFF:
          delete r[t]
      }
  }
  )),
  r[0])
      return Array.prototype.push.apply(this, r)
}

Feel free to add your overridal on top of that.