Sitecore Forms: A Step-by-Step Guide to Assigning Pattern Cards

Personalisation is the process of tailoring a website's content and user experience to an individual visitor based on their demographics, behaviour, and other relevant data. With Sitecore Personalise, businesses can create targeted and relevant content for each user, improving engagement and conversion rates.

Sitecore Personalisation works by collecting user data through various sources such as website behaviour, third-party integrations, or user input. This data is then used to create user profiles, which are used to segment visitors into different groups based on their characteristics and behaviour. Using these segments, businesses can create personalised experiences for each group. Such as showing different content or offers based on their interests, location, or past interactions with the website.

Sitecore provides a range of personalisation features, including rule-based personalisation, where content is personalised based on predefined rules and conditions, machine learning-based personalisation, where algorithms analyse user behaviour to predict and deliver personalised content, A/B testing, and segmentation, to help businesses create effective and relevant personalised experiences for their customers.

Sitecore Profiles and Pattern Cards are two important features of Sitecore Personalisation that enable businesses to deliver personalised experiences to their website users.

Sitecore Profiles are user profiles that store information about a website visitor's behaviour, interests, and preferences. Sitecore collects data about a visitor's behaviour on the website, such as pages visited, forms completed, search terms used, and downloads made, and uses this information to build a profile for that visitor. Sitecore Profiles can be used to create rules and conditions for personalising content and experiences for that visitor.

Pattern Cards, on the other hand, are pre-defined sets of rules that businesses can use to segment visitors based on their behaviour, interests, and preferences. Pattern Cards define a set of attributes and values that can be used to identify visitors who match the criteria of the pattern. For example, a business may create a Pattern Card for visitors who have viewed more than three pages on the website and downloaded a specific resource. This Pattern Card can then be used to personalise content and experiences for this segment of visitors.

By using Sitecore Profiles and Pattern Cards, businesses can create targeted and personalised experiences for visitors based on their behaviour and preferences, which can lead to increased engagement, conversion, and retention rates.

In this article we are going to go through the steps a developer can follow to assign Pattern Cards for visitors who submit a Sitecore form and how this can be used to personalise the content displayed on site pages based on a user's assigned Pattern Card.

In order to be able to implement this, we have created a custom submit action that matches the selected answer with a specific Pattern Card defined previously using a new configuration item, which links each answer in the answers list with a corresponding Pattern Card.

Step-by-Step Guide

1. In the Sitecore Marketing Control Panel, define a Profile and a list of Pattern Cards
2. Add new three templates for configuration items under

:/sitecore/templates/Feature/Forms

Form Settings Folder: This folder template will be used as a container for all forms settings.

Pattern Card Action Settings: Using this template we can define the form question ID that will be matched with this configuration item.

Pattern Card Action Mapping: Using this template we can define the mapping values between the defined Pattern Cards and the question answers values.

3. Add a new custom submit action:

/sitecore/system/Settings/Forms/Submit Actions

Use the below code for the custom submit action:

								
Public class AssignPatternCardAction : AnalyticsActionBase
{
	public AssignPatternCardAction(ISubmitActionData submitActionData) : base(submitActionData)
	{
	}

	protected override bool Execute(AssignPatternActionData data, FormSubmitContext formSubmitContext)
	{
		Assert.ArgumentNotNull(formSubmitContext, "formSubmitContext");

		if (data == null || !(data.ReferenceId != Guid.Empty))
		{
			// submit action was not configured properly     
			Log.Error(string.Format("AssignPatternCardAction failed: 
			Submit action settings were not configured properly for form {0}.", formSubmitContext.FormId), this);
			return false;
		}

		Item item = Context.Database.GetItem(new ID(data.ReferenceId));
		if (item == null || !item.IsDerived(Templates.PatternCardActionSettings.ID))
		{
			// submit action was not configured properly    
			Log.Error(string.Format("AssignPatternCardAction failed: 
			Submit action settings for form {0} point to an invalid item.", formSubmitContext.FormId), this);
			return false;
		}

		string questionFieldName = item[Templates.PatternCardActionSettings.Fields.FormQuestion];
		if (questionFieldName == null)
		{
			// settings item was not configured properly
			Log.Error(string.Format("AssignPatternCardAction failed: Submit action settings were not configured properly. 
			Form Question on settings item {0} is not assigned or does not exist in this context.", data.ReferenceId), this);
			return false;
		}

		//questionId = questionId.Replace("{", string.Empty).Replace("}", string.Empty).ToLower();
		IViewModel fieldModel = formSubmitContext.Fields.FirstOrDefault(f => f.Name == questionFieldName);
		if (fieldModel == null)
		{
			// no submitted field matched the configured form question          
			Log.Error(string.Format("AssignPatternCardAction failed: 
			Configured question Field Name {0} does not exist form {1}", 
			questionFieldName, formSubmitContext.FormId), this);
			return false;
		}

		string answer = string.Empty;
		try
		{
			answer = fieldModel.GetValue();
		}
		catch (Exception ex)
		{
			// could not get value of submitted field
			Log.Error("AssignPatternCardAction failed: could not parse value of submitted field.", ex, this);
			return false;
		}

		var mappings = item.GetChildren();
		var mapping = mappings.FirstOrDefault(m => m[Templates.PatternCardActionMapping.Fields.MappedValue]
		.ToLower().Trim() == answer.ToLower().Trim());
		if (mapping == null)
		{
			// could not find a mapping setting item for the submitted value
			// log warning and continue execution without failure
			Log.Warn(string.Format("AssignPatternCardAction failed: 
			No mapping found for answer {0} of question {1}", answer, questionFieldName), this);
			return true;
		}

		string patternCardId = mapping["Pattern Card"];
		Item matchedPatternCard = Context.Database.GetItem(patternCardId);

		try
		{
			if (Tracker.Current == null)
			{
				Tracker.StartTracking();

			}
			//Log.Warn(string.Format("AssignPatternCardAction checkup: matchedPatternCard= {0} ", matchedPatternCard.Name), this);
			Profile profile = Tracker.Current.Interaction.Profiles[matchedPatternCard.Parent.Parent.Name];
			//Log.Warn(string.Format("AssignPatternCardAction checkup: profile= {0} ", profile.ProfileName), this);

			Data.Fields.XmlField xmlData = matchedPatternCard.Fields["Pattern"];
			XmlDocument xmlDoc = xmlData.Xml;

			XmlNodeList parentNode = xmlDoc.GetElementsByTagName("key");
			Dictionary scores = new Dictionary();

			foreach (XmlNode childrenNode in parentNode)
			{
				if (childrenNode.Attributes["value"].Value != "0")
				{
					double keyValue;
					double.TryParse(childrenNode.Attributes["value"].Value, out keyValue);
					scores.Add(childrenNode.Attributes["name"].Value, keyValue * 3);
				}
			}


			profile.Score(scores);
			profile.UpdatePattern();
			Log.Warn(string.Format("AssignPatternCardAction succeed: updated pattern= {0} ", profile.PatternLabel), this);
		}
		catch (Exception ex)
		{
			Log.Error(string.Format("AssignPatternCardAction failed: 
			Could not update pattern for card {0}", patternCardId), ex, this);
			return false;
		}
		return true;
	}
}
							
4. After this you will need to create a Sitecore form, adding a radio button list field with dynamic source, reading from a predefined list of values. As shown below we created a custom radio button list field to have an icon displayed with each radio button.
5. On the form add a submit button and link it to your custom submit action (Assign Pattern Cards)
6. Save the form and publish. Then add the form to a page and test, once a user clicks the submit action then the selected answer will be mapped through the settings to a Pattern Card and assigned to the user.

After that, you will be able to see this Pattern Card assigned to the user in the experience profile within the profiling tab.

7. Now, we can use this assigned Pattern Card to personalise page content.

We hope you find this guide useful, if you have any questions feel free to reach out, we'd love to have a chat!

— Messo Jazairi

Want more?

Back to the Blog

NEW Switch have been awarded a Good Design Award in Digital Design.
Read More

Switch acknowledges the Traditional Custodians of the lands where we live, learn, and work and we pay our respects to their Elders past and present and emerging.

Sydney
Level 5
100 Market Street
Sydney NSW 2000
Melbourne
Level 22
120 Spencer St
Melbourne VIC 3000
New Zealand
11-19 Customs Street West
Auckland 1010

Singapore
8 Cross Street
Singapore, 048424

ABN 15 120 572 750   ·   © 2024 Switch

Subscribe Now

Sign up to our newsletter to gain access to this page.

No thanks