Personalize Sitecore DMS using Referring Sites

Sitecore DMS does provide us with a lot of out of the box conditions which helps the content authors to personalize their websites. One of the most requested condition is “Referring Sites”. This post is aimed at writing a custom Sitecore DMS Condition which helps in personalizing the website using Referring Sites.

Create a Condition in Sitecore

  • Navigate to /sitecore/system/Settings/Rules/Conditional Renderings/Conditions folder and insert a new folder under Conditions and call it “Referring Site Conditions”.
  • Now under that “Referring Site Conditions” folder, Insert an item of template /sitecore/templates/System/Rules/Condition, Name the item as “ReferringSitesCondition”.
  • For our use case we will need to fill in only two fields Text (Under Data Section Group) and Type (Under Script Group), We can ignore all the other fields.
  • The first field “Text” is used by Content Author to see the condition in Sitecore: Where the Referring Site has a value that [compares to] [ReferringSite Value].
    One point to note is that anything between the brackets will be selectable by the Content Author.
  • In order to achieve the above goal we need to have the text fields look like this “Where the Referring Site has a value that [operatorid,StringOperator,,compares to] [ReferringSiteValue,,,ReferringSite Value]“.
  • The type field is populated with the [Namespace].[ClassName], [Compiled Assembly] of the corresponding custom code class (See Section Write Code in Visual Studio below).
  • Please refer to this image for reference

Write Code in Visual Studio

  • Please create a class file in Visual Studio and place the following code
using System.Web;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
namespace SampleSite.Code.Conditions
{
    public class ReferringSiteCondition : StringOperatorCondition where T : RuleContext
    {
        #region Properties
        public string ReferringSiteValue { get; set; }
        #endregion

        #region Methods
        protected override bool Execute(T ruleContext)
        {
            bool ReturnValue = false;
            bool FoundExactMatch = false;
            bool FoundCaseInsensitiveMatch = false;
            bool FoundContains = false;
            bool FoundStartsWith = false;
            bool FoundEndsWith = false;

            string RuleValue = this.ReferringSiteValue ?? string.Empty;

            if (!string.IsNullOrWhiteSpace(RuleValue))
            {
                if (HttpContext.Current.Request.UrlReferrer != null)
                {
                   //Populated with Referring Site HOST
                   string referrer = HttpContext.Current.Request.UrlReferrer.Host;

                    if (referrer == RuleValue)
                    {
                        FoundExactMatch = true;
                        FoundCaseInsensitiveMatch = true;
                        FoundContains = true;
                        FoundStartsWith = true;
                        FoundEndsWith = true;
                    }
                    else if (referrer.ToLower() == RuleValue.ToLower())
                    {

                        FoundCaseInsensitiveMatch = true;
                        if (referrer.Contains(RuleValue))
                        {
                            FoundContains = true;
                        }
                        if (referrer.StartsWith(RuleValue))
                        {
                            FoundStartsWith = true;
                        }
                        if (referrer.EndsWith(RuleValue))
                        {
                            FoundEndsWith = true;
                        }
                    }
                    else if (referrer.Contains(RuleValue))
                    {

                        FoundContains = true;
                        if (referrer.StartsWith(RuleValue))
                        {
                            FoundStartsWith = true;
                        }
                        if (referrer.EndsWith(RuleValue))
                        {
                            FoundEndsWith = true;
                        }
                    }
                }
            }

            switch (base.GetOperator())
            {
                case StringConditionOperator.Equals:
                    ReturnValue = FoundExactMatch;
                    break;
                case StringConditionOperator.NotEqual:
                    ReturnValue = !FoundExactMatch;
                    break;
                case StringConditionOperator.CaseInsensitivelyEquals:
                    ReturnValue = FoundCaseInsensitiveMatch;
                    break;
                case StringConditionOperator.NotCaseInsensitivelyEquals:
                    ReturnValue = !FoundCaseInsensitiveMatch;
                    break;
                case StringConditionOperator.Contains:
                    ReturnValue = FoundContains;
                    break;
                case StringConditionOperator.StartsWith:
                    ReturnValue = FoundStartsWith;
                    break;
                case StringConditionOperator.EndsWith:
                    ReturnValue = FoundEndsWith;
                    break;
                default:
                    ReturnValue = false;
                    break;
            }

            return ReturnValue;
        }
        #endregion
    }
}

Test the New Condition

Once you have compiled the code and Build your solution, In Page editor Personalize the component of your choice and look for the new referring site condition (Click Here for Image Reference)

In order for testing in your Local Environments, URL spoofing tool like Spoofy can be very handy

Should you have any questions, Please comment below

4 thoughts on “Personalize Sitecore DMS using Referring Sites

Add yours

  1. A brilliant post, very clear and thorough instructions!

    A nice and logical addition to this condition would be “Where the original Referring Site at session start had a value that [compares to] [ReferringSite Value].”, or what do you think? Could be implemented reusing pretty much everything from your post, and just adding the simple logic of storing the original referrer URL to session (implemented outside this condition class of course) and making the comparisons against that value?

  2. Thank you for your kind comments,

    There are multiple ways to achieve the above requirement of tracking the site at session start.

    Here is a Easy One

    In your VS Solution open Global.asax file, Add the below lines of code
    public void Session_OnStart()
    {
    if(HttpContext.Current.Request.UrlReferrer!=null)
    {
    HttpContext.Current.Session[“refSite”] = HttpContext.Current.Request.UrlReferrer.Host;
    }
    }

    And now in your ReferringSiteCondition.cs File

    Replace these lines
    if (HttpContext.Current.Request.UrlReferrer != null)
    {
    string referrer = HttpContext.Current.Request.UrlReferrer.Host;
    ——-
    ________________________________________________________________________________

    with
    if (HttpContext.Current.Session[“refSite”] != null)
    {
    string referrer = HttpContext.Current.Session[“refSite”].ToString();
    ——

    Build and Compile the Solution.

    Let me know if it works

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: