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

[sourcecode language="csharp"]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 }}[/sourcecode]

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