Extending the xDB Contact Facets


Recently I had a client who needed to be able to personalize content based on a number of attributes stored in their CRM Dynamics system. This is, I think, a common request. Replace Crm system with custom database, Salesforce, or any other third party system that contains important information and just about any marketer sees value in this.

Luckily, Sitecore gives us a straight forward development path in order to get to this information and store it in Sitecore. If you are starting this fresh, I highly recommend looking at the Sitecore Data Exchange Framework. At this point, I have only had a chance to play around with it and am excited about what possibilities lie ahead with this product. If, however, you need to manually write some code in order to get custom information into the xDB here are the steps.

 

Create and Implement the contract.

IMehProfileFacet : IFacet
    {
        string ContactId { get; set; }
        List EmploymentHistory { get; set; }
    }

public class MehProfileFacet : Facet, IMehProfileFacet 
    {
        private const string FIELD_CONTACT_ID = "ContactId";
        private const string FIELD_EMPLOYMENT_HISTORY = "EmploymentHistory";

        public MehProfileFacet()
        {
            base.EnsureAttribute(FIELD_CONTACT_ID);
            base.EnsureAttribute<>>(FIELD_EMPLOYMENT_HISTORY);
        }

        public string ContactId
        {
            get { return base.GetAttribute(FIELD_CONTACT_ID); }
            set { base.SetAttribute(FIELD_CONTACT_ID, value); }
        }


        public List EmploymentHistory
        {
            get { return base.GetAttribute<>>(FIELD_EMPLOYMENT_HISTORY); }
            set { base.SetAttribute<>>(FIELD_EMPLOYMENT_HISTORY, value); }
        }
    }

Update your config patch file with your new contact facet.

Populate your custom facet at the appropriate time, in my case post authentication.

            

if (!Sitecore.Context.User.IsAuthenticated || !Tracker.Current.IsActive)
                return;

            try
            {                
                var currentConact = new MehContact();

				//This associates the current user with the record in xDB
                Tracker.Current.Session.Identify(Sitecore.Context.User.Name);
                
                currentConact.ProfileData = currentConact.Context.Contact.GetFacet("Meh Data");

                //Call to whatever web serivce or 3rd part daya provider
                var mehProfile = GetEmployeeInfo();

                if (mehProfile == null)
                    return;

                currentConact.ProfileData.ContactId = mehProfile.ContactId;
                currentConact.ProfileData.EmploymentHistory = mehProfile.EmploymentHistory.ToList();

				//This will populate the username and email fields within the Expierence Profile
                IContactPersonalInfo contactPersonalInfo = currentConact.Contact.GetFacet("Personal");

                if (string.IsNullOrEmpty(contactPersonalInfo.FirstName))
                    contactPersonalInfo.FirstName = memberPortalProfile.FirstName;
                if (string.IsNullOrEmpty(contactPersonalInfo.Surname))
                    contactPersonalInfo.Surname = memberPortalProfile.LastName;

                var contactEmailInfo = currentConact.Contact.GetFacet("Emails");
                contactEmailInfo.Entries.Create("Work");
                var email = contactEmailInfo.Entries["Work"];
                email.SmtpAddress = user.EmailAddress;
                contactEmailInfo.Preferred = "Work";

            }
            catch (Exception ex)
            {
               var logger = log4net.LogManager.GetLogger("Sitecore.Diagnostics.MEH");
                logger.Error("Failed to update xDB card: " + ex.Message);
            }

Kill your session and check your mongo db instance for your new attributes. You should see them under analytics > collections > Contacts. Note: you may need to delete your asp session cookies in order to get this information to show up. 

Next: creating personalization rules around these new attributes.

Categories: Sitecore
Tags: MongoDB, Sitecore;

SEARCH ARTICLES