Streamlining Customer Data: Merging Independent Contacts in Sitecore
In my project, I had a requirement involving a form that collected the first name, last name, and email address, with the email serving as the unique identifier. Additionally, there was a separate login portal that required a username and password, where the username was the unique identifier. When a user submitted the form, their first name, last name, and email were displayed in the Experience Profile. When the user logged into the portal, their username (in the custom tab), along with their first and last name, was displayed in the Experience Profile.Since the email and username were different identifiers, Sitecore was creating two separate contacts in the Experience Profile. However, the client's requirement was to have a single contact for the same user. The Experience Profile should display the first name, last name, email, and username (in a custom tab) all under one unified contact.
To achieve this functionality I have done the following:
Create Form Contact
- In the form, before creating a contact, you can check if 'userIdentifier' exists in Tracker.Current.Session.Contact?.Identifiers
var loginIdentifier = Tracker.Current.Session.Contact?.Identifiers?.FirstOrDefault(x => x.Source == 'userIdentifier')?.Identifier;
- In the form, create a new contact if it doesn't already exist in xDB.
- Identify the user by their email address, so if the same user submits the form from multiple devices, Sitecore will merge all contacts into a single contact.
- After creating the formContact, check if both the loginIdentifier and formContact are not null. If both are valid, retrieve the login contact from xDB. Once you have both contacts, you can proceed to merge them.
if (loginIdentifier != null && formContact!=null)
{
var loginTrackerIdentifier = new IdentifiedContactReference("loginUser", loginIdentifier);
if (loginTrackerIdentifier != null)
{
var contactExpandOptions = GetContactExpandOption();
Contact loginContact = client.Get(loginTrackerIdentifier, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation));
client.MergeContacts(loginContact, formContact);
client.Submit();
}
}
Create Login Contact
- In the form, before creating a contact, you can check if 'loginIdentifier' exists in Tracker.Current.Session.Contact?.Identifiers
var formIdentifier = Tracker.Current.Session.Contact?.Identifiers?.FirstOrDefault(x => x.Source == 'formIdentifier')?.Identifier;
- In the login portal, create a new contact if it doesn't already exist in xDB.
- Identify the user by their username, so if the same user submits the form from multiple devices, Sitecore will merge all contacts into a single contact.
- After creating the loginContact, check if both the formIdentifier and loginContact are not null. If both are valid, retrieve the form contact from xDB. Once you have both contacts, you can proceed to merge them.
if (formIdentifier != null && loginContact!=null)
{
var formTrackerIdentifier = new IdentifiedContactReference("formUser", formIdentifier);
if (formTrackerIdentifier Identifier != null)
{
var contactExpandOptions = GetContactExpandOption();
Contact formContact = client.Get(loginTrackerIdentifier, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation));
client.MergeContacts(formContact , loginContact);
client.Submit();
}
}
Comments