Referrer-Search Dynamic Content

blog header image

As many of you may know, a Dynamic Content Element a day keeps the static web sites away :-)  Here’s another I just did today: Referrer-Search. Once again I find myself in personalization mode. This idea is old. In fact I remember a former company I worked for trying to do a similar thing, so all credits go to them – you know who you are :-)

The idea is also simple: Whenever you arrive at a page on an EPiServer CMS site from Google (or another global search engine), and the page has this dynamic content element somewhere on it, the dynamic content will pick up the search words used on the global search, fire off a local search for the same words – and list the best couple of results in a box somewhere on the page. “Why is that useful?” I hear you cry. Well, because one of the major problems most websites face is the fact that many of the visitors coming from global search bounce off the site right away. This means a lost potential customer, and at the same time it might even affect your rank on google if you have too many people hit the ol’ back button. As my colleague @jacobkhan puts it, “Google works in mysterious ways”.

Imagine that someone were to search for “mortgage rates” and they click a link to YourBank. Unfortunately that link went to a boring article on your website (first mistake) on the development of mortgages rates in the early half of the 18th century. Wouldn’t you at least like to offer them a link box that knew they were interested in mortgage rates and which would show them links to your current mortgage rates, as well as links to becoming a customer? Perhaps a search banner with a campaign you are currently doing on mortgage rates for new customers? It would sure beat them bouncing back to the uncertain world of competition, I think.

In this example I’ve used the built-in search in EPiServer CMS, but the idea can be applied just about anywhere and with any vendor. The code is quite simple, and based on a UserControl that I used DCPlugin to convert to a Dynamic Content element.

It looks like this (in this case I arrived on the website after a search for “apple microsoft” (don’t ask how that got me to this local test page). 

ASCX is simple as can be – easy to customize:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ReferrerSearch.ascx.cs"
    Inherits="EPiServer.Research.DynamicContent.ReferrerSearch" %>
<asp:Panel runat="server" ID="MainPanel" Visible="false">
    <EPiServer:SearchDataSource runat="server" MaxCount="5" PageLink='<%# EPiServer.Core.PageReference.StartPage %>'
        SearchQuery="" ID="SearchSource">
    </EPiServer:SearchDataSource>
    <h3>
        <%# Title %></h3>
    <asp:Repeater runat="server" DataSourceID="SearchSource">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
        <ItemTemplate>
            <li>
                <EPiServer:Property runat="server" PropertyName="PageLink" />
            </li>
        </ItemTemplate>
        <FooterTemplate>
            </ul></FooterTemplate>
    </asp:Repeater>
</asp:Panel>

And there’s just a little bit of logic in the code behind:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Web.WebControls;
using System.Text.RegularExpressions;
using System.Linq;
using EPiServer.Research.DynamicContent;
 
namespace EPiServer.Research.DynamicContent
{
    [DCPlugin(DisplayName="Referer Search",Url="~/DynContent/ReferrerSearch.ascx")]
    public partial class ReferrerSearch : EPiServer.UserControlBase
    {
        public string Title { get; set; }
 
        public int ResultCount { get; set; }
 
        
 
        protected void Page_Load(object sender, EventArgs e)
        {
            MainPanel.Visible = false;
            //Get referer, and extract query (q=xxxx)
            if (Request.UrlReferrer != null)
            {
                if (Request.UrlReferrer.Query != null)
                {
                    Match m = Regex.Match(Request.UrlReferrer.Query, @"[qQ]\=(?<query>[^&\r\n]+)$?");
                    if (m != null)
                    {
                        //We have a winner!
                        MainPanel.Visible = true;
                        SearchSource.MaxCount = ResultCount;
                        SearchSource.PageLink = PageReference.StartPage;
                        SearchSource.SearchQuery = Server.UrlDecode(m.Groups["query"].Value);
                        SearchSource.Filter += new FilterEventHandler(SearchSource_Filter);
                        this.DataBind();
                    }
                }
            }
        }
 
        void SearchSource_Filter(object sender, EPiServer.Filters.FilterEventArgs e)
        {
            var p=e.Pages.Where(pd => pd.PageLink == CurrentPage.PageLink).FirstOrDefault();
            if (p != null) e.Pages.Remove(p);
        }
    }
}

 

Enjoy!

Recent posts