Sitecore 8 : How to Programatically associate visitor data into Contact Card

Hello Folks,

Disclaimer — All Information in this Blog is using Sitecore 8 Technical Preview. So, it may change upon final release

In my previous post about Sitecore Experience Profile, we saw how we can know everything about a visitor ranging from their visits to the events which they triggered during that visit. In this blog post I will walk you through the process of identifying an anonymous user and tying the user visits to a contact record which will then enable us to look into the contact’s history of visits and know more about our contacts.

Sitecore’s API provides various methods to identify a visitor and tie up various details about a visitor to a contact record.

How to identify an Anonymous Visitor ?

This is one of the major requirements in the process of identifying an anonymous visitor, Below are some of the ways where we can try to capture information about our visitor through various user actions, some of them are listed below

  • Login in to the Website (Sign Up, Using Social mediums such as Facebook, Twitter etc)
  • Submitting an Form on the website (Contact Us, Feedback, etc..)
  • Subscribing for newsletter, articles, Whitepapers using their email address

All of the above options will need the user to atleast give us their Email Address and from there on we can tie the unique email address to the visitor.

Sitecore out of the box gives us a number of facets to store additional information about the contact. This data then helps us to fill the Sitecore Experience Profile.

These facets are configured here {root}\Website\App_Config\Include\Sitecore.Analytics.Model.config

         <facets>
            <facet name="Personal" contract="Sitecore.Analytics.Model.Entities.IContactPersonalInfo, Sitecore.Analytics.Model" />
            <facet name="Addresses" contract="Sitecore.Analytics.Model.Entities.IContactAddresses, Sitecore.Analytics.Model" />
            <facet name="Emails" contract="Sitecore.Analytics.Model.Entities.IContactEmailAddresses, Sitecore.Analytics.Model" />
            <facet name="Phone Numbers" contract="Sitecore.Analytics.Model.Entities.IContactPhoneNumbers, Sitecore.Analytics.Model" />
            <facet name="Picture" contract="Sitecore.Analytics.Model.Entities.IContactPicture, Sitecore.Analytics.Model" />
          </facets>

Store visitor’s Email Address to the Contact Record

we will be using the Emails facet to store the current visitor’s email address to the current contact record, In the Emails facet you can specify any number of email address by providing an Key to each email address, As you can see below I have provided the Key called Work Email to store the visitors work email address. you can also associate a particular email address as Preferred Email address which will then be used to identify an contact using a email address.

Sitecore allows you to search a contact in the Experience Profile using the Email address

    
   if (Sitecore.Analytics.Tracker.Current.Contact != null)
          {
              var emailFacet = Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");
              //Check if an work email address already exists for the contact
              if (!emailFacet.Entries.Contains("Work Email"))
              {
                  IEmailAddress email = emailFacet.Entries.Create("Work Email");
                  email.SmtpAddress = "Email Address of the Visitor";
                  emailFacet.Preferred = "Work Email";
              }
          }

Store visitor’s Personal Information to the Contact Record

we will be using the Personal facet to store the current visitor’s personal information to the current contact record.Sitecore allows you to search a contact in the Experience Profile using any of the name attributes such as FirstName, LastName, Surname.

    
    if (Sitecore.Analytics.Tracker.Current.Contact != null)
          {
              var personalFacet = Tracker.Current.Contact.GetFacet<IContactPersonalInfo>("Personal");
              personalFacet.Title = "Name_Title";
              personalFacet.FirstName = "First_Name";
              personalFacet.MiddleName = "Middle_Name";
              personalFacet.Surname = "Last_Name";
              personalFacet.Gender = "Gender";
              personalFacet.JobTitle = "Job_Title";
              personalFacet.BirthDate = new DateTime(1983, 01, 01);
          }

Store visitor’s Phone Numbers to the Contact Record

we will be using the Phone Numbers facet to store the current visitor’s phone numbers to the current contact record, In the Phone Numbers facet you can specify any number of phone numbers by providing an Key to each phone number, As you can see below I have provided the Key called Cell Phone to store the visitors cell phone number . you can also associate a particular number as Preferred phone number.

    
 if (Sitecore.Analytics.Tracker.Current.Contact != null)
 {
    var phoneFacet = Tracker.Current.Contact.GetFacet<IContactPhoneNumbers>("Phone Numbers");
    if (!phoneFacet.Entries.Contains("Cell Phone"))
      {
        IPhoneNumber cellPhone = phoneFacet.Entries.Create("Cell Phone");
        cellPhone.CountryCode = "001";
        cellPhone.Number = "612-000-0000";
        cellPhone.Extension = "8455";
        phoneFacet.Preferred = "Cell Phone";
       }
 }

Store visitor’s Image / Picture to the Contact Record

we will be using the Picture facet to store the current visitor’s picture to the current contact record,

    
 if (Sitecore.Analytics.Tracker.Current.Contact != null)
          {
                MediaItem mediaItem = Sitecore.Context.Database.GetItem("Guid of the media item");
                var stream = mediaItem.GetMediaStream();
                var memoryStream = new MemoryStream();
                if (stream != null) stream.CopyTo(memoryStream);
                var pictureFacet = Tracker.Current.Contact.GetFacet<IContactPicture>("Picture");
                pictureFacet.Picture = memoryStream.ToArray();
                pictureFacet.MimeType = mediaItem.MimeType;
          }

How to Identify a Known Visitor ?

Once we have stored information about the visitor using the above methods , Sitecore does allow us to identify known visitors using the Sitecore.Analytics.Tracker.Current.Session.Identify Method. This method accepts a string variable which is  the visitor’s email address.

 
 var identifiers = Sitecore.Analytics.Tracker.Current.Contact.Identifiers;
 if (identifiers.IdentificationLevel == ContactIdentificationLevel.Anonymous)
 {
 Sitecore.Analytics.Tracker.Current.Session.Identify("Email Address of the visitor");
 }

Summary

Using Sitecore’s new API, it is now easier for developers to associate various information about the visitor and tie them to the contact card, This will immensely help marketers to understand their visitors better. Once we are able to associate a visitor using email address, we could also update the record using Information from CRM systems such as Salesforce or Dynamics.

Should you have any questions or comments, Please comment to this post or reach me at sjain@horizontalintegration.com

5 thoughts on “Sitecore 8 : How to Programatically associate visitor data into Contact Card

Add yours

  1. Thanks for you post, i am using the posted code but this is overwriting the existing Contact Record with the new Personal data. Plz let me know how to Add new Personal as separate record.

  2. Sitecore identifies a potential contact by the Sitecore Analytics Global Cookie. So in most probability since you are using the same browser it is getting tracked by the same cookie.
    I would recommend you to either use incognito/private mode in the browser or clear your cookies and then retry again

  3. This example was great and between it and another article you wrote i was able to get what i needed done. Now my question is, in the users profile, under the details tab is there a way to show the value of the session.identify()? In my case when users login, I’m setting the session.identify() to a userID# and then I set their name values. All is working but I would also like to show that userID# on the details tab but I’d prefer not to have to create a custom facet since its already being stored. Help is appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: