Recently I come across the requirement to do personalization based on UK postcodes. The format of UK postal codes is very well structured. It consists of two alphanumeric codes called “Outward code” and “Inward code”.
Outward code consists of 2 to four-letter defining area and district whereas Inward code consists of three letters defining sector and unit.

Now for complete flexibility in this personalization criteria, I have taken two input field with the option to add wildcard char “*”
The parameters are
- OutwardCode
- InwardCode
The combination of these parameters gives us options to do personalization as described in the following table
Outward Code | Inward Code | Behavior |
M1 | 4WB | Personalized content for customer on this postcode only (Same street) |
SW1W | 0* | Personalized content for customers on the same area, district but different units (So probably customer from nearby streets) |
M1 | 4* | Personalized content for customers on the same area, district but different units (So probably customers from nearby streets) |
M1 | * | Personalized content for customers whose postcode starts with M1 (Manchester city center area)Personalized content for customers in the Manchester area |
M* | * | Personalized content for customers in the Manchester area |
Getting Customer location.
There are multiple ways to get your customer (or visitor) postcode. The best way is if a customer is a registered customer and you have an address in a profile.
Another option is to get visitor’s geocoordinates (visitor’s latitude and longitude) and after that do reverse geocoding to get address and postcode from latitude and longitude. Once we have a postcode, we can use it to apply this personalization criterion.
In this example, I have a hard coded customer postcode.
Create Personalization criteria
UkPostcodeCriterionSettings Class:
public class UkPostcodeCriterionSettings : CriterionModelBase
{
[Required]
public string OutwardCode { get; set; }
[Required]
public string InwardCode { get; set; }
public override ICriterionModel Copy()
{
return ShallowCopy();
}
}
UkPostcodeCriterion Class:
[VisitorGroupCriterion(
Category = "Geolocation",
DisplayName = "UK Postal Codes",
Description = "Personalization criteria based on customer's UK address")]
public class UkPostcodeCriterion : CriterionBase<UkPostcodeCriterionSettings>
{
public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
{
//user location
var visitorlocation = GetVisitorPostcode();
// Wildcard in OutwardCode
if (Model.OutwardCode.Contains("*"))
{
var nOutwardcode = Model.OutwardCode.Replace("*", "");
return visitorlocation.OutwardCode.StartsWith(nOutwardcode);
}
// Wildcard in InwardCode
if (Model.InwardCode.Contains("*"))
{
var nInwardcode = Model.OutwardCode.Replace("*", "");
return visitorlocation.OutwardCode == Model.OutwardCode && visitorlocation.OutwardCode.StartsWith(nInwardcode);
}
// no Wildcard
if (!Model.OutwardCode.Contains("*") && !Model.InwardCode.Contains("*"))
{
return visitorlocation.OutwardCode == Model.OutwardCode && visitorlocation.InwardCode == Model.InwardCode;
}
return false;
}
private Postcode GetVisitorPostcode()
{
//ToDo: Get Customer postcode
var postcode = "M2 2AN";
var userLocation = new Postcode();
var codes = postcode.Split(new[] {' '});
if (codes.Any() && codes.Length == 2)
{
userLocation.OutwardCode = codes[0].ToUpper();
userLocation.InwardCode = codes[1].ToUpper();
}
return userLocation;
}
}
Source Code: The source code of this Personalisation criteria can be downloaded from this link.