Esempio n. 1
0
        /// <summary>
        /// Scores the content item for the current site visitor, based on the personalisation groups associated with it.
        /// </summary>
        /// <param name="groupMatchingService">The group matching service</param>
        /// <param name="pickedGroups">List of IPublishedContent items that are the groups you want to check against.</param>
        /// <returns>True if content should be shown to visitor</returns>
        private static int ScoreForVisitor(IGroupMatchingService groupMatchingService, IList <IPublishedContent> pickedGroups)
        {
            if (!pickedGroups.Any())
            {
                // No personalisation groups picked or no property for picker, so we score zero
                return(0);
            }

            return(groupMatchingService.ScoreGroups(pickedGroups));
        }
Esempio n. 2
0
        /// <summary>
        /// Determines if the content item should be shown to the current site visitor, based on the personalisation groups associated with it.
        /// </summary>
        /// <param name="groupMatchingService">The group matching service</param>
        /// <param name="pickedGroups">List of IPublishedContent items that are the groups you want to check against.</param>
        /// <param name="showIfNoGroupsDefined">Indicates the response to return if groups cannot be found on the content</param>
        /// <returns>True if content should be shown to visitor</returns>
        private static bool ShowToVisitor(IGroupMatchingService groupMatchingService, IList <IPublishedContent> pickedGroups, bool showIfNoGroupsDefined = true)
        {
            if (!pickedGroups.Any())
            {
                // No personalisation groups picked or no property for picker, so we return the provided default
                return(showIfNoGroupsDefined);
            }

            return(groupMatchingService.MatchGroups(pickedGroups));
        }
        private static bool MatchesGroups(this UmbracoHelper helper, IGroupMatchingService groupMatchingService, string[] groupNames, PersonalisationGroupDefinitionMatch matchType)
        {
            var groupsRootFolder = GetGroupsRootFolder(helper);

            if (groupsRootFolder == null)
            {
                return(false);
            }

            var groups = GetGroups(groupsRootFolder);

            return(groupMatchingService.MatchGroupsByName(groupNames, groups, matchType));
        }
        /// <summary>
        /// Adds an extension method to UmbracoHelper to calculate a hash for the current visitor for all visitor groups
        /// </summary>
        /// <param name="helper">Instance of UmbracoHelper</param>
        /// <param name="groupMatchingService">The group matching cache</param>
        /// <param name="appCaches">Provides access to the runtime cache</param>
        /// <param name="personalisationGroupsRootNodeId">Id of root node for the personalisation groups</param>
        /// <param name="cacheUserIdentifier">Identifier for the user to use in the cache key (likely the session Id)</param>
        /// <param name="cacheForSeconds">Length of time in seconds to cache the generated personalisation group hash for the visitor</param>
        /// <returns>Has for the visitor for all groups</returns>
        public static string GetPersonalisationGroupsHashForVisitor(
            this UmbracoHelper helper,
            IGroupMatchingService groupMatchingService,
            AppCaches appCaches,
            Guid personalisationGroupsRootNodeId,
            string cacheUserIdentifier,
            int cacheForSeconds)
        {
            var personalisationGroupsRootNode = helper.Content(personalisationGroupsRootNodeId);

            if (!personalisationGroupsRootNode.IsDocumentType(AppConstants.DocumentTypeAliases.PersonalisationGroupsFolder))
            {
                throw new InvalidOperationException(
                          $"The personalisation groups hash for a visitor can only be calculated for a root node of type {AppConstants.DocumentTypeAliases.PersonalisationGroupsFolder}");
            }

            return(GetPersonalisationGroupsHashForVisitor(helper, groupMatchingService, appCaches, personalisationGroupsRootNode, cacheUserIdentifier, cacheForSeconds));
        }
        /// <summary>
        /// Adds an extension method to UmbracoHelper to calculate a hash for the current visitor for all visitor groups
        /// </summary>
        /// <param name="helper">Instance of UmbracoHelper</param>
        /// <param name="groupMatchingService">The group matching cache</param>
        /// <param name="appCaches">Provides access to the runtime cache</param>
        /// <param name="personalisationGroupsRootNode">Root node for the personalisation groups</param>
        /// <param name="cacheUserIdentifier">Identifier for the user to use in the cache key (likely the session Id)</param>
        /// <param name="cacheForSeconds">Length of time in seconds to cache the generated personalisation group hash for the visitor</param>
        /// <returns>Has for the visitor for all groups</returns>
        public static string GetPersonalisationGroupsHashForVisitor(
            this UmbracoHelper helper,
            IGroupMatchingService groupMatchingService,
            AppCaches appCaches,
            IPublishedContent personalisationGroupsRootNode,
            string cacheUserIdentifier,
            int cacheForSeconds)
        {
            if (personalisationGroupsRootNode == null)
            {
                throw new ArgumentNullException(nameof(personalisationGroupsRootNode));
            }

            var cacheKey = $"{cacheUserIdentifier}-{AppConstants.CacheKeys.PersonalisationGroupsVisitorHash}";

            return(appCaches.RuntimeCache.GetCacheItem(cacheKey,
                                                       () => groupMatchingService.CreatePersonalisationGroupsHashForVisitor(personalisationGroupsRootNode),
                                                       timeout: TimeSpan.FromSeconds(cacheForSeconds)));
        }
 /// <summary>
 /// Adds an extension method to UmbracoHelper to determine if the current site
 /// visitor matches all of a set of personalisation groups.
 /// </summary>
 /// <param name="helper">Instance of UmbracoHelper</param>
 /// <param name="groupMatchingService">The group matching service.</param>
 /// <param name="groupNames">Names of group nodes to match</param>
 /// <returns>True if visitor matches all groups</returns>
 public static bool MatchesAllGroups(this UmbracoHelper helper, IGroupMatchingService groupMatchingService, string[] groupNames)
 {
     return(MatchesGroups(helper, groupMatchingService, groupNames, PersonalisationGroupDefinitionMatch.All));
 }
 /// <summary>
 /// Adds an extension method to UmbracoHelper to determine if the current site
 /// visitor matches a single personalisation group.
 /// </summary>
 /// <param name="helper">Instance of UmbracoHelper</param>
 /// <param name="groupMatchingService">The group matching service.</param>
 /// <param name="groupName">Name of group node to match</param>
 /// <returns>True if visitor matches group</returns>
 public static bool MatchesGroup(this UmbracoHelper helper, IGroupMatchingService groupMatchingService, string groupName)
 {
     return(MatchesGroups(helper, groupMatchingService, new string[] { groupName }, PersonalisationGroupDefinitionMatch.Any));
 }
Esempio n. 8
0
        /// <summary>
        /// Adds an extension method to UmbracoHelper to score the content item for the current site
        /// visitor, based on the personalisation groups associated with the Ids passed into the method
        /// </summary>
        /// <param name="umbraco">Instance of UmbracoHelper</param>
        /// <param name="groupMatchingService">The group matching service</param>
        /// <param name="groupIds">List of group Ids</param>
        /// <returns>True if content should be shown to visitor</returns>
        public static int ScoreForVisitor(this UmbracoHelper umbraco, IGroupMatchingService groupMatchingService, IEnumerable <int> groupIds)
        {
            var groups = umbraco.Content(groupIds).ToList();

            return(ScoreForVisitor(groupMatchingService, groups));
        }
Esempio n. 9
0
        /// <summary>
        /// Adds an extension method to UmbracoHelper to determine if the content item should be shown to the current site
        /// visitor, based on the personalisation groups associated with the Ids passed into the method
        /// </summary>
        /// <param name="umbraco">Instance of UmbracoHelper</param>
        /// <param name="groupMatchingService">The group matching service</param>
        /// <param name="groupIds">List of group Ids</param>
        /// <param name="showIfNoGroupsDefined">Indicates the response to return if groups cannot be found on the content</param>
        /// <returns>True if content should be shown to visitor</returns>
        public static bool ShowToVisitor(this UmbracoHelper umbraco, IGroupMatchingService groupMatchingService, IEnumerable <int> groupIds, bool showIfNoGroupsDefined = true)
        {
            var groups = umbraco.Content(groupIds).ToList();

            return(ShowToVisitor(groupMatchingService, groups, showIfNoGroupsDefined));
        }
Esempio n. 10
0
        /// <summary>
        /// Adds an extension method to IPublishedContent to score the content item for the current site
        /// visitor, based on the personalisation groups associated with it.
        /// </summary>
        /// <param name="content">Instance of IPublishedContent</param>
        /// <param name="groupMatchingService">The group matching service</param>
        /// <returns>True if content should be shown to visitor</returns>
        public static int ScoreForVisitor(this IPublishedElement content, IGroupMatchingService groupMatchingService)
        {
            var pickedGroups = groupMatchingService.GetPickedGroups(content);

            return(ScoreForVisitor(groupMatchingService, pickedGroups));
        }
Esempio n. 11
0
        /// <summary>
        /// Adds an extension method to IPublishedContent to determine if the content item should be shown to the current site
        /// visitor, based on the personalisation groups associated with it.
        /// </summary>
        /// <param name="content">Instance of IPublishedContent</param>
        /// <param name="groupMatchingService">The group matching service</param>
        /// <param name="showIfNoGroupsDefined">Indicates the response to return if groups cannot be found on the content</param>
        /// <returns>True if content should be shown to visitor</returns>
        public static bool ShowToVisitor(this IPublishedElement content, IGroupMatchingService groupMatchingService, bool showIfNoGroupsDefined = true)
        {
            var pickedGroups = groupMatchingService.GetPickedGroups(content);

            return(ShowToVisitor(groupMatchingService, pickedGroups, showIfNoGroupsDefined));
        }