Rules Developer Guide

mParticle’s Rules are JavaScript functions that manipulate an incoming batch object from an mParticle input. See mParticle’s Platform Guide for help setting up rules in the mParticle dashboard.

General format

Rules take the form of an AWS Lambda function, running in Node 6.10. The function takes an incoming batch argument to be manipulated and a context argument containing immutable metadata. The context argument is required, but for an mParticle rule is effectively null.

The callback takes a message and a data output. For mParticle Rules, the message will always be null. The output should either be an object in the same format as the original batch argument, or null.

exports.handler=(batch,context,callback)=>{
    //Do something with batch
    callback(null, batch); // or callback(null, null); to drop the batch completely
}

Note that standard AWS Lambda naming convention calls for (event,context,callback); here we have used (batch,context,callback) to avoid confusion with mParticle’s event object.

While all rules have the same basic syntax, there are two main use cases for rules:

  • working with the events array of a batch
  • working with any other properties of the batch.

Non-events Rules

There are two basic kinds of non-event rules:

The first is a simple ‘filter’. Based on some attribute/s of the batch, the callback either contains the original batch object, unaltered, or null, effectively dropping the batch, altogether:

exports.handler=(batch,context,callback)=>{
/* 
A support feed contains batches from internal and external users. 
We can create a rule to drop batches from internal users.
*/    
    if(batch.user_attributes.internal) {
        callback(null, null);
    }
    else{
        callback(null, batch);
    }
};

Alternatively, the callback can contain a modified version of the original batch, with some attributes added, changed or dropped.

exports.handler=(batch,context,callback)=>{
/* 
An input has firstname and lastname attributes for a user.
Our output platform expects a full name, so we can use a rule to create one.
*/    
    if(batch.user_attributes.$firstname && batch.user_attributes.$lastname) {
        var firstname = batch.user_attributes.$firstname;
        var lastname = batch.user_attributes.$lastname;
        batch.user_attributes.name = `${firstname} ${lastname}`;
    }
    callback(null, batch);

};

Event-focused rules

The batch object contains an events array, which can have any number of events. If you want to handle each event individually, you will need to define a handler function and use it to iterate over the events array.

exports.handler=(batch,context,callback)=>{
/* 
An input sends events with the event name 'Signup'. 
We can create a rule to change it to 'subscribe', to tailor it to a specific Output service.
*/
    function event_handler(event) {
        if (event.data.event_name === 'Signup') {
            event.data.event_name = 'subscribe';
        }
     	return event;
    }
    
    var events = batch.events;
    var newEvents = [];
    
    events.forEach(function(currentEvent){
    	try { 
    	    newEvents.push(event_handler(currentEvent));
    	}
    	catch(err){ }
    });
    
    batch.events = newEvents;
    
    callback(null, batch);
};

All Output vs Specific Output Rules

Rules can be applied in two places. ‘All Output’ rules are applied first, and their output is passed along to all Output services connected to that input. ‘Specific Output’ Rules are applied as part of a particular Input/Output connection and affect only that Output service.

In most ways the two types of rules operate in the same way. Both take the same arguments and return a batch object in the same format. However, slightly different fields can be accessed/altered in ‘All Outputs’ and ‘Specific Outputs’ rules. See Batch format for details.

Error Handling

When creating a rule in the mParticle dashboard, you must select a Failure Action. This determines what will happen if your rule throws an unhandled exception.

  • If you choose Discard, an unhandled exception will cause your rule return null, effectively dropping the batch.
  • If you choose Proceed, an unhandled exception will cause your rule to return the unaltered batch object, proceeding as if the rule had not been applied.

Regardless of which option you choose, it’s best practice to handle all exceptions in your code, rather than falling back on the above defaults. This is especially true if your rule deals with events, where an unhandled exception from just one event could lead to all events in the batch being dropped.

Batch format

See the main mParticle docs for full JSON batch examples. There are a few limitations on what is available and what can be changed in Rules:

Limitations applying to all Rules:

  • Unique IDs for the Batch (batch.batch_id) and for each event (event.event_id) cannot be altered.
  • Any null in the events array will cause a serialization error. If you want to drop individual events, make sure your handler does not push null to the event array.

Limitations applying only to ‘All Outputs’ Rules:

  • Unique IDs for the Batch (batch.batch_id) and for each event (event.event_id) will not be populated.
  • IP address (batch.ip) cannot be accessed in a rule. If a value is set for batch.ip it will be accepted only if it is a valid IP address.
  • The Application Info (batch.application_info) object cannot be accessed or altered.
  • Any changes made to the Deleted Attributes object (batch.deleted_user_attributes) will not be applied to the user profile.

Best Practices for Node JS Rules

AWS offers 2 Node runtimes: 4.3.2 and 6.10.3. If using Node for your Rule, be sure to select a runtime that supports the Javascript features you want to use.

To control costs, you can combine multiple transformations into a single rule.

The Batch object is deeply nested. Be careful when accessing nested keys, as the rule will error if one of the parent keys does not exist. Likewise, different event types have different properties. If part of your rule only applies to a specific event type, wrap it in a conditional statement.

/* 
If applied to an event that is not a Commerce event, 
this condition will cause an error and prevent 
the entire batch from being processed.
*/
if (event.data.product_action.action === "add_to_cart") {
    //do something
}

/* 
You can add a condition to only apply this part of 
the rule to Commerce events.
*/
if (event.event_type === "commerce_event") {
    if (event.data.product_action.action === "add_to_cart") {
        //do something
    }
}

/* 
The above rule will still fail if the commerce event does 
not contain a product action. To be safer, check each nested key.
*/
if (event.event_type === "commerce_event") {
    if (event.data && event.data.product_action && event.data.product_action.action === "add_to_cart") {
        //do something
    }
}

While the Rules UI supports two basic error handling mechanisms (either dropping the batch or forwarding it as is), it’s preferable for your rule to handle errors wherever possible to avoid unexpected behavior. If your rule applies multiple transformations to a batch, wrap each section in a try...catch statement so that an error in one part of your rule doesn’t stop other parts from being applied.

/* 
This event handler function has two parts to deal with commerce events and custom events. 
By using try...catch, we ensure that, if one part of the rule causes an unforseen error, 
the other part can still be applied.
*/
function event_handler(event) {
    try {
        if (event.event_type === "commerce_event") {
            if (event.data && event.data.product_action && event.data.product_action.action === "add_to_cart") {
                //do something
            }
        }  
    }
    catch (error) {
        // handle error
    }

    try {
        if (event.event_type === "custom_event") {
            if (event.data && event.data.event_name && event.data.event_name === "level_up") {
                event.data.event_name = "increase_level";
            }
        }  
    }
    catch (error) {
        // handle error
    }
    return event;
}

Examples

Sample test JSON and rules are available on Github.