Wednesday, July 29, 2015

Sitecore Experience Profiles How to Identify Users

In my Sitecore Experience Profiles and Identifying Users Overview post I covered the basics of Experience profiles, along with why it's beneficial to have identify users over anonymous users. In this post I will provide the code needed to actually identify the users - this will claim all of their data and assign it to one named profile.

Creating the user

The initial step is to "create" a user and populate some of the base data we know about them. This may be general information (Name and Title), contact details (especially email address) and even a picture. You would generally perform this action when a user is first defined: such as on registration onto your web site, or on submit of a contact form.
// Store visitors details
if (Sitecore.Analytics.Tracker.Current.Contact != null)
    // Email address
    var emailFacet = Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");

    if (!emailFacet.Entries.Contains("Work Email"))
        IEmailAddress email = emailFacet.Entries.Create("Work Email");
        email.SmtpAddress = txtEmail.Text;
        emailFacet.Preferred = "Work Email";

    // Personal details
    var personalFacet = Tracker.Current.Contact.GetFacet<IContactPersonalInfo>("Personal");
    personalFacet.FirstName = txtFirstName.Text;
    personalFacet.Surname = txtSurname.Text;
    personalFacet.JobTitle = txtRole.Text;
The code above is ensuring there is a current Sitecore analytic tracker and then assigning the details of the user to is. Once this action is complete the user is identified and all of their past/future data (based on cookie/session) is assigned to them.

Identifying a user

If the user visits again in the future, perhaps on a different device, their session will no longer be associated to them. This means that all data will once again be assigned to another anonymous profile, which is no good when you are trying to build the complete picture on a user (their browsing habits, goals reached, campaigns partaken in and so on). Therefore the following code could be run when a user identifies themselves (such as on login).
var identifiers = Sitecore.Analytics.Tracker.Current.Contact.Identifiers;

if (identifiers.IdentificationLevel == ContactIdentificationLevel.Anonymous)
Once this has run, if a match is found, the users data is stored against their experience profile, and the customer insight continues.

No comments:

Post a Comment