How do I trigger a profile in Sitecore DMS?

人盡茶涼 提交于 2019-11-30 23:40:37
xoail

I have done something similar on my project. Check out this code sample and let me know if you have any questions. Also, make sure you add profiles to content items too. Call FilterItemByBehavior on a collection of items and it will filter them based on user's past browsing behavior.

 private static Dictionary<string, List<string>> AnalyticsFilter()
    {
        Dictionary<string, List<string>> filter = new Dictionary<string, List<string>>();

        if (Tracker.CurrentVisit.Profiles.Count() > 0)
        {
            foreach (VisitorDataSet.ProfilesRow row in Tracker.CurrentVisit.Profiles)
            {
                List<string> keys = new List<string>();
                foreach (var key in row.Values)
                {
                    if (key.Value >= ResourceHelper.GetInt(new ID(Resources.Settings.AnalyticsProfileSetMinValGuid)))
                        keys.Add(key.Key);
                }
                filter.Add(row.ProfileName, keys);
            }
        }
        if(ResourceHelper.IsTurnedOn(new ID(Resources.Settings.AnalyticsUserProfileEnableSwitch)))
            filter = ApplyUserProfile(filter);
        return filter;
    }


    public static List<Item> FilterItemByBehavior(List<Item> items, int count)
    {
        try
        {
            var filter = AnalyticsFilter();
            foreach (var profile in filter)
            {
                int counter = ResourceHelper.GetInt(new ID(Resources.Settings.AnalyticsProfileTagsFilterMaxGuid));
                if (items.Count <= count) break;
                foreach (string key in profile.Value)
                {
                    if (items.Count <= count || counter == 0) break;
                    items = items.Where(i => (((MultilistField)i.Fields[profile.Key]).GetItems().ToList().Select(x => x.Name).Contains(key))).ToList();
                    counter--;
                }
            }
            return items.Count <= count ? items : items.Take(count).ToList();
        }
        catch (System.Exception ex)
        {
            Sitecore.Diagnostics.Log.Error(ex.Message, ex, new AnalyticsHelper());
            return items.Count <= count ? items : items.Take(count).ToList();
        }
    }
Arnold Zokas

I have received a response from Sitecore support on this question. Here it is:

"If you are using pattern cards for personalzation, then you can use the following code as the event handler for "item selected" event for the dropdown list:"

var profile = Sitecore.Analytics.Tracker.CurrentVisit.GetOrCreateProfile("<Profile Name>");
profile.BeginEdit();
profile.Score("<profile key>",<profile key value you want to set>);
profile.Score("<profile key>",<profile key value you want to set>);
profile.UpdatePattern(); //sets the appropriate pattern based on the current profile keys values you have just set.
profile.EndEdit();

This interferes with automatic profile matching, so I am not sure I want to use this approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!