Create a custom outcome model

Abstract

How to create definition items and custom outcome models as well as how to define the custom facet in the model and how to use the custom outcome model.

This topic describes how to create a custom outcome named CarPurchaseOutcome that inherits Outcome, and assumes the following requirements:

  • The CarPurchaseOutcome outcome will be used with a single definition item

  • The CarPurchaseOutcome has two additional mandatory properties named Make and Model

  1. Open the Sitecore Content Editor.

  2. Go to /sitecore/system/Marketing Control Panel/Outcomes.

  3. Right-click on the Outcomes item and select Insert > Outcome

  4. Enter an item name when prompted - for example, Car Purchase.

  5. Select the newly created definition, then click the Review tab, then select Deploy. This publishes the definition to the web database and deploys it to the Reference Data Service database.

Create a new class named CarPurchaseOutcome that inherits the Outcome class. This class has a fixed definition ID, which means it cannot be instantiated with any other definition ID.

using Sitecore.XConnect;
using System;

namespace Documentation
{
    public class CarPurchaseOutcome : Outcome
    {
        public static Guid CarPurchaseEventDefinitionId { get; } = new Guid("2b7e9e7e-f7ac-4ac2-8f8e-26a9c8644b23");

        public CarPurchaseOutcome(DateTime timestamp, string currencyCode, decimal monetaryValue, string model, string make)
            : base(CarPurchaseEventDefinitionId, timestamp, currencyCode, monetaryValue)
        {
            Model = model;
            Make = make;
        }

        public string Model { get; set; }
        public string Make { get; set; }
    }
}

Define the new outcome in your collection model using the .DefineEventType method. In the following example, the outcome model has been added to a model named SampleModel. If you are using the model in a Sitecore context, the model class must be registered in the client configuration file.

using Sitecore.XConnect;
using Sitecore.XConnect.Schema;

namespace Documentation
{
    public class SampleModel
    {
        public static XdbModel Model { get; } = SampleModel.BuildModel();

        private static XdbModel BuildModel()
        {
            XdbModelBuilder modelBuilder = new XdbModelBuilder("SampleModel", new XdbModelVersion(0, 1));

            modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
            modelBuilder.DefineEventType<CarPurchaseOutcome>(false);

            return modelBuilder.BuildModel();
        }
    }
}

Warning

If you do not register your outcome model, you cannot use it. You will get errors such as ‘The type of this instance does not correspond to any type in the schema’.

Use the xConnect Client API to add your outcome to an interaction. The following example demonstrates how to instantiate the custom outcome:

var purcaseOutcome = new CarPurchaseOutcome(DateTime.UtcNow, "DKK", 60000, "MX-5", "Mazda");

To use a custom outcome model within the tracker, you must create an event conversion pipeline processor. For more information, see Triggering custom events.