Personalize content
Describes how to personalize content using a headless approach for content recommendations through the Optimizely Content Delivery API.
How do you add content personalization to a headless solution where the tracking does not hit a controller in the presentation layer? You can use a headless approach for content recommendations through the Optimizely Content Delivery API. To do this you need to implement compatible tracking and calls to the recommendation API. See Tracking.
In a headless scenario, you can also customize the DefaultContentModelMapper
and replace it with your own. See Customizing data returned to clients and Serialization.
Example: Customizing DefaultContentModelMapper
You can manipulate with personalization by customizing the DefaultContentModelMapper
.
- Create a new class where your
CustomContentModelMapper
inherits fromDefaultContentModelMapper
. Register it withServiceConfiguration
and inDependencyResolverInitialization
(this is an example from an Alloy site).
public void ConfigureContainer(ServiceConfigurationContext context)
{
//Implementations for custom interfaces can be registered here.
context.ConfigurationComplete += (o, e) =>
{
//Register custom implementations that should be used in favour of the default implementations
context.Services.AddTransient < IContentRenderer, ErrorHandlingContentRenderer > ()
.AddTransient < ContentAreaRenderer, AlloyContentAreaRenderer > ()
.AddTransient < IContentModelMapper, CustomContentModelMapper > ();
};
}
- With
CustomContentModelMapper
:
[ServiceConfiguration(typeof (IContentModelMapper), Lifecycle = ServiceInstanceScope.Singleton)]
public class CustomContentModelMapper: DefaultContentModelMapper
{
public CustomContentModelMapper(IContentTypeRepository contentTypeRepository, ReflectionService reflectionService, IContentModelReferenceConverter contentModelService, IUrlResolver urlResolver, IEnumerable < IPropertyModelConverter > propertyModelConverters): base(contentTypeRepository, reflectionService, contentModelService, urlResolver, propertyModelConverters)
{}
public override ContentApiModel TransformContent(IContent content, bool excludePersonalizedContent = false, string expand = "")
{
var personalization = System.Web.HttpContext.Current.Request.Params["personalization"];
return base.TransformContent(content, string.IsNullOrWhiteSpace(personalization) ? excludePersonalizedContent : Boolean.Parse(personalization), expand);
}
}
In this way, you can take advantage of personalization parameters in your request.
Note
The
DefaultContentModelMapper
is currently marked as internal, so there is a risk of unadvertised breaking changes when customizing this.
Updated about 1 month ago