internal static bool MatchGroups(IList <IPublishedContent> pickedGroups)
        {
            // Package is disabled, return default
            if (UmbracoConfig.For.PersonalisationGroups().DisablePackage)
            {
                return(true);
            }

            // Check each personalisation group assigned for a match with the current site visitor
            foreach (var group in pickedGroups)
            {
                var definition = group.GetPropertyValue <PersonalisationGroupDefinition>(AppConstants.PersonalisationGroupDefinitionPropertyAlias);
                if (IsStickyMatch(definition, group.Id))
                {
                    return(true);
                }

                var matchCount = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

                // If matching any and matched at least one, or matching all and matched all - we've matched one of the definitions
                // associated with a selected personalisation group
                if ((definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                    (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count()))
                {
                    MakeStickyMatch(definition, group.Id);
                    return(true);
                }
            }

            // If we've got here, we haven't found a match
            return(false);
        }
        /// <summary>
        /// Scores the content item for the current site visitor, based on the personalisation groups associated with it.
        /// </summary>
        /// <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(IList <IPublishedContent> pickedGroups)
        {
            if (!pickedGroups.Any())
            {
                // No personalisation groups picked or no property for picker, so we score zero
                return(0);
            }

            // Check each personalisation group assigned for a match with the current site visitor
            var score = 0;

            foreach (var group in pickedGroups)
            {
                var definition = group.GetPropertyValue <PersonalisationGroupDefinition>(AppConstants.PersonalisationGroupDefinitionPropertyAlias);
                if (IsStickyMatch(definition, group.Id))
                {
                    score += definition.Score;
                }

                var matchCount = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

                // If matching any and matched at least one, or matching all and matched all - we've matched one of the definitions
                // associated with a selected personalisation group
                if ((definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                    (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count()))
                {
                    MakeStickyMatch(definition, group.Id);
                    score += definition.Score;
                }
            }

            return(score);
        }
        /// <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="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(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);
            }

            // Check each personalisation group assigned for a match with the current site visitor
            foreach (var group in pickedGroups)
            {
                var definition = group.GetPropertyValue <PersonalisationGroupDefinition>(AppConstants.PersonalisationGroupDefinitionPropertyAlias);
                if (IsStickyMatch(definition, group.Id))
                {
                    return(true);
                }

                var matchCount = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

                // If matching any and matched at least one, or matching all and matched all - we've matched one of the definitions
                // associated with a selected personalisation group
                if ((definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                    (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count()))
                {
                    MakeStickyMatch(definition, group.Id);
                    return(true);
                }
            }

            // If we've got here, we haven't found a match
            return(false);
        }
Exemplo n.º 4
0
        internal static int ScoreGroups(IList <IPublishedContent> pickedGroups)
        {
            // Package is disabled, return default
            if (PersonalisationGroupsConfig.Value.DisablePackage)
            {
                return(0);
            }

            // Check each personalisation group assigned for a match with the current site visitor
            var score = 0;

            foreach (var group in pickedGroups)
            {
                var definition = group.GetPropertyValue <PersonalisationGroupDefinition>(AppConstants.PersonalisationGroupDefinitionPropertyAlias);
                if (GroupMatchingHelper.IsStickyMatch(definition, group.Id))
                {
                    score += definition.Score;
                    continue;
                }

                var matchCount = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

                // If matching any and matched at least one, or matching all and matched all - we've matched one of the definitions
                // associated with a selected personalisation group
                if ((definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                    (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count()))
                {
                    GroupMatchingHelper.MakeStickyMatch(definition, group.Id);
                    score += definition.Score;
                }
            }

            return(score);
        }
        /// <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="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, IPublishedContent personalisationGroupsRootNode,
                                                                    string cacheUserIdentifier, int cacheForSeconds)
        {
            Mandate.ParameterNotNull(personalisationGroupsRootNode, "personalisationGroupsRootNode");

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

            return((string)UmbracoContext.Current.Application.ApplicationCache.RuntimeCache
                   .GetCacheItem(cacheKey,
                                 () =>
            {
                var groups = personalisationGroupsRootNode.Descendants(AppConstants.DocumentTypeAliases.PersonalisationGroup);
                var sb = new StringBuilder();
                foreach (var group in groups)
                {
                    var definition = group.GetPropertyValue <PersonalisationGroupDefinition>(AppConstants.PersonalisationGroupDefinitionPropertyAlias);
                    var matchCount = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);
                    var matched = ((definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                                   (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count()));

                    if (sb.Length > 0)
                    {
                        sb.Append(",");
                    }

                    sb.AppendFormat("{0}={1}", group.Name, matched);
                }

                return sb.ToString().GetHashCode().ToString();
            }, timeout: TimeSpan.FromSeconds(cacheForSeconds)));
        }
        /// <summary>
        /// Adapted IsMatch from https://github.com/AndyButland/UmbracoPersonalisationGroups/blob/master/Zone.UmbracoPersonalisationGroups/ExtensionMethods/PublishedContentExtensions.cs
        /// </summary>
        public bool VisitorInSegment(string segmentKey)
        {
            var helper  = new UmbracoHelper(UmbracoContext.Current);
            var segment = helper.TypedContent(segmentKey);

            if (segment == null)
            {
                return(false); //something went wrong, we didn't find the segment!
            }

            var definition = segment.GetPropertyValue <PersonalisationGroupDefinition>(AppConstants.PersonalisationGroupDefinitionPropertyAlias);

            if (IsStickyMatch(definition, segment.Id))
            {
                return(true);
            }

            var matchCount = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

            // If matching any and matched at least one, or matching all and matched all - we've matched one of the definitions
            // associated with a selected personalisation group
            if ((definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count()))
            {
                MakeStickyMatch(definition, segment.Id);
                return(true);
            }


            // If we've got here, we haven't found a match
            return(false);
        }
Exemplo n.º 7
0
        public static void AppendMatchedGroupDetailToVisitorHashString(StringBuilder sb, PersonalisationGroupDefinition definition, string name)
        {
            var matchCount = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);
            var matched    = (definition.Match == PersonalisationGroupDefinitionMatch.Any && matchCount > 0) ||
                             (definition.Match == PersonalisationGroupDefinitionMatch.All && matchCount == definition.Details.Count());

            if (sb.Length > 0)
            {
                sb.Append(",");
            }

            sb.AppendFormat("{0}={1}", name, matched);
        }
        public void PersonalisationGroupMatcher_CountMatchingDefinitionDetails_WithDefinitonForMatchAll_AndMatchesAll_ReturnsCount()
        {
            // Arrange
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.All,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    TestHelpers.Definitions.MatchingDayOfWeekDefinition(),
                TestHelpers.Definitions.MatchingTimeOfDayDefinition(),
                }
            };

            // Act
            var result = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

            // Assert
            Assert.AreEqual(2, result);
        }
        public void PersonalisationGroupMatcher_CountMatchingDefinitionDetails_WithDefinitonForMatchAny_AndMatchingFirst_ReturnsShortCutCount()
        {
            // Arrange
            var definition = new PersonalisationGroupDefinition
            {
                Match   = PersonalisationGroupDefinitionMatch.All,
                Details = new List <PersonalisationGroupDefinitionDetail>
                {
                    Definitions.MatchingDayOfWeekDefinition(),
                Definitions.NonMatchingDayOfWeekDefinition(),
                Definitions.MatchingTimeOfDayDefinition(),
                }
            };

            // Act
            var result = PersonalisationGroupMatcher.CountMatchingDefinitionDetails(definition);

            // Assert
            Assert.AreEqual(1, result);
        }