Swati Gupta
Swati Gupta

Reputation: 735

Personalization in SItecore

I want to display some different sublayout if user has visit some page more than 2 times so I want to use this rules "where the visit no. compares to number" but I do not have idea how I can use it? I tried to add this rule and replace "number" to 2 but it is not working.

Upvotes: 2

Views: 698

Answers (2)

Jonathan Robbins
Jonathan Robbins

Reputation: 2047

As Marek Musielak said, Where the visit no. compares to .. related to visit to the site and not an individual page.

I had a look in the Sitecore API, its Tracker namespace and the closest property I can find to individual page view count is VisitPageIndex but decompiling the code and checking in MongoDB shows that is just the index of the page was viewed for that visit to the site so this won't work for you.

Looking in MongoDB there are no properties to store page views but it does store the Pages viewed for Interactions so you could write a custom rule counting the number of times that page is in the Pages array

e.g.

int pageViewed = Tracker.Current.Session.Interaction.Pages.Count(p => p.Item.Id.Equals(yourPageId))

An alternative if you don't want to write custom is to change your approach a bit inline with how Sitecore personalisation scan work out of the box.

You'll want to use or create profile keys in the Marketing Centre e.g. 'Brand Aware'. Assign your new profile key to the page in question and assign it a score e.g. 10. This means that each time a user visits this page they will be given a 10 points in 'Brand Aware'.

Now for the personalisation bit. Create a new personalisation rule on the existing sublayout using 'where the value of the specific profile key compares to specific value' set it to hide if the score is greater than or equal to 20. Create another to display your new sublayout if the value is greater than or equal to 20.

I've wrote a blog about this if you need more info

Upvotes: 2

Ian Graham
Ian Graham

Reputation: 3216

As Marek said this is not possible with the condition you are using. However you could adjust the Rule condition to achieve this by looking at the VisitPageIndex for the page.

public class ContactVisitPageIndexCondition<T> : OperatorCondition<T> where T : RuleContext
{
    public int No
    {
        get;
        set;
    }

    public ID PageGUID 
    {
        get;
        set;
    }

    public ContactVisitPageIndexCondition()
    {
    }

    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");
        Assert.IsNotNull(Tracker.Current.Session.Interaction, "Tracker.Current.Session.Interaction is not initialized");
        int contactVisitPageIndex  =  Tracker.Current.Session.Interaction.Pages.SingleOrDefault(p => p.Item.Id == PageGUID).VisitPageIndex;
        switch (base.GetOperator())
        {
            case ConditionOperator.Equal:
            {
                return contactVisitPageIndex == this.No;
            }
            case ConditionOperator.GreaterThanOrEqual:
            {
                return contactVisitPageIndex >= this.No;
            }
            case ConditionOperator.GreaterThan:
            {
                return contactVisitPageIndex > this.No;
            }
            case ConditionOperator.LessThanOrEqual:
            {
                return contactVisitPageIndex <= this.No;
            }
            case ConditionOperator.LessThan:
            {
                return contactVisitPageIndex < this.No;
            }
            case ConditionOperator.NotEqual:
            {
                return contactVisitPageIndex != this.No;
            }
        }
        return false;
    }
}

Upvotes: 2

Related Questions