Sitecore 8 and Tracker.Current.Session.Identify – Overriding expired contact session lock for contact id

The new Sitecore 8 Experience Profile is a vital part, yes almost a cornerstone of the new xDB concept.

UPDATE 2018-01-19: Please note that in Sitecore 9, Tracker.Current.Session.Identify have been replaced with Tracker.Current.Session.IdentifyAs. For more information, see Sitecore 9 Tracker.Current.Session.Identify is replaced with Tracker.Current.Session.IdentifyAs.

In xDB, you store information about the current user, anonymous or named, as a Contact in the Experience Profile (stored in the MongoDB).
Any user begins his life as an anonymous user, and the associated Contact object has no identifier, only a key matched in the cookie of the current user.

In order to connect the Contact with a named user, you use the Tracker.Current.Session.Identify(userName) method:

if (!Tracker.IsActive)
  return;
Tracker.Current.Session.Identify("extranet\\user@domain.com");

The method identifies the user@domain.com as a Contact, creates a named Contact if the contact does not exist, or merges the current contact data into one named Contact. Also, the Contact will be locked for this user.

Please note that Tracker.Current.Session.Identify(userName) accepts a string. You can create named contacts with any key. This is great if your named users does not come from Sitecore Users.

But here lies the danger. If you by accident Identifies extranet\anonymous, every anonymous visitor will be merged and locked to the same Contact. And since a contact can only be locked to one session at a time, any subsequent calls to Tracker.Current.Session.Identify will wait, and wait, and wait, … until the previous contact unlocks the Contact.

And the log will be filled with:

Message: Failed to extend contact lease for contact 43188c31-cbe9-4386-8739-c12c3dc049c2

Or even:

2844 18:01:11 ERROR Cannot finish Analytics page tracking
Exception: Sitecore.Analytics.Exceptions.ContactLockException
Message: Failed to extend contact lease for contact 43188c31-cbe9-4386-8739-c12c3dc049c2
Source: Sitecore.Analytics
at Sitecore.Analytics.Tracking.ContactManager.SaveAndReleaseContact(Contact contact)
at Sitecore.Analytics.Pipelines.EndAnalytics.ReleaseContact.Process(PipelineArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Analytics.Pipelines.HttpRequest.EndAnalytics.Process(HttpRequestArgs args)

So it is VERY important that you check that the user identified is not your anonymous user:

// THIS IS BAD!!!
// The user could be extranet\anonymous
if (!Tracker.IsActive)
  return;
Tracker.Current.Session.Identify(Sitecore.Context.User.Name);

// THIS COULD BE A SOLUTION:
if (!Tracker.IsActive)
  return;
if (Sitecore.Current.User.Name.ToLower() == "extranet\\anonymous")
  return;
Tracker.Current.Session.Identify(Sitecore.Context.User.Name);

// OR MAYBE THIS?
if (!Tracker.IsActive)
  return;
if (!Sitecore.Context.User.IsAuthenticated)
  return;
Tracker.Current.Session.Identify(Sitecore.Context.User.Name);

MORE TO READ:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 8, Sitecore and .net and tagged , , , , . Bookmark the permalink.

8 Responses to Sitecore 8 and Tracker.Current.Session.Identify – Overriding expired contact session lock for contact id

  1. kamsarr says:

    If a contact can only be locked to one session at a time, does that mean if I visit and log in on a laptop, then come back without logging out on say an iPad and authenticate that it will block the tablet until the laptop’s session expired?

    Like

  2. briancaos says:

    Almost. If 2 sessions call the Tracker.Identify with the same username at the same time, those users, and the rest of the sessions attached to that contact will wait until one of the calls times out. This will make any page call take 10-20 seconds, even if this page call does not include calling Tracker.Identify.

    Like

  3. Pingback: Sitecore Contacts – Create and save contacts directly to and from xDB (MongoDB) | Brian Pedersen's Sitecore and .NET Blog

  4. Pingback: xDB Reports with Powershell | Jocks to the Core

  5. If Tracker.Current.Session.Identify(identifier value) returns a value does that mean the contact is locked and in session?

    Like

  6. Pingback: Add Contact Id in Interactions for Non-Logged in Visitors – Sitecore Blogs

  7. madhavjoshi says:

    Hey,

    I have a weird scenario as our application is in a transition from vb.net to sitecore8.2 with MVC. I have Sitecore forms authentication and this redirects to a legacy application for session updates and comes back to Homepage which is again in Sitecore. I can see contacts in cms but all show as Anonymous. I am just confused where the above code should go? is Global.asax file right place?

    Like

  8. Pingback: Sitecore 9 Tracker.Current.Session.Identify is replaced with Tracker.Current.Session.IdentifyAs | Brian Pedersen's Sitecore and .NET Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.