Create a custom goal model
How to create a custom goal model including how to create a goal definition item, how to create a custom goal model and how to use the custom goal model.
This topic describes how to create a custom goal named NewsletterSignup
that inherits Goal
, and assumes the following requirements:
-
The
NewsletterSignup
goal will be used with a single goal definition item -
The
NewsletterSignup
goal has an optionalCampaignId
property
Create a goal definition item
-
Open the Sitecore Content Editor.
-
Go to /sitecore/system/Marketing Control Panel/Goals.
-
Right-click on the Goals item and select Insert > Goal.
-
Enter an item name when prompted - for example, Newsletter Signup.
-
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 custom goal model
Create a new class named NewsletterSignup
that inherits the Goal
class. This class has a fixed definition ID, which means it cannot be instantiated with any other definition ID. The class has an optional property named CampaignId
which can be set if the contact signed up for a newsletter as part of a campaign.
using Sitecore.XConnect;
using System;
namespace Documentation
{
public class NewsletterSignup : Goal
{
public static Guid NewsletterSignupEventDefinitionId { get; } = new Guid("3a7e9e7e-a2ac-4ac2-8f8e-26a9c8644b23");
public Guid CampaignId { get; set; }
public NewsletterSignup(DateTime timestamp)
: base(NewsletterSignupEventDefinitionId, timestamp)
{
}
}
}
Define a custom goal in the model
Define the new goal in your collection model using the .DefineEventType
method. In the following example, the goal 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<NewsletterSignup>(false);
return modelBuilder.BuildModel();
}
}
}
If you do not register your goal 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 custom goal model
Use the xConnect Client API to add your goal to an interaction. The following example demonstrates how to instantiate the custom goal:
var newsletterGoal = new NewsletterSignup(DateTime.UtcNow);
var campaign = Guid.Parse("e7f411bc-09e6-4872-8961-1cf078e317e9"); // Sample campaign ID
newsletterGoal.CampaignId = campaign;
Use a custom goal model with the tracker
To use a custom goal model within the tracker, you must create an event conversion pipeline processor. For more information, see Triggering custom events.