Author : Vikas Kumar
Director - Customer Experience, Espire | Sitecore MVP 2021
03/Feb/2019

Vikas is a Sitecore MVP who heads the Digital Experience Platform (DXP) initiatives at Espire. A skilled craftsman in competency building, Vikas is a go-to-CMS expert, known for designing & developing best practices, that consistently transforms digital journey for clients.


Driving personalization with xConnect APIs and custom facet data | Part 2

Introduction

This blog post is in continuation to the post 'Driving Personalization with xConnect APIs and Custom Facet Data | Part 1'.

The previous post described how to create a custom facet in Sitecore xConnect, utilizing xConnect API to create a custom console application to create contacts in XDB and saving data in custom facets for those contacts.

This post will focus on the steps required to use the custom data captured to identify the contact visiting the website and rendering personalized content with the help of custom personalization rules.

Follow our session on YouTube

Deploy Model to Sitecore

This step is for enabling Sitecore to account for the new collection model and facets to be loaded when contact data is fetched from XDB for tracking.

  • Add reference to the library containing the model details to your website application
  • Create the following patch file and publish to your Sitecore instance

Sitecore Blog

Identifying the contact

While creating the contact, we added a unique ID as the identifier instead of the email. In our demo scenario we want the visitor to be identified using this unique ID which is available I the query string of the URL that was provided to the student to access the website.

1. We now add a processor the ‘mvc.requestBegin’ pipeline, which would identify the used based on the query string value as soon as the page loads.


 public class IdentifyContactOnLoad : RequestBeginProcessor 
    { 
        readonly string source = Sitecore.Configuration.Settings.GetSetting("UIDSource"); 
        readonly string uidQueryString = Sitecore.Configuration.Settings.GetSetting("QueryString"); 
        public override void Process(RequestBeginArgs args) 

        { 
            if (HttpContext.Current.Request.QueryString.HasKeys()) 
            { 

                var uid = HttpContext.Current.Request.QueryString[uidQueryString]; 
                if (!string.IsNullOrEmpty(uid)) 
                { 
                    XConnectHelper.IdentifyUser(uid, source); 
                } 
            } 
        } 
    } 

    public class XConnectHelper 
    { 
        public static string facetName = Sitecore.Configuration.Settings.GetSetting("FacetName"); 
        public static string redirectUrl = Sitecore.Configuration.Settings.GetSetting("RedirectUrl"); 
        readonly static string uidQueryString = Sitecore.Configuration.Settings.GetSetting("QueryString"); 
        public static void IdentifyUser(string uid, string source) 
        { 
            try 
            { 
                HttpContext.Current.Session.Abandon(); 

                if (Tracker.Current != null) 
                { 
                    Tracker.Current.EndVisit(true); 
                    Tracker.Current.EndTracking(); 
                } 

                if (Tracker.Current == null && Tracker.Enabled) 
                { 
                    Tracker.StartTracking(); 
                } 

                if (Tracker.Current != null && Tracker.Current.IsActive && Tracker.Current.Session != null) 
                { 
                    Tracker.Current.Session.IdentifyAs(source, uid); 
                } 
            } 

            catch (Exception ex) 
            { 
                HttpContext.Current.Response.Redirect(redirectUrl); 
            } 
        } 
} 


2. Add the following patch in a config file to patch the processor created to the pipeline:

Sitecore Blog 2-2

Creating custom personalization rule

We now want to create a custom personalization rule that would work on the custom facet value to deliver personalized content to the students.

Application changes

1. Extended helper method to retrieve custom facet data from Tracker
Add the following method to the XConnectHelper class to retrieve custom facet data from the tracker.


 public static string GetCourseName() 
        {         
            return string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[uidQueryString]) ? "" : StudentInfoFacets().Courses; 
        } 

        public static string GetCountry() 
        { 
            return string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[uidQueryString]) ? "" : StudentInfoFacets().Country; 
        } 

        private static StudentInfo StudentInfoFacets() 
        { 
            var xConnectFacets = Tracker.Current.Contact.GetFacet("XConnectFacets"); 
            return xConnectFacets.Facets[StudentInfo.DefaultFacetKey] as StudentInfo; 
        } 


2. Create custom condition class for the rule

Create a custom rule which would enable rendering different content based on the Course that the student has registered for.

public class DegreeRule : StringOperatorCondition where T : RuleContext 
    { 
        public string DegreeValue { get; set; } 
        protected override bool Execute(T ruleContext) 
        { 
            string studyArea = XConnectHelper.GetCourseName(); 
            if (!string.IsNullOrEmpty(XConnectHelper.GetCourseName())) 
            { 
                if (DegreeValue.ToString().ToLower() == studyArea.ToLower()) 
                { 
                    return true; 
                } 
            } 
            return false; 
        } 
     } 

3. Publish the changes to the Sitecore instance

Sitecore Changes

  • Create a custom condition in Sitecore at ‘/sitecore/system/Settings/Rules/Definitions/Elements'
  • In the text field mention the rule text, which would show up in the Personalization Editor
  • In the type field enter the custom condition class created and the library name
  • Save the item


Sitecore blog1



  • Use the rule set editor to setup personalized variants for content based on the Course selected by the student


Sitecore blog2



That's it, create more rules to utilize the custom data captured and deliver a highly personalized experience for your visitors.

MORE FROM OUR BLOGS

Delivering Consistent And Compelling Customer Communication With Self Serve Communication Portal

Delivering Consistent And Compelling Customer Communication With Self Serve Communication Portal

Supporting & Building Digital Solutions for Improving Customer Experience

Espire upgraded their existing corporate website to Sitecore version 9.1 for streamlining navigation issues, upgraded their E-commerce platform for delivering personalized customer experiences, deployed Sitecore technical enhancements to improve development process like refactoring, Unicorn, Glass mapper and T4templates, Search etc

A Leading Offender Management Software Solution Provider Gains the Competitive Edge of Agility

The client is a leading provider of offender management product, catering to the Governments sector mainly to Canada, USA and UK. With the aim of becoming agile by automating the internal processes and modernizing the systems, the client decided to develop an application to assess their end-client’s business process environment.

Powering Digital Transformation by revamping Quote & Buy process for a UK based leading Insurance company

Espire enhanced the existing Digital Transformation Journey using Umbraco CMS,Designed and implemented Umbraco based system for content management of the forms and modules

A Complete guide to upgrade to sitecore xm cloud why when and how

Sitecore XM Cloud's composable architecture allows organizations to create and manage personalized digital experiences for their customers, increasing engagement and conversions. With its cloud-based delivery model, Sitecore XM Cloud eliminates the need for organizations to maintain on-premises infrastructure, providing a cost-effective and secure solution for managing digital customer experiences

Leveraging composable architecture to drive unforgettable digital experiences

A composable architecture allows the application to adapt to the speed of business while enhancing responsiveness and scalability, paving the way towards driving greater business growth. In our latest blog, we share the benefits of leveraging a composable microservices-led architecture for business applications.

Top 10 strategic technology trends for 2023 by gartner Part2

In our first blog on Top 10 Strategic Technology Trends for 2023 by Gartner, we had discussed the top 5 technology trends that will enable businesses to offer customized experiences to the end users for better engagement, ROI, conversion, and sales. As a concluding part of this 2 series blog, we will enlist and discuss the next 5 disruptive tech trends to be witnessed in 2023 such as Wireless value realization, Superapps, Adaptive AI, Metaverse and Sustainable technology.

Top 10 strategic technology trends for 2023 by gartner Part1

The pace of change is accelerating, driven by emerging technologies such as artificial intelligence, the Internet of Things, big data and blockchain-all of which are changing how we work, live, and manage our businesses. In this blog, Part 1 of the top 10 strategic technology trends for 2023, we will be exploring the top 5 trends identified by Gartner, which can significantly contribute to your success story.

Subscribe To Our Blog

By clicking on "SUBSCRIBE NOW" you acknowledge having read our Privacy Notice.

Speak to Our Digital Transformation Specialists

Let's get you started on the digital-first & transformation journey. Reserve your free consultation or a demo today!

global-locations

12

+

Global Locations

global-services

100

+

Global Customers Served

resources-certified

1200

+

Resources

technology-partnerships

30

+

Technology Partnerships

years-of-experience

20

+

Years of Experience in Digital Transformation & Total Experience