import { core } from "@mediarithmics/plugins-nodejs-sdk";
import { CustomInstanceContext } from "./interfaces/InstanceContextInterface";
export class ActivityAnalyzerPlugin extends core.ActivityAnalyzerPlugin {
// Called to update a user activity
// Uses the instance context built with instanceContextBuilder
// to adapt to the properties and technical files
protected async onActivityAnalysis(
request: core.ActivityAnalyzerRequest,
instanceContext: CustomInstanceContext)
: Promise<core.ActivityAnalyzerPluginResponse> {
const updatedActivity = request.activity;
// Your code to modify the activity.
// Exemple adding product infos in each event
// If the technical configuration allows it
if (instanceContext.technicalConfig.updateActivities){
updatedActivity.$events.forEach(event => {
if (event.$properties && event.$properties.$items && event.$properties.$items.length > 0) {
event.$properties.$items.forEach((item: any) => {
var product = Products.find(p => p.$id == item.$id);
item.$name = product.$name;
item.categories = product.categories;
item.inStock = product.inStock;
const response: core.ActivityAnalyzerPluginResponse = {
return Promise.resolve(response);
const errorResponse: core.ActivityAnalyzerPluginResponse = {
this.logger.error(`TRANSFORMATION ERROR while processing activity: ${JSON.stringify(request.activity)}`);
return Promise.resolve(errorResponse)
// Build the instance context
// by fetching properties and configuration files
protected async instanceContextBuilder(activityAnalyzerId: string)
: Promise<CustomInstanceContext> {
const baseInstanceContext = await super.instanceContextBuilder(activityAnalyzerId);
// Retrieve a technical configuration file
const validator = new Jsonschema.Validator();
const technicalConfig: ITechnicalConfig = await this.validateJSONSchema(TECH_CONFIG_FILE, validator, technicalConfigurationSchema, activityAnalyzerId);
// Retrieve a property from the plugin instance
const eventExclusionList = baseInstanceContext.properties.findStringProperty("events_exclusion_list");
// Return the completed instance context
const result: CustomInstanceContext = {
event_exclusion_list: eventExclusionList,
technicalConfig: technicalConfig
this.logger.debug(`Loaded InstanceContext with: ${JSON.stringify(result,null,4)}`);
return Promise.resolve(result);
this.logger.error(`Something bad happened during the build of the Instance Context ${err}`);
return Promise.reject(`Something bad happened during the build of the Instance Context ${err}`);