Developer
User guidesDeveloper websiteHelp centerLog in
  • Welcome!
  • Organisations structure
    • Datamart
    • Users and roles
  • User points
    • User identifiers
      • Networks IDs
        • Device-based Network IDs
          • Custom Device ID integration
          • ID5
          • First ID
        • User-based Network IDs
          • Custom User ID integration
          • UTIQ martechpass
      • Accounts
      • Emails
      • Device identifiers
    • User activities and events
    • Compartments
    • User profiles
    • User segments
    • Hyper point & Quarantine
  • Data model
    • Defining your schema
    • Computed fields
      • Concepts
      • Setup
      • Development
      • Examples
  • Data ingestion
    • Real time user tracking
      • Website tracking
      • Mobile apps tracking
      • Ads exposure tracking
      • AMP tracking
      • Conversions tracking
      • Email views and clicks
      • Tracking API
      • Event rules
      • Activity analyzers
    • Bulk processing
      • Imports
        • User activities import
        • User profiles import
        • User choices import
        • Segments import
      • Deletions
        • User identifiers deletion
        • Device points deletion
        • User points deletion
      • User identifiers association
      • Integration batch
    • Activities analytics
    • Data warehouse
      • Preliminary setup
        • BigQuery
      • Create data warehouse
  • Querying your data
    • OTQL queries
    • OTQL examples
    • GraphQL queries
    • UserPoint API
    • User activities
    • Activities analytics queries
      • API Quickstart
      • Dimensions and metrics
      • Use cases
    • Funnel API
  • Alerting
    • Alert configurations
  • Data visualisation
    • Quickstart
    • Dashboards
    • Sections and cards
    • Charts
    • Datasets and data sources
      • Using a data file data source
    • Transformations
    • Filters
    • Cookbook
    • Reference
  • Advanced usages
    • Audience segmentation
      • Audience features
      • Segment builders
      • Audience segment metrics
      • Audience segment feed
        • Building new feeds
        • Monitoring a feed
        • Curated Audiences (SDA)
      • Edge segments
      • Cohort-based Lookalike
    • Contextual targeting
      • Setup
      • Activation
        • Google Ad Manager
        • Xandr (through prebid.js)
      • API documentation
    • Exporting your data
      • Query Exports
      • Datamart replication
    • Data privacy compliance
      • User choices
      • Cleaning rules
      • Exercise of user rights
      • Cookies
    • Campaigns
    • Automations
      • Email routers
      • Email renderers
      • Opt-in provider
      • Custom action plugins
      • Usage limits for automations
    • Plugins
      • Concepts
      • Creation & Deployment
      • Coding your plugin
      • Manage existing plugins
      • Layouts
      • Presets
      • Monitoring
      • Throttling
      • Batching (for external feeds)
    • Platform monitoring
      • Resources usage
        • Dimensions and metrics
      • Collection volumes
        • Dimensions and metrics
      • Events ingestion monitoring
        • Dimensions and metrics
    • Data Clean Room
      • Bunker
      • Clean room
  • Resources
    • Tutorial: Data Ingestion
      • Your first events
        • Add the mediarithmics tag
          • Getting the tag
          • Adding the tag
        • Send events using the tag
          • Adding event properties
          • Finding the UserEvent type in your schema
          • Matching your schema
          • Standard events
      • Your first bulk imports
        • API basics
          • Authentication
          • Your first API call
        • Send documents using the API
          • Requirements
          • Sending documents
    • Using our API
      • Authentication
    • Tools & libraries
      • mics CLI
      • JS Tag
      • Plugin SDK
    • Data cubes
      • Creating a report
      • Reference
Powered by GitBook
On this page
  • Context for the following example
  • Step 1: Define your context for the Datamart Function
  • State Declaration
  • Result Declaration
  • UserActivity Declaration
  • Step 2: Declare your Computed Field class
  • onUpdateActivity
  • buildResult
  • Step 3: Test your plugin
  • Step 4: Declare your Computed Field in the schema
  • Handling Profile and Activity Information

Was this helpful?

Export as PDF
  1. Data model
  2. Computed fields

Examples

Context for the following example

To demonstrate how the computed field works, consider this use case:

Goal: Calculate the sales over 12 months for items in the IT category per user.

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:

  1. Aggregate the order amounts over the last 12 months.

  2. 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 sales amount 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 sales amount over the last 12 months for items in the IT category.

State Declaration

The State stores the order amounts for each day, categorized by IT items, for the last 12 months. Here's how the state structure looks :

export interface State {
  activities_for_the_last_12 _months: { 
    [date: number] : [{
      amount: number;
    }]
  }
}

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 order value for that day.

Result Declaration

The Result represents the total sales amount over the last 12 months for the IT category.

export interface Result {
  IT_amount_for_the_last_12_months: number;
}

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.

interface Items {
    category: string;
    price: number;
}

interface UserActivity {
  items_bought: Items[];
}

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:

export class MyComputedField extends core.ComputedFieldPlugin<State, Result, UserActivity, UserProfile, ComputedField> { 
    constructor() { 
        super(); 
    }
 
    // Function to Update the state
    onUpdateActivity(state: State, userActivity: UserActivity): State { ... }
    
    // Won't be used but need to be declared;
    onUpdateUserProfile(state: State, userProfile: UserProfile, operation: core.Operation): State {
       return state;
    }

    // Won't be used but need to be declared;
    onUpdateComputedField(state: State, computedField: ComputedField): State {
        return state;
    }
  
    // Function to compute the Result
    buildResult(state: State | null): Result { ... }
}

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:

type UserPoint {
  id: ID!
  accounts: [UserAccount]
  …
  IT_amount_for_the_last_12_months: Int! @ComputedField(technical_name = “IT_Amount”) @TreeIndex(index:"USER_INDEX")
}

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.

PreviousDevelopmentNextData ingestion

Last updated 14 days ago

Was this helpful?