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

[sourcecode language=”xml”]
<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>
[/sourcecode]

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

[sourcecode language=”csharp”]

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";
}
}

[/sourcecode]

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.

[sourcecode language=”csharp”]

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);
}

[/sourcecode]

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.

[sourcecode language=”csharp”]

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";
}
}
[/sourcecode]

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,

[sourcecode language=”csharp”]

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;
}

[/sourcecode]

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.

[sourcecode language=”csharp”]

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

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