How to Set Persona/Pattern Card Programmatically in Sitecore?

Recently came across a very interesting requirement of analyzing user’s pattern based on search action performed by user on the site. Every webiste has one common functionality implemented – site search. Here you can hook your code to understand user’s pattern by observing the keywords user is searching. For example: whenever user search terms like softwareprogramming or computer then system should be intelligent enough to understand that – user matches with one of the existing persona Software Engineering.

Being a Sitecore developer, you know that how to set persona on any Sitecore item which is straight forward process (e.g. select desired persona from right top corner of content tab). But how about setting persona or pattern card programmatically at runtime? Here are the steps I followed to accomplish above mentioned requirement. 1) Provided facility to configure all search keywords or tags.

2) Added one custom field Persona Tags in pattern card template which lists all above mentioned search tags.

3) The final step is to write custom code which will set current user’s profile/persona based on search terms by associating with existing Sitecore pattern cards. You can hook this code on search button click as shown in very first screen-shot of this post.

public static void SetPatternCard(string searchedTerm)         {             try             {                 Stopwatch sw = Stopwatch.StartNew();                 Sitecore.Data.Database db = Sitecore.Context.Database;                  //Get persona tag from Data folder matching the searched keyword                 var peronaDataTagsQuery = string.Format("/sitecore/content/Home/Data/Persona Tags//*[@name='{0}']", searchedTerm);                 Item matchedPeronaTag = db.SelectSingleItem(peronaDataTagsQuery);                  if (matchedPeronaTag != null)                 {                     //Get all pattern cards which has matching persona tag as per searched keyword                                     var query = string.Format("/sitecore/system/Marketing Control Panel/Profiles//*[@@templateid='{0}' and contains(@Persona Tags,'{1}')]", "{4A6A7E36-2481-438F-A9BA-0453ECC638FA}", matchedPeronaTag.ID);                     Item[] allPatternCards = db.SelectItems(query);                      foreach (Item patternCard in allPatternCards)                     {                         //Set current profile with respective pattern                         var profile = Sitecore.Analytics.Tracker.Current.Interaction.Profiles[patternCard.Parent.Parent.Name];                          Sitecore.Data.Fields.XmlField xmlData = patternCard.Fields["Pattern"];                         XmlDocument xmlDoc = xmlData.Xml;                          XmlNodeList parentNode = xmlDoc.GetElementsByTagName("key");                         var scores = new Dictionary();                          foreach (XmlNode childrenNode in parentNode)                         {                             if (childrenNode.Attributes["value"].Value != "0")                             {                                 float keyValue;                                 float.TryParse(childrenNode.Attributes["value"].Value, out keyValue);                                 scores.Add(childrenNode.Attributes["name"].Value, keyValue * 3);                             }                         }                          profile.Score(scores);                         profile.UpdatePattern();                         sw.Stop();                         Sitecore.Diagnostics.Log.Audit("SetPatternCard took total " + sw.ElapsedMilliseconds.ToString() + " milliseconds.", new Exception());                     }                 }             }             catch (Exception ex)             {                 Sitecore.Diagnostics.Log.Error("Error occured in SetPatternCard " + ex.ToString(), new Exception());             } 

How can you confirm that it has really set the associated pattern? Answer is simple! You can personalize any page component based on pattern card condition. So for above Software Engineering example, you can personalize component as suggested in below screens.

This implementation will definitely add value to your digital marketing strategy!

6 Responses to “How to Set Persona/Pattern Card Programmatically in Sitecore?”
  1. Martin English October 16, 2015
  2. Anonymous November 24, 2015
  3. Anonymous December 13, 2016
  4. Nilesh Thakkar December 13, 2016
  5. Nilesh Thakkar December 13, 2016
  6. Nilesh Thakkar December 13, 2016

Leave a Reply