Create a calculated facet
This topic demonstrates how to implemented a calculated facet named AverageSpend. The facet stores data about the contact’s purchase history, including average spend per purchase.
Create and register a facet
-
Create a facet class:
NoteThis example assumes that all purchases are made using the same currency.
-
Register the facet in a model:
Implement a calculated facet plugin
-
Create a calculated facet handler that implements
Sitecore.XConnect.Service.MergingCalculatedFacetHandler<AverageSpend>. Notice the following:-
An
InteractionFacetDependencyobject is passed into the base handler. This ensures that theWebVisitfacet is loaded if it is present on the interaction. However, the facet is not required for the handler to run. -
The
Merge()method determines what happens to this facet when two contacts are merged. In this, the total spend and total purchase are added together, and the average spend per purchase is recalculated. You must implement theMerge()method for calculated facets. -
The
UpdateFacet()method determines what happens when a new interaction is submitted to xConnect. -
All methods return
trueif the facet was updated.
NoteIf the the
Requiredproperty of anyInteractionFacetDependencyis set totrue, the handler will only execute if this facet is present on the interaction. -
-
Open the
\App_data\config\sitecore\Collection\sc.XConnect.Collection.Model.Plugins.xmlconfiguration file in xConnect. -
Add two entries for the handler - one for
ICalculatedFacetHandlerand one forIContactMergeHandler:
Pass in configuration options
You can pass configuration options into a calculated facet handler. In the following example, a configuration option controls whether or not the LatestReferrer property of the AverageSpend facet is set.
-
Add a public property to represent your option - for example,
public bool PopulateReferrer { get; }. -
Add a constructor that takes an
IConfigurationobject. The following scenario assumes that the configuration option is namedPopulateReferrer: -
Open
\App_data\config\sitecore\Collection\sc.XConnect.Collection.Model.Plugins.xmland add options into configuration as shown. If the option is used during merge and update, specify the option twice as shown.
Get calculated facets
Use the xConnect Client API to retrieve calculated facets in the same way as any other facet. Do not edit calculated facets directly.