Expand my Community achievements bar.

Join us January 15th for an AMA with Champion Achaia Walton, who will be talking about her article on Event-Based Reporting and Measuring Content Groups!
SOLVED

Rule not firing onchange event

Avatar

Level 6

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;
});
)};

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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;".

View solution in original post

8 Replies

Avatar

Correct answer by
Community Advisor

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;".

Avatar

Level 6

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.

Avatar

Community Advisor

@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.

Avatar

Level 6

@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.

Avatar

Community Advisor
@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.

Avatar

Level 6

@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.

Avatar

Level 6

@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.

Avatar

Level 10
Do any of the answers below answer your initial question? If so, can you select one of them as the correct answer? If none of the answers already provided answer your question, can you provide additional information to better help the community solve your question?