How do I trigger a profile in Sitecore DMS?

后端 未结 2 1210
孤城傲影
孤城傲影 2021-01-06 14:48

I am looking for a way to allow visitors to select what content they want displayed on the site.

Is there a way to programatically trigger a profile in Sitecore DMS?

2条回答
  •  礼貌的吻别
    2021-01-06 15:16

    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> AnalyticsFilter()
        {
            Dictionary> filter = new Dictionary>();
    
            if (Tracker.CurrentVisit.Profiles.Count() > 0)
            {
                foreach (VisitorDataSet.ProfilesRow row in Tracker.CurrentVisit.Profiles)
                {
                    List keys = new List();
                    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 FilterItemByBehavior(List 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();
            }
        }
    

提交回复
热议问题