Sitecore EXM Personalization- Custom Rules in EXM

Sitecore email Personalization- Custom Rules in EXM

Sitecore Version: Sitecore 9.3

In the previous blog, we covered custom rules for Segmented lists and Marketing Automation. In this blog, we will see how to create custom rules for component personalization. Specifically, we will cover, personalization for EXM email components. In the EXM,  we can create an email that we design an Experience editor page using componentization.




To create a custom AB testable template in EXM, please read this documentation: https://doc.sitecore.com/developers/exm/90/email-experience-manager/en/walkthrough–building-a-custom-message-template.html

Now, we can personalize the components in Email similar to the Experience editor components. We’ll see how to create a rule based on custom facets.


First step is to create a new tag for our Custom rule under “/sitecore/system/Settings/Rules/Definitions/Tags” based on template- “/sitecore/templates/System/Rules/Taxonomy/Tag


Next- create custom Element and personalization condition under “/sitecore/system/Settings/Rules/Definitions/Elements”

As part of the personalization condition there are two fields to populate:

Text– It shows text that would be presented to content editors when using this condition with Rules Engine, this is what we have used in this example-where the contact’s customer ID is [ContactID, Text,, specific value]

Everything under square brackets []  would be replaced by Rules Engine as selectors.

Type – Type specifies fully qualified class and assembly names where we want to write/perform the business rule for the custom personalization condition.

using System.Collections;
using Sitecore.Analytics;
using Sitecore.Analytics.Model.Entities;
using Sitecore.Analytics.Model.Framework;
using Sitecore.Analytics.Tracking;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using Sitecore.XConnect.Segmentation.Predicates;
using xconnect.customfacet.Model;
 
namespace exm.EmailConfiguration.Predicates
{
    class CustomerIDMatches<T> : WhenCondition<T> where T : RuleContext
    {
        public string ContactID { get; set; }
        public StringOperationType Comparison { get; set; }
 
        protected override bool Execute(T ruleContext)
        {
            Contact contact = Tracker.Current.Session.Contact;
             
            if (contact == null)
            {
                Log.Info(this.GetType() + ": contact is null", this);
                return false;
            }
 
            var xConnectFacet = Sitecore.Analytics.Tracker.Current.Contact.GetFacet<Sitecore.Analytics.XConnect.Facets.IXConnectFacets>("XConnectFacets");
            var allFacets = xConnectFacet.Facets;
            CustomerFacets customerInfo = allFacets[CustomerFacets.DefaultFacetKey] as CustomerFacets;
            if (customerInfo.CustomerId == ContactID)
            {
                return true;
            }
            return false;
        }
    }
}

As in the above code you can see we are trying to fetch our custom facet (CustomerFacet) value from session. To load custom facets in session, we need to register custom facets key in App_Config\Sitecore\Marketing.Tracking\Sitecore.Analytics.Tracking.config file

<dataAdapterManager defaultProvider="xconnect">
            <providers>
                <clear/>
                <add name="xconnect" type="Sitecore.Analytics.XConnect.DataAccess.XConnectDataAdapterProvider, Sitecore.Analytics.XConnect">
                    <facets hint="raw:AddFacet">
                        <!-- Removing some of the default facets (Classification, EngagementMeasures, etc.) can cause issues, because there is code depending on them. -->
                        <facet facetKey="Classification"/>
                        <facet facetKey="EngagementMeasures"/>
                        <facet facetKey="ContactBehaviorProfile"/>
                        <facet facetKey="Personal"/>
                        <facet facetKey="KeyBehaviorCache"/>
                        <facet facetKey="ListSubscriptions"/>
                        <facet facetKey="CustomerInterest"/>
                    </facets>
                    <GetOperationTimeout>0.00:00:05</GetOperationTimeout>
                </add>
            </providers>
        </dataAdapterManager>


All built-in facets define a DefaultFacetKey – inspect this property to get the default key for a particular facet. For example, the AddressList facet key is Addresses.

Facets are represented by the Sitecore.Analytics.Tracker.Current.Contact.Facets dictionary in the tracker and are loaded from xConnect into shared session state at the start of a session. The tracker does not lock contacts in session, and changes made to facets stored in Sitecore.Analytics.Tracker.Current.Contact.Facets are not saved on session end.

Step 3- Next step is to assign custom tag which we created in step#1 to default element tag definition.



Step 4- Next- we would need to add this tag to conditional rendering such that this new rule is available in the rule editor for content editors to personalize the components.

Step 5- Once all the above changes are made content/business editors can configure this rule to personalize the components from experience editor or content editor from presentation->Personalize section.

References:

https://doc.sitecore.com/developers/93/sitecore-experience-platform/en/load-facets-into-session.html

Comments