Thursday, April 4, 2019

Sitecore Pattern Cards and third party Service Integration


The Sitecore’s profiles, rules and pattern cards are very powerful personalization mechanism. On my current project, I had a requirement to integrate OOB personalization and a third party service that is responsible for a user personalization metrics.
When the user visit the page, we need to call an API, get the metrics and personalize the page’s look and feel. For example let’s say the metrics we get - are the distribution lists that the user belongs to, for example:

{
'user' : 'user name',
'data' : ['managers@somecompany.com','employee@somecompany.com',
'usa@somecompany.com']
}


The customer wants to display diff content for USA managers, USA employees and employees from Europe.
Let’s start from creating a Profile named “Audience” and necessary Profile Keys at /sitecore/system/Marketing Control Panel/Profiles:


Set the Max Value to 1 for all items, now let’s create 3 Pattern cards:




Let’s apply all those cards to our personalizable components, we just need to create few simple rules:




The Last step - now  when somebody visits the page, we need to get the metrics from the third party API and apply it to the Visit(we don’t care about previous visits) and Sitecore then can find the appropriate Pattern Card itself.

Register a Processor for it:


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <processitem>
        <processor patch:after="processor[@type='Sitecore.Analytics.Pipelines.CreateVisits.InitializeProfile, Sitecore.Analytics']" type="SomeNamespace.AudienceProfileProcessor, LibraryName">
      </processor></processitem>
    </pipelines>
  </sitecore>
</configuration>


And the processor code:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using Sitecore.Analytics;
using Sitecore.Analytics.Model;
using Sitecore.Analytics.Pipelines.ProcessItem;

namespace SomeNamespace
{
    public class AudienceProfileProcessor : ProcessItemProcessor
    {
        public override void Process(ProcessItemArgs args)
        {
            var profileName = "Audience";
            string name = GetCurrentUserName(); //Get current visitor name

            User user = GetAudienceData(name); //Call third party API for metrics
            if (!user.Data.Any())
            {
                return;
            }

            var profileKeys = GetProfileKeysFromDL(user.Data);

            if (Tracker.Current.Interaction.Profiles.ContainsProfile(profileName))
            {
                Tracker.Current.Interaction.Profiles.Remove(profileName);
            }

            List listOfProfileData = new List
            {
                new ProfileData(profileName)
            };
            Tracker.Current.Interaction.Profiles.Initialize(listOfProfileData);
           //Apply our metrics
            var profile = Tracker.Current.Interaction.Profiles[profileName];
            profile.Score(profileKeys);
            profile.UpdatePattern();
        }
}



That is it, now Users will see only appropriate content!

No comments:

Post a Comment