In order to be able to boost the user pattern, you may use the below code.
public static void BoostUserPattern(Session userSession, string patternName)
{
var patternCards = // Get your predefined pattern cards
var patternCard = // Get the specific pattern you want from the list
if (patternCard == null)
{
return;
}
var profile = Tracker.Current.Interaction.Profiles[patternCard.Parent.Parent.Name];
BoostUserPattern(userSession, patternCard, profile);
}
public static void BoostUserPattern(Session session, Item patternCard, Profile profile)
{
if (patternCard != null && !patternCard.Name.Equals(profile.PatternLabel))
{
Sitecore.Data.Fields.XmlField xmlData = patternCard.Fields["Pattern"];
XmlDocument xmlDoc = xmlData.Xml;
XmlNodeList parentNode = xmlDoc.GetElementsByTagName("key");
var scores = new Dictionary<string, float>();
foreach (XmlNode childrenNode in parentNode)
{
if (childrenNode.Attributes != null)
{
scores.Add(childrenNode.Attributes["name"].Value, 0);
}
}
// Set a score value here
scores[patternCard.Name] = 5;
profile.Score(scores);
profile.PatternId = patternCard.ID.ToGuid();
profile.PatternLabel = patternCard.Name;
UpdateBehaviorProfile(session);
}
}
private static void UpdateBehaviorProfile(Session session)
{
var profileConverterBase = BehaviorProfileConverterBase.Create();
if (session?.Contact == null || Tracker.Current.Interaction == null)
{
return;
}
session.Contact.BehaviorProfiles.RemoveAll();
foreach (var profileName in session.Interaction.Profiles.GetProfileNames())
{
var profile = session.Interaction.Profiles[profileName];
if (!IgnoreInteractionProfile(profile))
{
var matchedBehaviorProfile = profileConverterBase.Convert(profile);
session.Contact.BehaviorProfiles.Add(matchedBehaviorProfile.Id, matchedBehaviorProfile);
}
}
}
private static bool IgnoreInteractionProfile(Profile profile)
{
Assert.ArgumentNotNull(profile, "profile");
return false;
}
When the user clicks on the radio and submit the value, you trigger the method BoostUserPattern(Session userSession, string patternName)
. The code will set the pattern to the user.
UPDATE
You can retrieve the predefined pattern cards by fetching them from Sitecore. Example:
var patternCards = Context.Database.GetItem("Path of your Pattern Container Item").Children;
By using the parameter patternName
, you can perform a query from the list patternCards
. Example:
var patternCard = patternName.ToLower().Equals("Pattern A")
? patternCards.FirstOrDefault(w => w.Name.ToLower().Equals("Pattern A"))
: patternCards.FirstOrDefault(w => w.Name.ToLower().Equals("Pattern B"));
Note that you can already have a list or some values in a config file where you can read from it and the do the comparison.
Pattern A and B are as shown below
