Sitecore Rules Engine - Has Visited Certain Page Given Number Of Times Condition

Posted 22 June 2017 by Marek Musielak

sitecore rules engine

I've recently saw a question on StackOverflow where one asked how to „make a module on a page show up the first time you visit the page“. There is a Sitecore built in rule "where the [specific page] has been visited during the current visit" but the code which counts the page visits is called before the rule is executed, so if you try to use "except where the [specific page] has been visited during the current visit", it will always be false on that specific page, even when you open it for the first time. I haven't found any OOTB solution, so I've created my own condition: HasVisitedCertainPageGivenNumberOfTimesCondition.

First thing you have to do is to create new condition item. I navigated to /sitecore/system/Settings/Rules/Definitions/Elements/Visit/Page was Visited item, duplicated it and called it Page was visited given number of times. Then you need to set:

  • Text: where the [PageId,Tree,root=/sitecore/content,specific] page has been visited [OperatorId,Operator,,compares to] [Index,Integer,,number] times during the current visit
  • Type: YourAssembly.YourNamespace.HasVisitedCertainPageGivenNumberOfTimesCondition,YourAssembly

sitecore rules engine user has visited certain page given number of times during current visit condition

Next step is to create code for this class:

public class HasVisitedCertainPageGivenNumberOfTimesCondition<T>
                : OperatorCondition<T> where T : RuleContext
    public string PageId { get; set; }
    public int Index { get; set; }

    protected override bool Execute(T ruleContext)
        Assert.ArgumentNotNull(ruleContext, "ruleContext");
        Assert.IsNotNull(Tracker.Current, "Tracker.Current is not initialized");
        Assert.IsNotNull(Tracker.Current.Session, "Tracker.Current.Session is not initialized");
            "Tracker.Current.Session.Interaction is not initialized");

        Guid pageGuid;

            pageGuid = new Guid(PageId);
            Log.Warn(string.Format("Could not convert value to guid: {0}", PageId), GetType());
            return false;

        var pageVisits = Tracker.Current.Session.Interaction.GetPages()
            .Count(row => row.Item.Id == pageGuid);
        switch (GetOperator())
            case ConditionOperator.Equal:
                return pageVisits == Index;
            case ConditionOperator.GreaterThanOrEqual:
                return pageVisits >= Index;
            case ConditionOperator.GreaterThan:
                return pageVisits > Index;
            case ConditionOperator.LessThanOrEqual:
                return pageVisits <= Index;
            case ConditionOperator.LessThan:
                return pageVisits < Index;
            case ConditionOperator.NotEqual:
                return pageVisits != Index;
                return false;

And that's it. You can use it now with where the [YOUR_PAGE] page has been visited [IS EQUAL TO] [1] times during the current visit values and it will make sure your component is displayed only when you open this particular page once.

Another situation when you can use this condition is when user has opened particular page multiple times - maybe the reason is that they're trying to find something on your website and have some difficulties with it. Maybe it would be a good idea to create a "Need some help" component and display it using where the [YOUR_PAGE] page has been visited [IS GREATER THAN] [3] times during the current visit condition.

Thank you for reading. I hope you enjoyed. Visit my Sitecore blog for more Sitecore related articles.

Comments? Find me on or Sitecore Chat