Convert an outcome

This topic describes how to convert an outcome that was triggered from the tracker into a custom CarPurchaseOutcome before it is saved to xConnect.

using Sitecore.XConnect;
using Sitecore.XConnect.Collection.Model;
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; }
    }

}

Create an outcome conversion processor

  1. Create a pipeline processor that inherits ConvertToXConnectEventProcessorBase<OutcomeData>.

  2. Implement CreateEvent and CanProcessPageEventData as shown. This handler assumes that the make and model of the car are stored in the CustomValues dictionary.

using Sitecore.Analytics.Model;
using Sitecore.Analytics.XConnect.DataAccess.Pipelines.ConvertToXConnectEventPipeline;
using Sitecore.Framework.Conditions;
using Sitecore.XConnect;

namespace Documentation
{
    public class ConvertCarPurchaseOutcome : ConvertToXConnectEventProcessorBase<OutcomeData>
    {
        protected override Event ConvertToEvent(OutcomeData entity)
        {
            var make = entity.CustomValues["Make"] as string;
            var model = entity.CustomValues["Model"] as string;

            var carPurchase = new CarPurchaseOutcome(entity.Timestamp, make, model, entity.CurrencyCode, entity.MonetaryValue);

            return carPurchase;
        }

        protected override bool CanProcess(Sitecore.Analytics.Model.Entity entity)
        {
            if (entity is OutcomeData)
            {
                OutcomeData outcomeData = (OutcomeData)entity;

                return (outcomeData.OutcomeDefinitionId == CarPurchaseOutcome.CarPurchaseOutcomeID);
            }

            return false;
        }
    }
}

Register outcome conversion processor

Create a new configuration file named MyBrand.CustomEvents.config and patch in your handler as shown:

Important

Your processor must be patched in before the final outcome handler.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
        <convertToXConnectEvent>
            <processor patch:before="processor[@type='Sitecore.Analytics.XConnect.DataAccess.Pipelines.ConvertToXConnectEventPipeline.ConvertOutcomeDataToOutcome, Sitecore.Analytics.XConnect']" type="Documentation.ConvertCarPurchaseOutcome, Documentation"/>
        </convertToXConnectEvent>
        </pipelines>
    </sitecore>
</configuration>

Trigger the outcome

Trigger the outcome with the custom data that the outcome conversion pipeline processor expects:

var ev = Sitecore.Analytics.Tracker.MarketingDefinitions.Outcomes[CarPurchaseOutcome.CarPurchaseOutcomeID];

if (ev != null)
{
    var outcomeData = new Sitecore.Analytics.Data.OutcomeData(ev, "DKK", 100000.00m);

    outcomeData.CustomValues.Add("Make", "Mazda");
    outcomeData.CustomValues.Add("Model", "MX-5");

    Sitecore.Analytics.Tracker.Current.CurrentPage.RegisterOutcome(outcomeData);
}

Note

From 9.0 Update-1 onwards, you can also trigger an outcome with custom data for the current interaction.