I have a scenario where I need to programmatically set a Profile to the Tracker.Current.Interaction.Profiles if it is not present.

I have tried to achieve this by using BehaviorProfileConverter but I rolled back the code. The reason of rollback is because I fear it will cost me in terms of performance as it makes use of Reflection to create objects but also, this will need to run each time.

The below snippet is from the Sitecore.Analytics.Pipelines.InitializeInteractionProfile.CopyProfiles and it is the BehaviorProfileConverterBase which makes use of Reflection

BehaviorProfileConverterBase profileConverterBase = BehaviorProfileConverterBase.Create();

IList<ProfileData> profileDataList = (IList<ProfileData>)new List<ProfileData>();

foreach (IBehaviorProfileContext profiles in behaviorProfiles.Profiles)
    ProfileData profileData = profileConverterBase.Convert(profiles);


Any ideas how to set the Profile programmatically?



While we're at it, I'm going to explain the life cycle of profile objects in xDB.

  1. When a new contact is created, its BehaviorProfiles property doesn't have any objects.
  2. During a session, some profile objects may be added to the current interaction.
  3. On session end, profile data will be copied from the interaction to the contact.
  4. Between sessions, profile data persists in the "Behavior Profiles" facet of the contact document in the Collection database.
  5. In a new session, profile data is loaded as a part of the contact and is copied to a new interaction.
  6. Go to step 2.

The take-home point here is that normally you should add new profiles through the interaction API. Later on, this data will be automatically copied to the contact before it is stored in MongoDB.

Adding a profile

  1. Make sure that the profile exists in Tracker.DefinitionItems.Profiles["YOUR PROFILE NAME"]. This basically means that the profile should be present under this path:

    /sitecore/system/Marketing Control Panel/Profiles
  2. Simply access the profile like this:

    Profile profile = Tracker.Current.Interaction.Profiles["YOUR PROFILE NAME"];

    This will create a profile object if it doesn't exist in the interaction yet. Then you can work with this profile as you normally would.

| improve this answer | |
  • Great one. It worked. I always thought that using Tracker.Current.Interaction.Profiles["YOUR PROFILE NAME"] will cause an error if not present – Hishaam Namooya Jun 9 '17 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.