Sitecore 个性化首次访问规则

mlu*_*ker 5 sitecore sitecore-mvc sitecore8

我想知道 Sitecore 8.2 中的任何内置个性化规则是否符合我所寻找的要求。使用个性化,我试图使页面上的模块在您第一次访问该页面时显示。当前会话中对该页面的任何后续访问都不会呈现该模块。

我认为内置规则“当前访问期间已访问[特定页面] ”会起作用,但在我的场景中不起作用。如果[特定页面]参数不是当前页面,它会起作用,但这不是我需要的。

似乎访问是在验证规则之前记录的,因此当规则最终验证时,它认为该页面之前已经被访问过,而实际上这可能是第一次页面访问。

除了创建自定义规则之外还有什么想法吗?提前致谢。

Mar*_*lak 5

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)