Expand my Community achievements bar.

SOLVED

Check Direct Call occurred in Custom Event

Avatar

Level 2

For example,  say I have a rule, and in that rule, I want to check that both:

1.  The Data Layer has completed, which I can tell by a direct call

2.  The windowLoaded event has been fired by the browser.

 

I though of doing in a custom event, and I understand how to check for windowLoaded, but how do I check if the direct call happened too?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Have one Rule with 2 Events:

  1. Window Loaded event.
  2. Direct Call event.

Include one custom code Action:

// get the list of previously detected event types
let windowLoadedAndDirectCall = _satellite.getVar("window-loaded-and-direct-call");

// transform the list of previously detected event types into an array
// if there have not been any previously detected event types, then create an empty array
windowLoadedAndDirectCall = windowLoadedAndDirectCall ? windowLoadedAndDirectCall.split(',') : [];

// get the current triggered event type: "window-loaded" or "direct-call"
let eventType = event.$type;

// append the current triggered event type to the list of previously detected event types
windowLoadedAndDirectCall.push(eventType);

// remember the list of previously detected event types
_satellite.setVar("window-loaded-and-direct-call", windowLoadedAndDirectCall.join(','));

// check if both event types have been triggered
if (windowLoadedAndDirectCall.indexOf("window-loaded") > -1 && windowLoadedAndDirectCall.indexOf("direct-call") > -1) {
  // send your Custom Event here
}

FYI:

  • Each time this Rule runs (triggered by either the Direct Call or Window Loaded), the action will remember the triggering event and save to a temporary data element, "window-loaded-and-direct-call". You don't need to create this data element separately in the Launch UI.
  • Test this properly! I've written the code off the top of my head, so there might be some bugs that I haven't accounted for.

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Have one Rule with 2 Events:

  1. Window Loaded event.
  2. Direct Call event.

Include one custom code Action:

// get the list of previously detected event types
let windowLoadedAndDirectCall = _satellite.getVar("window-loaded-and-direct-call");

// transform the list of previously detected event types into an array
// if there have not been any previously detected event types, then create an empty array
windowLoadedAndDirectCall = windowLoadedAndDirectCall ? windowLoadedAndDirectCall.split(',') : [];

// get the current triggered event type: "window-loaded" or "direct-call"
let eventType = event.$type;

// append the current triggered event type to the list of previously detected event types
windowLoadedAndDirectCall.push(eventType);

// remember the list of previously detected event types
_satellite.setVar("window-loaded-and-direct-call", windowLoadedAndDirectCall.join(','));

// check if both event types have been triggered
if (windowLoadedAndDirectCall.indexOf("window-loaded") > -1 && windowLoadedAndDirectCall.indexOf("direct-call") > -1) {
  // send your Custom Event here
}

FYI:

  • Each time this Rule runs (triggered by either the Direct Call or Window Loaded), the action will remember the triggering event and save to a temporary data element, "window-loaded-and-direct-call". You don't need to create this data element separately in the Launch UI.
  • Test this properly! I've written the code off the top of my head, so there might be some bugs that I haven't accounted for.

Avatar

Level 2

Thanks for the code - I'm a little confused at what I put here?:

// get the list of previously detected event types
let windowLoadedAndDirectCall = _satellite.getVar("window-loaded-and-direct-call");

What goes in the Data Element ("window-loaded-and-direct-call")

that indicates an event has occurred already?  - I think that's my challenge ...is I can't tell if a direct call occurred from within a rule, so I can't check it for its existence? 

Avatar

Community Advisor

The code that populates this "window-loaded-and-direct-call" Data Element is in the next few lines:

 

// get the current triggered event type: "window-loaded" or "direct-call"
let eventType = event.$type;

// append the current triggered event type to the list of previously detected event types
windowLoadedAndDirectCall.push(eventType);

// remember the list of previously detected event types
_satellite.setVar("window-loaded-and-direct-call", windowLoadedAndDirectCall.join(','));

 

Notice the use of "event.$type". This is a property in the "event" object that all Launch events return automatically.

  • Initially, when the Launch library is loaded, this Data Element will not have any value.
  • Then when the Window Loaded event gets triggered, event.$type = "window-loaded", and this gets set in the Data Element. The "if" statement then runs, but "direct-call" isn't found in the Data Element, so it doesn't run its block.
  • Then when the Direct Call event gets triggered, event.$type = "direct-call", and this gets set in the Data Element. The "if" statement then runs, and now both "window-loaded" and "direct-call" are found in the Data Element, so it runs its block.

Hope that helps!

(BTW I updated my original answer. I realised that I had forgotten to "split" the Data Element's value after reading it, so that its comma-separated values become an array properly.)