1. Create custom profile list to update Interaction programmatically
List<ProfileDetails> profileDetails = new List<ProfileDetails> profileDetails();
2. Check whether analytics tracking is enable or not.
if (!Tracker.Enabled)
{
return;
}
if (!Tracker.IsActive)
{
Tracker.StartTracking();
}
3. Get the user interaction from the user context
var interaction = Tracker.Current.Session.Interaction;
4. Update Analytics Tracking Profile
foreach (var profile in profileDetails)
{
var profileName = profile.CategoryName;
var profileScores = profile.ProfileScores;
if (!string.IsNullOrEmpty(profileName) && (profileScores?.Any() ?? false))
{
updated = true;
Profile xConnectProfile;
if (interaction.Profiles.ContainsProfile(profileName))
{
xConnectProfile = interaction.Profiles[profileName];
}
else
{
var profiles = new List<ProfileData>
{
new ProfileData(profileName)
};
interaction.Profiles.Initialize(profiles);
xConnectProfile = interaction.Profiles[profileName];
}
xConnectProfile.Score(profileScores);
xConnectProfile.UpdatePattern();
}
}
5. Update the interaction and persist the changes to contact
if (updated)
{
interaction.AcceptModifications();
try
{
var contact = Tracker.Current.Session.Contact;
var contactManager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as ContactManager;
contactManager?.SaveContactToCollectionDb(contact);
}
catch (Exception ex)
{
Logger.ABCLogger.Error($"CreateOrUpdateProfileData Exception: ", ex);
}
}
No comments:
Post a Comment