Sitecore xDB Basics — Identifying Users, Contacts, Visitors, Customers

This post references Sitecore 8.1 Update-2 revision 160302

The Experience Database (xDB) is a key element of the Sitecore platform, particularly when you are looking to elevate your solution beyond simple content management requirements. It is used to track how your users (a.k.a. contacts, visitors, customers) interact with your website. Marketers can use this data to understand customer behavior, develop and optimise campaigns, track users through the buying process and measure the success of the website.

On your Sitecore dashboard you may have seen the Experience Profile tile.
Experience Profile Tile When you click this you are presented with a list of the visitors to your website. If you have had a number of visits to your website it will likely contain a large number of rows. Unfortunately they'll probably all be Name "Anonymous" and Email "Unknown". How can we fix this? Why do we want to?

Experience Profile List - unidentified users

Why do we want to identify users?

Identifying users has a number of benefits:

  • Gives useful context to Sitecore Experience Profiles and Experience Analytics reports - this is especially important when individual customers are high value such as in a B2B scenario.
  • If our site has an authenticated section (login) then it brings these users through to Sitecore back-end reports as a marketing user might expect.
  • Provide a single view of a customer across different browser sessions, browsers, devices (computer, phone etc.) and even channels (online, offline etc.).
  • It makes the Sitecore back-end look a whole lot better!

Sitecore Experience Profiles are not the same as your authentication/membership/user store. With xDB and Sitecore contacts we can track and identify both authenticated and non-authenticated users in the same manner.

How do we identify users?

This can be done in a number of ways including as a result of a Web Forms for Marketers (WFFM) submission or in code.

Either way, it will use the following Sitecore API:

// identify the user
Sitecore.Analytics.Tracker.Current.Session.Identify(identifier)  

Here the identifier can be any string which is unique to the user. You would commonly use one of the following:

  • username / login / user ID — if the user has authenticated
  • email address — if the user has offered their email via an interaction such as a subscription signup or a contact form

After we identify a user with the above code it would be reasonable to expect the Experience Profile to update accordingly. Unfortunately this isn't the case! It now just says "Unknown" instead of "Anonymous".

Experience Profile List - Not Updated as expected

This is not what a marketing person would expect when a customer has been "identified". The user has been identified to Sitecore, but Sitecore doesn't make this known to Sitecore back-end users. To update the name and email shown in this list we need more code:

// get the contact
var contact = Sitecore.Analytics.Tracker.Current.Session.Contact;

// get the personal facet
var contactPersonalInfo = contact.GetFacet<Sitecore.Analytics.Model.Entities.IContactPersonalInfo>("Personal");

// set the contact's name
contactPersonalInfo.FirstName = "Brad";  
contactPersonalInfo.Surname = "Curtis";

// get the email facet
var contactEmail = contact.GetFacet<Sitecore.Analytics.Model.Entities.IContactEmailAddresses>("Emails");

// Create an email if not already present.
// This can be named anything, but must be the same as "Preferred" if you want
// this email to show in the Experience Profiles backend. 
if (!contactEmail.Entries.Contains("Home"))  
{
    contactEmail.Entries.Create("Home");
}

// set the email
var email = contactEmail.Entries["Home"];  
email.SmtpAddress = "myemail@bradcurtis.com";  
contactEmail.Preferred = "Home";  

Here we have set a number of out-of-the-box Sitecore contact facets. One key thing to note here is that a Sitecore contact can have multiple associated email addresses. In this example we have created a single email with the label "Home". For the email address to display in the Experience Profile visitors list we must set the Preferred email to this same label.

Now we get what we want!
Experience Profile List - what we want

I've created a test page so that you can play with this code on your own site. Simply drop it into your Sitecore website directory.

xdb-identify.aspx example

comments powered by Disqus