Examples
Context for the following example
To demonstrate how the computed field works, consider this use case:
Goal: Calculate the basket amount over 12 months for items in the IT category.
For simplicity, this example focuses on the IT category. However, the computed field can be scaled to compute the amount for all categories, not just IT.
In this scenario, the computed field will:
Aggregate the basket amounts over the last 12 months.
Filter the data based on the IT category (or any other category, depending on the use case).
By scaling this approach, you can calculate the basket amounts for multiple categories, ensuring flexibility and extensibility in your calculations.
Step 1: Define your context for the Datamart Function
In this use case, we need to declare the State, Result, and UserActivity for computing the basket amount over the last 12 months for items in the IT category.
State Declaration
The State stores the basket amounts for each day, categorized by IT items, for the last 12 months. Here's how the state structure looks :
The activities_for_the_last_12_months
keeps the amount data for each day, where the date
is represented by a numeric value (e.g., timestamp), and the amount
is the total basket value for that day.
Result Declaration
The Result represents the total basket amount over the last 12 months for the IT category.
The result will return the computed IT_amount_for_the_last_12_months
after summing up the values stored in the state.
UserActivity Declaration
The UserActivity defines the structure of the activity that triggers the update. In this example, we focus on the items bought, particularly in the IT category.
The items_bought
array contains details about each item, such as category
(IT, for example) and price
.
To get some examples of your activity structure, you can look at the activity returned in the User lookup page in Navigator.
Step 2: Declare your Computed Field class
With the context declared, you can implement your computed field logic. Here's how the MyComputedField class looks:
onUpdateActivity
Goal: Update the state with new IT category purchases and remove activities older than 12 months.
It filters the user activity to ensure only IT items are included.
Removes old activities beyond the 12-month period.
Best Practice: Use the Events filter
to exclude activities or profiles that are not relevant to your computed field, rather than adding a filter within your function.
buildResult
Goal: Sum all the basket amounts stored in the state and return the total.
It checks each stored activity date to ensure it is within the last 12 months and sums the
amount
for each IT purchase.
The onUpdateActivity
function is triggered only when a new activity occurs. Therefore, you need to handle outdated activities within your function if the user point does not receive a new event to update the State.
Step 3: Test your plugin
Before deploying the plugin, create tests to ensure the function behaves as expected. Test cases might include:
Adding new activities and verifying the state is updated correctly.
Checking that old activities are purged.
Validating that the correct basket amounts are returned in the result.
Step 4: Declare your Computed Field in the schema
Once the computed field is implemented, you need to declare it in your schema as follows:
This will link the computed field (IT_amount_for_the_last_12_months
) to your schema, making it available for querying.
Handling Profile and Activity Information
When working with profile and activity information:
Be mindful that the computed field needs to be commutative during the initial loading. For cases where user profiles and activities need to be aggregated, it might be necessary to store more information in the State to ensure consistency and accuracy during this phase.
Example Use Case: Basket Amount by Fidelity Card
During initial loading, if you need to track the basket amount for current fidelity cards, you may need to store all activities grouped by fidelity card in the state. This is because, until the initial loading completes, you may not know which fidelity cards are currently active for the user.
Last updated
Was this helpful?