Enabling Sitecore DMS to reuse existing Visitor records across devices
This post looks at how DMS stores visitor records, and this can be manipulated so that returning visitors across devices can reuse the same Visitor records.
When a user visits your site for the first time, the Sitecore DMS will create a brand new Visitor record in the analytics database, and link this to a new Visit record. This visit record will be updated for the duration of that session with relevant information, such as pages visited, profiles triggered etc. Sitecore will also place two cookies onto the clients computer, one for the session, and a global cookie that will help Sitecore identify the visitor between sessions. These cookies are called
SC_ANALYTICS_SESSION_COOKIE, and SC_ANALYTICS_GLOBAL_COOKIE respectively.
Once the first visit has been registered, your Visits Table will look like this (obviously ignoring any previous visits):
The VisitorId matches the new Visitor record created in the Visitors table, which will look something like this:
Once that session ends, then the visit will be closed (this is all handled via the VisitEnd pipeline, which is invoked by the SessionEnd pipeline). If the user then returns to the site, Sitecore will check for the SC_ANALYTICS_GLOBAL_COOKIE cookie, and if found, will use the existing Visitor record to attribute to a new Visit Record. So to simulate this, delete the existingSC_ANALYTICS_SESSION_COOKIE (shown through Chrome below);
Now when you refresh the site, Sitecore will not find a session cookie, and so will create a new visit. However, as it can find a Global cookie, it can detect who you are, and so the same VisitorId will be used as the previous visit.
So this is great, Sitecore can now continue attributing visits to the same user so long as it can find the Global cookie. The Global cookie is set to expire 10 years in the future, so as long as the user does not delete that cookie, and continues to browse from the same device, then Sitecore will continue to recognise them.
However, if the user browses to the site from another device (mobile, different browser etc), then obviously the cookie won't be there and Sitecore will create a new Visitor record. Assuming that the user hasn't authenticated against the site (and so no Sitecore User exists for them) then there is unfortunately no way to detect that user on another device. What about when the user does log in or becomes known in some other way? Unfortunately Sitecore still does not recognise this as the same visitor, and so the new Visitor record will endure.
If you do want to use the same Visitor record however, then this is possible, so long as the user has registered (or in some way has a corresponding Sitecore User profile). When the user registers with the site a Sitecore User profile is created. Of course at this point you should also be using the 'ExternalUser' field to set the user name (or other identifiable attributes), which would allow you to query for the user across different visitor records as well.
Tracker.Visitor.ExternalUser = Sitecore.Context.GetUserName();
So once the user registers, the Visitor record will be updated as such:
To allow Sitecore to recognise a returning authenticated user, and set the Visitor record appropriately, you simply need to recognise the visitor first, and then replace the automatically generated Session cookie with a new Session cookie containing the previous visitor details. To do this, within the Log In method for example, you can see if the user has an existing profile (using the user name), and if so set the cookie;
var visitor = VisitorManager.GetVisitorByExternalUser(domainUser);
if (visitor == null)
Log.Info("First visit of " + domainUser, this);
Log.Info("Returning visit of " + domainUser, this);
if(visitor.VisitorId != Guid.Empty)
With this method in place, when the user firsts visits your site from a different device, a new visitor record will be created to match their anonymous status (as they are not known at this point).
Once they log in however, a new Visit record will be created with the previous Visitor details set. This does mean that everything for the user post log in will be treated as a new visit, but using the previous VistorId.
Hi, I am trying to find out if there is a way to see what is the number of active users on the website. I used to have a query on SC6.1 with OMS with Sessions table. Now its a visits table. and below query gives me over 9000 visits. Which is suspicious to me for the fact I know that our usage is 300/400 users at a second. SELECT COUNT(*) FROM [Visits] WHERE [StartDateTime] > DATEADD(SECOND, -(5), GETDATE()) I tried EndDateTime and StateChanged field And I am not sure if they are good candidates to consider as they also reports around the same number.