Rule not firing onchange event

jezwn

04-10-2020

I've added the event Core - Data Element Change and in custom code consoling it out. The data element name is 'form Abandonment' and js object is digitalData.form.formAbandonment
if(digitalData.form.formAbandonment != ""){
console.log("Refresh case:"+_satellite.getVar("form Abandonment"));
return true;
} else {
console.log("Not updated");

return false;
}

I tried consoling out the same value in datalayer and I'm getting the right values for both js and satellite object. The formAbandonment is set on unload event.
$(window).on( "unload", function() {
formArray['formAbandonment'] = fieldName;
});
)};

Analytics rules

Accepted Solutions (0)

Answers (1)

Answers (1)

yuhuisg

04-10-2020

What is most likely happening is that there's a "race condition" between when your digitalData.form.formAbandonment gets set vs when the browser has already unloaded and is navigating to the next page. You can verify if this is the case with the Adobe Experience Platform Debugger's Events panel.

With window's unload function, one "trick" I've used that works pretty consistently is to perform the Rule's action within a condition. Basically, run the action's custom code in a condition, and make sure the last line is "return true;".

The event is not getting triggered at all it seems to be. No traces in the debugger. Yeah I've used some condition in rules action but since the data element change is not triggering it isn't working. Also the browser unload only happens after every line inside it's executed. Even I tried with a delay after variable set in js, still the same.

@jezwna Data Element Change event might not be the best for you.

Try using a Custom Code event. In there, you can use:

window.addEventListener('beforeunload', function (event) {
  trigger(); // REQUIRED
});

Then, you can read from your data element in a condition or action.

@yuhuisg Is it a good practice to set the variables and send s.tl() from the datalayer itself, I mean inside the unload event. Tried with that and it works. Just need to make sure if it could be done.

@jezwn I personally wouldn't rely on window.onunload because there will always be race conditions between your code getting executed vs the browser navigating away. Maybe it works for you, but it might not with your users. But if you find that your method works, then go ahead, and I suggest that you document it clearly, because it's definitely out of the ordinary.

@yuhuisg Whatever written inside the unload event gets executed unless it's an async activity. But I asssume there was this race condition before, when I tried to set the js variable inside unload vs the unload event. After setting the js variable, currently I'm setting evars, props and events inside the unload event in datalayer. But this seems bit odd doing this in datalayer. That's the concern was about.
s.eVar21 = fieldName;
s.prop29 = s.eVar21;
s.events = "event3";
s.tl();
s.clearVars();


Your suggestion was to add a custom code event and trigger(), right! I'll check with that. Thanks.

@yuhuisg There's some other issue doing it this way, the one you shared and the one I was on. When I check the network requests it shows pending, none of the requests on unload are getting completed.