Development

List of Methods

OnUpdate methods

These functions are triggered during the initialization of the Datamart Function and whenever a new activity, profile update, or computed field modification occurs, updating the State accordingly.

onUpdateActivity

onUpdateActivity(state: State, userActivity: UserActivity): State

  • Inputs:

    • state: Current state object.

    • userActivity: New user activity data.

  • Outputs: Updated state object.

  • What It Does: Updates the state with new user activities and removes outdated activities.

  • Example:

// Trigger by a new activity
onUpdateActivity(state, userActivity) {
    // Logic to update state
    return updatedState;
}

The activities or profiles received by the plugin are raw data, so you can't rely on the structure defined in your schema to understand its design. However, you can refer to the structure outlined on the User Lookup page.

onUpdateUserProfile

onUpdateUserProfile(state: State, userProfile: UserProfile, operation: core.Operation): State

  • Inputs:

    • state: Current state object.

    • userProfile: Updated user profile data.

    • operation: Operation type (enum: UPDATE | DELETE).

  • Outputs: Updated state object.

  • What It Does: Handles updates to user profiles.

  • Example:

// Trigger by an update on the user profile
onUpdateUserProfile(state, userProfile, operation) {
    // Logic to handle user profile updates
    return updatedState;
}

onUpdateComputedFields

onUpdateComputedFields(state: State | null): Result

  • Inputs: state: Current state object or null.

  • Outputs: Result object.

  • What It Does: Computes the result from the current state.

  • Example:

// Trigger by the result computation of another computed field
onUpdateComputedFields(state) {
    // Logic to compute result
    return state;
}

buildResult

buildResult(state: State | null): Result

  • Inputs: state: Current state object or null.

  • Outputs: Result object.

  • What It Does: Computes the result from the current state.

  • Example:

buildResult(state) {
    // Logic to compute result
    return result;
}

Test

Before deploying the plugin, create tests to ensure the function behaves as expected.

Test cases might include:

  • Adding new activities, and update profiles and verifying the state is updated correctly. Check that the order we send profile updates and activities has no impact.

  • Checking that activities which are no more relevant to compute the result are removed from the state.

  • Validating that the correct values are returned in the result.

Best Practices

  • How to decide if we create 1 or several plugin(s)? (example with 2 scores)

    • If the required data in the state is the same for both scores → 1 plugin

    • If the required data in the state is different (state is polymorph) → 2 plugins

  • Always return the same state in case of error, or null. Otherwise, we delete the state, and so reset the result.

  • Maximize the number of scores calculated by a Computed Field Function to optimize performance. Therefore, the same state is used to calculate multiple scores, and we avoid storing the same state multiple times.

  • Ensure that the state is designed to store relevant information efficiently and is commutative to maintain consistency while building the result.

  • Be sure to have a cleaning rule to update the state base on the defined lookback window for both userProfile and userActivities.

    • In other words, when updating the state, be sure to remove the data that are no more useful to compute the result, in order to limit the state size.

  • When editing a live computed field, you must relaunch an Initial Loading through API.

Last updated

Was this helpful?