EPi Personalization: Showing Block based on page not visited

Vote:
 

Hi,

I am implementing Personalization on blocks and I came accross one criteria:

Show perticular block unless user does not visit "My Pages" page. So, for the first time block will appear for user unless he visits "My Page" page and then it will disappear.

How can I do that. I tried to apply Visited Page criteria combining No Of visits with Less than 1, it didnt woeked.

//Sanket Mahimkar

#172042
Nov 24, 2016 11:43
Vote:
 

Hi Sanket,

You can create a custom criterion like this:

[VisitorGroupCriterion(
        Category = "Site Criteria",
        Description = "Matches if the user didn't visit a certain page",
        DisplayName = "Did not visit a page",
        LanguagePath = "/shell/cms/visitorgroups/criteria/unvisitedpages")]
public class UnvisitedPagesCriterion : CriterionBase<ViewedPagesModel>
{
    private const string SessionKey = nameof(UnvisitedPagesCriterion);

    public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
    {
        var viewedPages = GetViewedPages(httpContext);

        if (viewedPages == null) return true;

        return !viewedPages.Contains(Model.GetViewedPageLink());
    }

    public override void Subscribe(ICriterionEvents criterionEvents)
    {
        criterionEvents.VisitedPage += criterionEvents_VisitedPage;
    }

    public override void Unsubscribe(ICriterionEvents criterionEvents)
    {
        criterionEvents.VisitedPage -= criterionEvents_VisitedPage;
    }

    private void criterionEvents_VisitedPage(object sender, CriterionEventArgs e)
    {
        var pageLink = e.GetPageLink();
        if (PageReference.IsNullOrEmpty(pageLink) || pageLink != Model.GetViewedPageLink())
        {
            return;
        }
        AddViewedPage(e.HttpContext, pageLink);
    }

    private static void AddViewedPage(HttpContextBase httpContext, PageReference pageLink)
    {
        var pageReferenceSet = GetViewedPages(httpContext);
        if (pageReferenceSet == null)
        {
            pageReferenceSet = new HashSet<PageReference>();
            if (httpContext.Session != null)
            {
                httpContext.Session[SessionKey] = pageReferenceSet;
            }
        }
        if (pageReferenceSet.Contains(pageLink))
        {
            return;
        }

        pageReferenceSet.Add(pageLink);
    }

    private static HashSet<PageReference> GetViewedPages(HttpContextBase httpContext)
    {
        return httpContext.Session?[SessionKey] as HashSet<PageReference>;
    }
}

And then define a custom visitor group like this:

Hope this helps!

#172056
Edited, Nov 24, 2016 14:20
Vote:
 

...or flip logic and assign visitor group to those that visited My Pages => MyPagesVisitor. 

Then create the group NotMyPagesVisitor by checking if you NOT belong to MyPagesVisitor.

Invert visitor groups membership

Dejans solution is more fun though :)

#172156
Edited, Nov 24, 2016 15:55
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.