Skip to content

Commit f46e71b

Browse files
committed
Update implement-custom-segment-parameters.md
1 parent f7f727c commit f46e71b

File tree

1 file changed

+99
-16
lines changed

1 file changed

+99
-16
lines changed

16/umbraco-engage/marketers-and-editors/personalization/extending-personalization/implement-custom-segment-parameters.md

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ description: >-
55

66
# Implement your own segment parameters
77

8-
Umbraco Engage comes with various built-in segment parameters to build a segment, such as "Customer Journey" and "Time of Day". However, you may want to build segments with custom rules that are not part of Engage by default. We've got you covered; you can add your own custom segment parameters to the Engage.
8+
Umbraco Engage comes with built-in parameters to build a segment, such as "Customer Journey" and "Time of Day".
9+
However, you may want to build segments with custom rules that are not part of Engage by default. We've got you covered; you can add your own custom segment parameters to Engage.
910

1011
The following guide will explain how this can be done. Note this is aimed at developers.
1112
There are 3 steps, 2 of which are mandatory and the 3rd is optional:
1213

1314
1. C# definition
14-
2. AngularJS definition
15-
3. (optional) Cockpit visualization
15+
2. Web component definition
16+
3. Cockpit visualization (optional)
1617

1718
This guide will use concrete 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 will be satisfied.
1819

1920
## 1. C# definition
2021

21-
Your custom segment parameter will need to be defined in C# in order for the Engage to use it.
22+
Your custom segment parameter will need to be defined in C# in order for Engage to use it.
2223
In code we refer to a segment parameter as a "segment rule".
2324

2425
A segment rule is not much more than this:
@@ -70,7 +71,8 @@ public class DayOfWeekSegmentRuleFactory : ISegmentRuleFactory
7071
}
7172
```
7273

73-
We are using the class DayOfWeekSegmentRuleConfig as a representation of the configuration of the rule, which is not strictly necessary but makes it easier. The configuration is stored as a string in the database but in code we like to have intellisense so we parse the stored configuration to this class:
74+
We are using the class DayOfWeekSegmentRuleConfig to represent the rule configuration. This is not strictly necessary but makes it easier.
75+
The configuration is stored as a string in the database. In code we like to have intellisense so we parse the stored configuration to this class:
7476

7577
```c#
7678
//Generating config schema on client side.
@@ -83,19 +85,87 @@ public class DayOfWeekSegmentRuleConfig
8385

8486
That's the C# part of the custom segment parameter.
8587

86-
## 2. Lit definition
88+
## 2. Web component definition
8789

88-
We have implemented the business logic for the segment parameter in the previous step, but the parameter cannot be added to your segments in the backoffice yet. In this step we will add a Lit element to enable you to add and configure your segments in the Engage segment builder.
90+
We have implemented the business logic for the segment parameter, but the parameter cannot be used in the backoffice yet. In this step we will add a web component to render the new rule in the Engage segment builder.
8991

9092
These steps will once again show concrete code samples that belong to our demo parameter "Day of week".
93+
You can create a folder to manage new files. Those files look like this:
94+
* segment-rule-base.ts
95+
* Type declaration for UeSegmentRuleBaseElement from Umbraco Engage. This is a temporary declaration until Umbraco Engage provides an npm package.
96+
* segment-rule-day-of-week.ts
97+
* Declaration for your web component using Lit.
98+
* index.ts
99+
* Exporting all your elements.
100+
* manifest.ts
101+
* Declares your element as a backoffice extension and register it in the Extension Registry. Read more about Extension Manifest [here](../../../../umbraco-cms/customizing/extending-overview/extension-registry/extension-manifest.md/).
91102

92103
First, re-generate the DayOfWeek config type on client side using the below command
93104

94105
```text
95106
npm run generate:api
96107
```
97108

98-
Then, create a new Lit element, implemeting new "Day of week" segment.
109+
**segment-rule-base.ts**
110+
111+
```typescript
112+
enum RuleDirection {
113+
INCLUDE = "include",
114+
EXCLUDE = "exclude",
115+
}
116+
117+
export interface UeSegmentRuleParameterConfig<ValueType> {
118+
isNegation: boolean;
119+
config: ValueType;
120+
}
121+
122+
export class UeSegmentRuleBaseElement<UeSegmentRuleParameterConfig> extends UmbLitElement {
123+
abstract renderReadOnly();
124+
abstract renderEditor();
125+
value: UeSegmentRuleParameterConfig;
126+
initialized: Promise<void>;
127+
128+
@property({ type: Boolean, attribute: true, reflect: true })
129+
readonly?: boolean;
130+
131+
updateParameterValue(value: any, key: keyof ValueType) {
132+
if (!this.value?.config) return;
133+
134+
const config = { ...(this.value.config ?? {}), ...{ [key]: value } };
135+
this.pending = assignToFrozenObject(this.value, { config });
136+
this.renderReadOnly();
137+
}
138+
139+
render() {
140+
return this.readonly ? this.#renderReadOnly() : this.#renderEditor();
141+
}
142+
143+
#renderReadOnly() {
144+
return html`
145+
<div id="readonly" .title=${this.manifest?.meta.name ?? ""}>
146+
<uui-icon .name=${this.manifest?.meta.icon ?? "icon-document"}></uui-icon>
147+
${this.renderReadOnly()}
148+
</div>
149+
`;
150+
}
151+
152+
#renderEditor() {
153+
return html`
154+
<div id="editorHeader">
155+
<h3>${this.manifest?.meta.name}</h3>
156+
<ue-button-group
157+
.values=${Object.values(RuleDirection)}
158+
.value=${this.value?.isNegation
159+
? RuleDirection.EXCLUDE
160+
: RuleDirection.INCLUDE}
161+
></ue-button-group>
162+
</div>
163+
<div id="editor">${this.renderEditor()}</div>
164+
`;
165+
}
166+
}
167+
168+
```
99169

100170
**segment-rule-day-of-week.ts**
101171

@@ -163,27 +233,40 @@ declare global {
163233

164234
```
165235

166-
Then, register this element in a manifest file.
236+
**index.ts**
237+
238+
```text
239+
export { UeSegmentRuleDayOfWeekElement } from "./segment-rule-day-of-week.js";
240+
export { UeSegmentRuleBaseElement } from "./segment-rule-base.js";
241+
```
167242

168243
**manifest.ts**
169244

170245
```json
171246
{
172-
name: "Day of week",
173-
type: "DayOfWeek",
174-
icon: "icon-calendar",
175-
elementName: "day-of-week",
176-
config: { dayOfWeek: "Sunday" },
247+
type: ENGAGE_SEGMENT_RULE_EXTENSION_TYPE,
248+
name: 'Engage Day of Week Segment Rule',
249+
alias: 'Engage.Segment.Rule.DayOfWeek',
250+
elementName: 'ue-segment-rule-day-of-week',
251+
weight: 100,
252+
meta: {
253+
name: 'Day of week',
254+
icon: 'icon-calendar',
255+
type: 'DayOfWeek',
256+
config: { dayOfWeek: 'Sunday' },
257+
},
177258
}
178259
```
179260

180261
That's it. If all went well you will see your custom parameter editor show up in the segment builder:
181262

182263
<figure><img src="../../../.gitbook/assets/engage-tutorials-personalized-segments-v16.png" alt="Day of week Segment."><figcaption><p>Day of week Segment.</p></figcaption></figure>
183264

184-
## 3. (optional) Cockpit visualization
265+
## 3. Cockpit visualization (optional)
266+
267+
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.
185268

186-
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 simply display the the raw configuration of the parameter as stored in the database ("{ dayOfWeek: Thursday }" in our example), and if you hover over it you will see the rule identifier "DayOfWeek" rather than a friendly name.
269+
By default it will simply display the the raw configuration of the parameter as stored in the database ("{ dayOfWeek: Thursday }" in our example), and if you hover over it you will see the rule identifier "DayOfWeek" rather than a friendly name.
187270

188271
<figure><img src="../../../.gitbook/assets/engage-tutorials-personalized-segments-cockpit-v16.png"></figure>
189272

0 commit comments

Comments
 (0)