We have migrated from Sitecore 8.2 to Sitecore 10.2, We are facing issue with Personalization set in our login page.
1.User logins using their credential and gets redirected to another application. When this happens we set a custom value. We are using custom facet to store this data. 2.When user logs off he is able to see the expected personalization message. 3.When the same user closes the browser and logins after a day or two, we expect to see the personalization message in our login screen, this is not working as expected.
Following code, we are using to save the facet data,
private static void UpdateContactFacet(UserInfo userInfo)
{
using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
{
try
{
Sitecore.XConnect.Contact contact = null;
ContactManager contactManager = (ContactManager)Factory.CreateObject("tracking/contactManager", true);
if (contactManager != null)
{
string customUserFacetKey = CustomUserInformation.DefaultFacetKey;
if (Tracker.Current.Contact.IsNew)
{
// Save contact to xConnect; at this point, a contact has an anonymous
// TRACKER IDENTIFIER, which follows a specific format. Do not use the contactId overload
// and make sure you set the ContactSaveMode as demonstrated
Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
contactManager.SaveContactToCollectionDb(Tracker.Current.Contact);
// Now that the contact is saved, you can retrieve it using the tracker identifier
// NOTE: Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource is marked internal in 9.0 Initial and cannot be used. If you are using 9.0 Initial, pass "xDB.Tracker" in as a string.
var trackerIdentifier = new Sitecore.XConnect.IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource, Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"));
contact = client.Get(trackerIdentifier, new Sitecore.XConnect.ContactExecutionOptions(new Sitecore.XConnect.ContactExpandOptions()));
}
else
{
var anyIdentifier = Tracker.Current?.Contact?.Identifiers?.FirstOrDefault();
contact = client.Get(new IdentifiedContactReference(anyIdentifier?.Source, anyIdentifier?.Identifier),
new ContactExecutionOptions(new Sitecore.XConnect.ContactExpandOptions(customUserFacetKey)));
}
if (contact != null)
{
var userInfoFacet = contact.GetFacet<CustomUserInformation>(CustomUserInformation.DefaultFacetKey);
if (userInfoFacet == null)
{
userInfoFacet = new userInfo()
{
CustomValues = userInfo?.CustomValues != null ? JsonConvert.SerializeObject(userInfo?.CustomValues) : string.Empty,
UserId = userInfo?.UserId
};
}
else
{
userInfoFacet.CustomValues = userInfo?.CustomValues != null ? JsonConvert.SerializeObject(userInfo?.CustomValues) : string.Empty;
userInfoFacet.UserId = userInfo?.UserId;
}
client.SetFacet(contact, CustomUserInformation.DefaultFacetKey, userInfoFacet);
client.Submit();
// Remove contact data from shared session state - contact will be re-loaded
// during subsequent request with updated facets
contactManager.RemoveFromSession(Tracker.Current.Contact.ContactId);
Tracker.Current.Session.Contact = contactManager.LoadContact(Tracker.Current.Contact.ContactId);
}
}
}
Code used to retrieve the Custom Value:
private IList<UserData> GetCustomValues(Sitecore.Analytics.Tracking.Contact contact)
{
IList<UserData> customValues = null;
try
{
var xConnectFacet = contact?.GetFacet<Sitecore.Analytics.XConnect.Facets.IXConnectFacets>("XConnectFacets");
if (xConnectFacet != null && xConnectFacet.Facets != null && xConnectFacet.Facets.ContainsKey(CustomUserInformation.DefaultFacetKey))
{
CustomUserInformation userData = xConnectFacet.Facets[CustomUserInformation.DefaultFacetKey] as CustomUserInformation;
if (userData != null)
{
customValues = JsonConvert.DeserializeObject<IList<CustomValues>>(userData?.CustomValues);
return customValues;
}
}
catch (Exception ex)
{
}
return customValues;
}
enter code here
How to get the personalization working if user logins later after a day or so? How to retrieve the contact data from the xDB?
Note: I can see the Custom Facet Data getting stored in ContactFacet table. I have added the config to load the value into the session.