Sitecore Previous Visit Last Page
To finish up this last weeks posts on DMS, I want to flip the table and instead of showing how to manage the Analytic data, show how the data can be used to make decisions on your site. Specifically with returning visitors you might want to learn what the last page was that the user was on before leaving the site. Maybe the user was reviewing a specific product and you want to bring that back to their attention. Or they could have been mid step with filling out a multi page form and you want to redirect them back to this. Possibly you want to report on which end pages have the most return visits or to immediately adjust their profile to give them a better viewing experience. But before we get into the code we should probably talk briefly about how a visitor and visits are handled. When a visitor comes to your site for the very first time a cookie is created called SC_ANALYTICS_GLOBAL_COOKIE . This stays with the visitor when they leave and expires after one year or if the user clears browser cookies. So our global session cookie makes the visitor, and then each visit gets its own session cookie called SC_ANALYTICS_SESSION_COOKIE . This cookie expires when the user closes their browsers. So through these two cookies we can identify a unique visitor and each unique visit they make to the site. If you simply want to grab the last page the code looks like:
VisitorDataSet.VisitsRow prevVisit = Tracker.Visitor.GetVisit(Tracker.CurrentVisit.VisitorVisitIndex - 1, VisitLoadOptions.All);
VisitorDataSet.PagesRow currentPage = prevVisit.GetPage(pageCount);
We simply set the visit to our previous visit by looking at our current visit index and stepping back one. Then we grab the last page by running a page count. Now just gathering the last page might not be good enough. You might be looking for something a bit more logical. Maybe a specific last category for example. In my example below I am looking for the last Sitecore item as well stepping back further if the last page visit cannot be the first page hit. This could be applied to a mutli page form or a workflow through a cart checkout:
VisitorDataSet.VisitsRow prevVisit = Tracker.Visitor.GetVisit(Tracker.CurrentVisit.VisitorVisitIndex - 1, VisitLoadOptions.All);
int pageCount = prevVisit.VisitPageCount;
bool pageFound = false;
VisitorDataSet.PagesRow currentPage = null;
Item pageItem = null;
// get the last page reached, however not all pages are sitecore items so check for that and loop through until found.
while (pageCount > 0 && pageFound == false)
{
currentPage = prevVisit.GetPage(pageCount);
if (Sitecore.Context.Database.GetItem(new ID(currentPage.ItemId)) != null)
{
pageItem = Sitecore.Context.Database.GetItem(new ID(currentPage.ItemId));
if (String.IsNullOrEmpty(pageItem["Decision Parent Page"]) == false)
{
pageItem = Sitecore.Context.Database.GetItem(new ID(pageItem["Decision Parent Page"].ToString()));
}
pageFound = true;
}
pageCount--;
}
if (pageFound)
{
literalTest.Text = String.Format("Page was found: {1}", LinkManager.GetItemUrl(pageItem), pageItem.Name);
}
else
{
literalTest.Text = "No page was found.";
}
So we start with collecting the previous visit. Then we loop through looking for a valid Sitecore item. On finding our first Sitecore item we check to see if it has a link to another item identified by the "Decision Parent Page". If so we link to that page, otherwise we link to the initial page we found. The return actions are pretty basic here, but I could see a lot of potential use for this. Last pages could not only be used for linking / redirecting but also for profiling. Similar to how Amazon shows your previous view of products and suggests other products you could do the same here. You could drill much deeper as well and loop through all the pages of the previous visit to find even more meaningful data or logically problem solve something for the visitor that sticks out from their previous visit. The list goes on!