Sometimes you need more fine-grained personalization for your website. For this purpose the Umbraco Engage exposes a service called the IAnalyticsStateProvider.
This service provides access to all analytics-related information for the current request, and the segment information. When you need to execute custom code specifically tied to the personalization, you can use this service.
To get started you need an instance of anIAnalyticsStateProvider
, which can be resolved through Dependency Injection. For example consider the following case, where we use route hijacking to execute custom code for our content type called "Home
":
Umbraco will automatically resolve the service, without having to write any code. We can now use the service in our request, by calling the .GetState()
method to get the current state for the current request. Which in turn exposes the PageView containing the concrete information we are looking for.
The PageView
lies at the heart of Umbraco Engage Analytics feature and exposes a lot of interesting information. For now, we will focus on reading all segments for the current pageview. Depending on your configuration, a visitor can fall into multiple segments, as we can see by enumerating overall PageViewSegments
.
Consider the following example (continued from above) where the content of content type "Home
" was requested. We will now tell Umbraco to execute this custom code whenever the template HomeTemplate
is requested:
We can for example check if the current visitor falls into a segment called "MySegment". Keep in mind that a visitor can fall into any number of segments (zero, one, or all). A segment alone don't anything and can be regarded as purely informational, or as a "Flag" or "Label".
The personalization used by the Umbraco Engage to modify the appearance of a page is called Applied Personalization.
A page request can have only one active Applied Personalization. Based on the current segments (and their sort order), Umbraco Engage picks the first applicable Applied Personalization. This could be a multi-doctype or multi-page personalization (Engage section) or single-page personalization (content).
To inspect the resolved Applied Personalization, we can use the property AppliedPersonalization
on the state's PageView:
Be aware that no personalization may have been resolved for the current request. Make sure to do a null check before reading the AppliedPersonalization
property. The property SegmentId
will tell you which active segment was responsible for triggering the Applied Personalization.
A Segment may be used by different Applied personalizations, but only one personalization will ever be resolved and displayed.
This implies some different important things:
The Applied Personalization can only trigger on currently active segments (as found on Pageview.PageviewSegments
)
Not having any active segments automatically means there will never be any Applied Personalizations active. So configuring correct segments is essential.
Having a maximum of one active Applied Personalization per request means that you might find an unexpected personalization was activated. Always make sure to check the segment sorting order.
When testing it is a good idea to inspect the Cockpit information in your front end requests so you can see which segments are active.
The main two pillars of personalization that the Umbraco Engage offers are personas and customer journeys.
The impact of the content page on these can be managed on the Content Score tab in the Personalization app on the top right.
Sometimes you want more fine-grained control over when something does (or doesn't) score. For example, if a user places an order, the user has shifted from the customer journey step "think" to "do".
This might be difficult to accomplish through the Content Scoring UI in Umbraco, but can be done by code.
To manage scoring for personas, we need to get a reference to IPersonaService
. For the customer journey, we will need the ICustomerJourneyService
. Both services can be found under the namespace Umbraco.Engage.Infrastructure.Personalization.Services
.
To implement our example above, we will be using the ICustomerJourneyService
. To modify the customer journey step scoring, we need to know the ID of the step we are trying to score. For your implementation you could hardcode the IDs (since they are unlikely to change), we can also fetch them by name through the ICustomerJourneyGroupRepository
.
To resolve the required services, we will use Dependency Injection:
We will now request Umbraco Engage to provide the customer journey step "Do" from the group "Customer Journey".
This is the default name for the customer journey upon installation.
We can now inspect the step Do variable and find its ID
. To score the step, we provide the ID
and the score to the CustomerJourneyService
:
We have now added a score of 100 to the Customer Journey step "Do". It is also possible to add negative scores. In our example, we can decrease the scores for "See" and "Think".
Since the user is no longer (shifting away) from that step of the Customer Journey the implementation strategy is the same for personas.
Another, more advanced, example could be on how to reset the score of a persona for a given visitor. We can use the same approach as above to fetch the persona instead of the Customer Journey for the current visitor. We can get the visitor's current score based on the Persona ID, and subtract that exact score from said visitor.
Umbraco Engage has different built-in segment parameters to build segments, such as "Customer Journey" and "Time of Day".
You may want to build segments with custom rules not included in Umbraco Engage by default. You can add your custom segment parameters to Umbraco Engage.
In the following guide, we will show how this is done. There are three steps:
This guide will use 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.
You can download the following code files to your project to add the parameter directly to your solution.
Your custom segment parameter must be defined in C# for the Umbraco Engage to use it. In code, we refer to a segment parameter as a segment rule.
A segment rule is:
A unique rule identifier, e.g. DayOfWeek
.
A configuration object, e.g. { dayOfWeek: 3 }
.
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 page view.
You will have to implement the following interfaces for a new custom parameter:
Umbraco.Engage.Business.Personalization.Segments.Rules.ISegmentRule
You can extend the existing BaseSegmentRule
to simplify the implementation.
It is important to implement the bool IsSatisfied(IPersonalizationProfile context)
method.
Umbraco.Engage.Business.Personalization.Segments.Rules.ISegmentRuleFactory
Register your implementation of the segment rule factory with Lifetime.Transient
in 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:
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 or IntelliSense support in code. The stored configuration is parsed into this class:
The segment rule factory needs to be registered so Umbraco Engage can use it. The code below registers the factory in a new composer, you can use an existing composer for this if you like:
In the above example, we have shown how you can define custom segment parameters using C#. Next we look into enabling and configuring our segment in the Umbraco Engage segment builder.
We implemented the business logic for the segment parameter in the previous step, however, the parameter cannot be added to your backoffice segments yet. In this step, we will add some JavaScript and HTML to enable you to add and configure your segments in the Umbraco Engage segment builder.
This step will show concrete code samples that belong to our demo parameter "Day of week".
You need to create a folder in the App_Plugins folder of your project that will hold the new files.
For this example name it "day-of-week
". The folder and content look like this:
App_Plugins\day-of-week
package.manifest
Instructs Umbraco to load your JavaScript files
segment-rule-day-of-week-display.html
View for displaying the configuration of your segment parameter
segment-rule-day-of-week-display.js
AngularJS component for displaying your segment parameter
segment-rule-day-of-week-editor.html
View for editing the configuration of your segment parameter
segment-rule-day-of-week-editor.js
AngularJS component for editing the configuration of your segment parameter
segment-rule-day-of-week.js
Define your segment parameter and register it in the segment rule repository of Umbraco Engage
You can name the files, however, make sure to reference the JS files in your package.manifest
properly.
The contents for each of the files are below:
segment-rule-day-of-week.js
In this file, you define the segment parameter and register it in the repository of Umbraco Engage.
segment-rule-day-of-week-editor.html
This file contains the view of your parameter editor. Our example editor is a <select>
filled with the 7 days of the week.
We write the picked value to the config.dayOfWeek
property of our rule. You can make the editor as complex as you want, use multiple fields, etc.
For more inspiration, you can look at the built-in rule editors of Umbraco Engage in App_Plugins\Umbraco.Engage\dashboard\segments\builder\rules
.
We use the data.days
property of our rule definition in the editor. The editor gets passed in the rule definition as well as a config
object which we should update according to the user input.
segment-rule-day-of-week-editor.js
This registers the editor component in the Umbraco Engage module so we can use it.
It should not be necessary to update this file other than update the component name and templateUrl
.
segment-rule-day-of-week-display.html
This is the view file used for the visual representation of the segment parameter. We want to display the picked day to the user:
The chosen day of the week is stored as an integer (0-6) in $ctrl.config.dayOfWeek
, but in the display component shows the actual day (for example. Monday
). Our rule definition defines the mapping in its data.days
property so we convert it using that and display the name of the day.
segment-rule-day-of-week-display.js
In this file, we register the display component.
package.manifest
To make sure Umbraco loads your JS files we specify them here:
If all goes well you will see your custom parameter editor show up in the segment builder:
The new segment parameter will show up automatically in the Cockpit that is part of our package. The cockpit is a live view of Umbraco 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: 3 }
" in our example).
If you hover over it you will see the rule identifier DayOfWeek
rather than a friendly name.
If you want to change this to be more readable you can implement the Umbraco.Engage.Web.Cockpit.Segments.ICockpitSegmentRuleFactory
interface.
For the DayOfWeek
demo parameter, this is the implementation:
So we transform the JSON into a human-readable representation and we configure an icon to show up in the cockpit. Make sure to register this class in a composer (you can reuse the composer from the first step):
After it has been registered, Umbraco Engage will use the additional information to properly render your segment parameter in the cockpit as well.
The "DayOfWeek test" string is the name of the segment. This segment happens to have only 1 parameter which is the DayOfWeek parameter.
The personalization provided by Umbraco Umbraco Engage is built so users can personalize the content or layout of any page without programming skills from the UI.
One of the most common situations for implementing personalization is because you want to serve specific content to your users.
Groups of visitors are identified through segments, consisting of requirements to be met. This segment information is used to customize the user's experience through Applied Personalization.
The following articles cover common methods for implementing personalized content on your website.
Implement your own segment parameters
Retrieve segment information from code
Add custom scoring