Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Personalization First Visit Rule

I am wondering if any of the built in rules for personalization within Sitecore 8.2 fit the requirements of what I am looking for. Using personalization I am trying to make a module on a page show up the first time you visit the page. Any subsequent visits to the page within the current session would not render the module.

I thought the built in rule "where the [specific page] has been visited during the current visit" would work but it doesn't in my scenario. It works if the [specific page] parameter is not the current page but that's not what I need.

It seems as if the visit is recorded before the rule is validated so when the rule is eventually validated it thinks the page has already been visited before when in actuality this may be the first page visit.

Any thoughts other than creating a custom rule? Thanks in advance.

like image 997
mluker Avatar asked Jun 21 '17 20:06

mluker


People also ask

Where can we set all the personalization in Sitecore?

On the Sitecore launchpad, click Experience Editor. Click the content tree icon to open the list of pages, and select the page you want to add personalization rules to. On the page, select the component you want to personalize.

What is personalize rule?

Personalization rules tailor the website experience to every user depending who they are and why they came to your site. They adjust, change, or even hide particular elements of the website.


1 Answers

I don't think there is anything OOTB in Sitecore. You're right - Sitecore first counts the page visit and then executes the rule.

I've created a blog post describing what you need: https://www.skillcore.net/sitecore/sitecore-rules-engine-has-visited-certain-page-given-number-of-times-condition

In shortcut:

  1. Create a new condition item:

    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

  2. Use it to personalize your component with values:

    where the [YOUR_PAGE] page has been visited [IS EQUAL TO] [1] times during the current visit

  3. Create the code:

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");
        Assert.IsNotNull(Tracker.Current.Session.Interaction, "Tracker.Current.Session.Interaction is not initialized");

        Guid pageGuid;

        try
        {
            pageGuid = new Guid(PageId);
        }
        catch
        {
            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;
            default:
                return false;
        }
    }
}
like image 72
Marek Musielak Avatar answered Oct 05 '22 22:10

Marek Musielak