Convert a goal

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

Create a custom goal model

using Sitecore.XConnect;
using System;

namespace Documentation
{
    public class NewsletterSignupGoal : Goal
    {
        public static Guid NewsletterGoalID = Guid.Parse("{1779CC42-EF7A-4C58-BF19-FA85D30755C9}");

        public NewsletterSignupGoal(DateTime timestamp, Guid newsletterID) : base(NewsletterGoalID, timestamp)
        {
            NewsletterId = newsletterID;
        }

        public Guid NewsletterId { get; private set;  }
    }
}

Create a goal conversion processor

  1. Create a pipeline processor that inherits ConvertPageEventDataToEventBase.

  2. Implement CreateEvent and CanProcessPageEventData as shown. This handler assumes that the NewsletterId is stored in the Data property.

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

namespace Documentation
{
    public class ConvertNewsletterSignupGoal : ConvertPageEventDataToEventBase
    {
        protected override bool CanProcessPageEventData(PageEventData pageEventData)
        {
            Condition.Requires(pageEventData, nameof(pageEventData)).IsNotNull();

            if (pageEventData.PageEventDefinitionId == NewsletterSignupGoal.NewsletterGoalID)
            {
                return true;
            }

            return false;
        }

        protected override Event CreateEvent(PageEventData pageEventData)
        {
            var newsletter = Guid.Empty;

            Guid.TryParse(pageEventData.Data, out newsletter);

            NewsletterSignupGoal newsletterSignupGoal = new NewsletterSignupGoal(pageEventData.DateTime, newsletter);

            return newsletterSignupGoal;
        }
    }
}

Register goal 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 goal handler as shown.

<?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.ConvertPageEventDataToGoal, Sitecore.Analytics.XConnect']" type="Documentation.ConvertNewsletterSignupGoal, Documentation"/>
        </convertToXConnectEvent>
        </pipelines>
    </sitecore>
</configuration>

Trigger the goal

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

var ev = Sitecore.Analytics.Tracker.MarketingDefinitions.Goals[NewsletterSignupGoal.NewsletterGoalID];

if (ev != null)
{
    var newsletterId = Guid.NewGuid(); // Fake newsletter ID
    var pageData = new Sitecore.Analytics.Data.PageEventData(ev.Alias, ev.Id);

    pageData.Data = newsletterId.ToString();

    Sitecore.Analytics.Tracker.Current.CurrentPage.Register(pageData);
}