Expand my Community achievements bar.

SOLVED

Do Actions have a "this"?

Avatar

Level 2

Hello - 

 

Do actions have a way to access their friendly name or other properties from within the action

 

Why?:
I have a rule "Fire OrderConfirm Events"

 

That rule contains 10 actions.  

 

In each action, I'd like to call a function that checks the action reached "the end" and didn't error.  If it does error, I'd want to surface the name of the action that errored

 

So instead of:
Rule Fire OrderConfirm Events Fired - followed by

unexpected:  Undefined etc etc.

 

I can have it throw 

"Action ${action.name} Failed"  => "Action Pinterest Failed"

 

That way I can easily track errors in larger rules

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

That is correct, sorry misread. Had a look at the source code of Adobe Launch and cannot see how to do it. 

 

Having said that, we adopted the following standards when it comes to actions using custom code. We placed the code inside try/catch and we provide meaningful error logs, this way we are sure where the error originated from. 

 

If your script has multiple parts and it is "ok" for a part to fail then add try/catch around each block to isolate their errors.

 

So either:

 

try {
    //action code
} catch (e) {
    _satellite.logger.error('Failed in actionX', e)
}

//OR in case we want to isolate errors based on logical blocks

function test() {
    try {
        //logical block one
    } catch (e) {
        _satellite.logger.error('Failed in actionX to send pixel XYZ', e)
    }

    try {
        //logical block two
    } catch (e) {
        _satellite.logger.error('Failed in actionX to inject surveyXYZ', e)
    }
}

test();

View solution in original post

4 Replies

Avatar

Community Advisor

Actions (and also Events and Conditions) in a Rule have access to a "this". But the "this" refers to the object (usually the DOM object) that caused the Event to trigger. E.g. with a Click Event, the "this" refers to the DOM element that was clicked (as determined by the selector specified in the Event).

So the "this" won't work for your use-case. If you're using a Custom Code action, then you can wrap that custom code in a Promise and handle the failure on your own.

Avatar

Community Advisor

Yes I believe you can do it:

https://experienceleague.adobe.com/docs/experience-platform/tags/extension-dev/web/action-types.html...

 

Contextual event data

A second argument would then have to be passed to your module which contains the contextual information about the event that fires the rule. It may be beneficial in certain cases and can be accessed as follows:

module.exports = function(settings, event) {
  // event contains information regarding the event that fired the rule
};
Copy

The event object must contain the following properties:

Property Description
$typeA string describing the extension name and event name, joined using a period. For example, youtube.play.
$ruleAn object containing information about the currently executing rule. The object must contain the following sub-properties:
  • id: The ID of the currently executing rule.
  • name: The name of the currently executing rule.

 

That is taken from the documentation to create your own extension but believe it or not, if the action or condition or event pass the `event` argument you can access the same using this code:

 

_satellite.logger.log(event.$rule.name)

 

However some extensions do not pass event argument by default, also you need to make sure to not select the `execute globally` checkbox as it will overwrite the this and event and target 

Avatar

Community Advisor

event.$rule.name returns the name of the rule, as implied in the object path.

But the question here is how to get an action's name. That can't be done with the method that you described.

Avatar

Correct answer by
Community Advisor

That is correct, sorry misread. Had a look at the source code of Adobe Launch and cannot see how to do it. 

 

Having said that, we adopted the following standards when it comes to actions using custom code. We placed the code inside try/catch and we provide meaningful error logs, this way we are sure where the error originated from. 

 

If your script has multiple parts and it is "ok" for a part to fail then add try/catch around each block to isolate their errors.

 

So either:

 

try {
    //action code
} catch (e) {
    _satellite.logger.error('Failed in actionX', e)
}

//OR in case we want to isolate errors based on logical blocks

function test() {
    try {
        //logical block one
    } catch (e) {
        _satellite.logger.error('Failed in actionX to send pixel XYZ', e)
    }

    try {
        //logical block two
    } catch (e) {
        _satellite.logger.error('Failed in actionX to inject surveyXYZ', e)
    }
}

test();