Trigger an Outcome Programmatically

As you probably already know, the main difference between an outcome and a goal, is the possibility to assign a monetary value to an outcome so that you can use it to get better reporting within your analytics and eventually to understand which paths/content converted better.

Outcomes can only be assigned programmatically, this snippet simulate a controller rendering that trigger the ProductPurchase Outcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Mvc;
using System.Web.Mvc;
using System.Web;
using Sitecore.Analytics.Outcome.Extensions;
using Sitecore.Analytics.Outcome.Model; 

public ActionResult PurchaseOutcome()
    {
        // Register an outcome
        //It will only be saved to the database on session end

        var id = Sitecore.Data.ID.NewID;
        var interactionId = Sitecore.Data.ID.NewID;
        var contactId = Sitecore.Data.ID.NewID;

       // definition item for Purchase Outcome
      var definitionId = new Sitecore.Data.ID("{9016E456-95CB-42E9-AD58-997D6D77AE83}");

        var outcome = new ContactOutcome(id, definitionId, contactId);
        outcome.MonetaryValue = 153;
        Sitecore.Analytics.Tracker.Current.RegisterContactOutcome(outcome);
        return Content("Outcome purchase 153");
    }

These are the references required:

referencesoutcome

And this is the Outcome definition:

outcome

Leave a comment