9

I have a task wherein I need to create a functionality that may need to trigger a pattern card programmatically if a specific radio button (where an item is assigned to this with a profile card) is selected and a submit button is clicked.

Can someone enlighten me on how to do this?

I am a newbie on this matter please respect.

Thanks

2
  • You want to change the user pattern programmatically? Jul 27, 2017 at 9:48
  • Hmm I have a predefined pattern cards set for testing and would like to check if it can somehow match on one of it by triggering the pattern card.
    – Jovit Mayo
    Jul 27, 2017 at 9:50

2 Answers 2

8

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

enter image description here

7
  • Forgot to add some comments. Thanks @Hishaam Namooya as I've got an idea for the problem I had. I also found this link useful to the one I am working
    – Jovit Mayo
    Jul 31, 2017 at 8:57
  • How do you get the predefined pattern cards? And get the specific pattern you want from the list? Jul 31, 2017 at 16:08
  • @eat-sleep-code please see updated section Jul 31, 2017 at 16:57
  • @HishaamNamooya I am not following what Pattern A and Pattern B are? I thought "patternName" was the name of the pattern you are trying to boost? Jul 31, 2017 at 21:03
  • @eat-sleep-code please see image attached and you need to retrieve the Pattern Card Item to be able to boost the user pattern. If you see the last piece of code, from the list of pattern cards, it will return the Pattern Card A if the name matches the Pattern A and so on Aug 1, 2017 at 6:46
3

If I am understanding your question correctly, you have a pattern configured in Sitecore and you would like to test to see if the visitor matches that pattern. If that is the case and all you want to do is check to see what pattern the visitor currently matches then the below should be what you are looking for:

// get the name of the pattern
Tracker.Current.Interaction.Profiles[profileName].PatternLabel;
// get the ID of the pattern
Tracker.Current.Interaction.Profiles[profileName].PatternId;

If you're looking to actually change the visitor's scores, then code like @HishaamNamooya's or any other code that modifies scores and ends in the below (called by Hishaam's code as well) will do:

// get the profile - I didn't, but be sure to null-check
var profile = Tracker.Current.Interaction.Profiles[profileName]; 

var updatedProfileKeys = ... do your scoring logic here ...
// updatedProfileKeys is now a Dictionary<string,float> with the updated scores to apply

// update the scores on the profile
profile.Score(updatedProfileKeys); 
// update the pattern based on the scores you updated - this is supposed to be called from Score as well, but in my experience it doesn't always update unless you call it explicitly
profile.UpdatePattern(); 
1
  • So when attempting this I'm getting an error from within Sitecore.Analytics.XConnect.DataAccess.XdbModelExtensions when it tries to resolve with GetProfileKeyByAlias. Any thoughts? (Currently using 9.0.2)
    – Tyler.S
    Jan 7, 2020 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.