Implement your own segment parameters
Discover how to create and manage custom segments.
Umbraco Engage comes with built-in parameters to build a segment, such as "Customer Journey" and "Time of Day". However, custom segments can be built by providing your own segment parameters.
The following guide explains how to achieve this. It is aimed at developers. There are three steps, two are mandatory, and the last one is optional:
C# definition
Web component definition
Cockpit visualization (optional)
This guide uses code samples to add a "Day of week" segment parameter where you can select a single day of the week. If a pageview happens on that day, the segment parameter is satisfied.
1. C# definition
Your custom segment parameter needs to be defined in C# for Engage to use it. In code, a segment parameter is referred to as a "segment rule".
A segment rule is not much more than this:
A unique rule identifier, e.g. "DayOfWeek".
A configuration object, e.g. "{ dayOfWeek: "Monday" }"
This is optional, but most rules will have some sort of configuration that the user can alter in the Segment Builder. In our example, the user can configure the specific day of the week.
A method that specifies whether the rule is satisfied by the current pageview.
You will have to implement the following interfaces for a new custom parameter:
Umbraco.Engage.Infrastructure.Personalization.Segments.Rules.ISegmentRuleYou can extend the existing
BaseSegmentRuleto simplify the implementation.The most important part to implement is the
bool IsSatisfied(IPersonalizationProfile context)method.
Umbraco.Engage.Infrastructure.Personalization.Segments.Rules.ISegmentRuleFactoryRegister your implementation of the segment rule factory with
Lifetime.Transientin a composer.
For the "Day of week" example, the code looks like this:
And the factory which is used to create an instance of this rule:
The class DayOfWeekSegmentRuleConfig is used to represent the rule configuration. This is not strictly necessary, but it makes it easier. The configuration is stored as a string in the database. In code, Intellisense is enabled to parse the stored configuration to this class:
2. Web component definition
The business logic for the segment parameter has been implemented, but the parameter cannot yet be used in the backoffice. In this step, a web component will be added to render the new rule in the Engage segment builder.
This demo assumes you are creating multiple custom rules, which are then provided as a bundle in the backoffice.
First, follow the Vite Package Setup article to scaffold your extension. Use MySegmentRules as your package name.
Install
@umbraco-engage/backofficepackage, replacingx.x.xwith your Engage version:
Ensure your
vite.config.tslooks like the example below:
Update
umbraco-package.jsonto register the bundle and segment rule. Note themeta.typeproperty matches theRuleTypedefined above inDayOfWeekSegmentRuleFactory.
Update
src/my-element.tsas below. NoteDayOfWeekSegmentRuleConfigModelreflects the same data contract as theDayOfWeekSegmentRuleConfigclass in C#. While it is possible to generate this using code-gen tools, this has been done manually to avoid complicating this tutorial.
Note too the meta.config object defined above in the package JSON implements the DayOfWeekSegmentRuleConfigModel interface.
By extending UeSegmentRuleBaseElement you avoid writing boilerplate code, and don't have to worry about handling value updates or syncing data back to the segment.
Build the typescript file:
That's it. If all went well you will see your custom parameter editor show up in the segment builder:

3. Cockpit visualization (optional)
The new segment parameter will show up automatically in the Cockpit that is part of our package. The cockpit is a live view of Engage data for the current visitor. This includes active segments of the current visitor, and therefore your new segment parameter can also show up in the cockpit.
By default, it will display the raw configuration of the parameter as stored in the database ("{ dayOfWeek: Thursday }" in our example). If you hover over it, you will see the rule identifier "DayOfWeek" rather than a friendly name.

If you would like to change this to be a bit more readable, you can implement the Engage.Web.Cockpit.Segments.ICockpitSegmentRuleFactory interface. For the DayOfWeek demo parameter, this is the implementation:
The JSON is transformed into a human-readable representation, and an icon is configured to appear in the cockpit. Make sure to register this class in a composer (you can reuse the composer from step 1):
Engage will then use the additional information to render your segment parameter correctly in the cockpit. The "DayOfWeek test" string is the name of the segment. This segment happens to have only 1 parameter which is the DayOfWeek parameter.

Last updated
Was this helpful?