Various Contact Lock Stages in Sitecore xDB

This blog is based on one of the topic covered in my recent presentation in Auckland Sitecore user group where we discussed on Contact lock stages in Sitecore xDB while I presented on xDB and Personalization at Scale.

It was demonstrated for Sitecore 8.2(update 7 instance)

Assuming user has already visited website and the contact data and corresponding Interactions data are already stored in xDB(Collection Database in Mongo DB). So What happens next once same Contact visits website again and how is Contact data passed at different stages and how are the locking and unlocking of Contact data is done at various stages-

Pictorial Representation of the entire Process below-

Stage 1:
Assuming there are no active sessions of the Contact and at this point of time , Contact data is stored in Mongo DB (Collection Database).
Contact data lies in Contact Collection.
User Interactions data lies in Interaction Collection.

Stage 2:
As soon user session starts – Contact data gets locked in collection database and a copy of Contact data is loaded in Shared Session database. So at this Stage Contact data cannot be updated in Collection database.

Stage 3:
On each individual Page request by same user – Contact Data then gets locked in Shared Session database and a copy of Contact Data gets loaded in Tracker object. At this point new user Interaction data is loaded as well in Tracker object. This is the point where Contact data is accessible in the Page Code.

Stage 4:
When the Page request ends – Contact is removed from the Tracker object and at this point now Contact data gets unlocked in Shared Session database.

Stage 5:
And in end of this process – as soon user Session ends – Contact data is removed from Shared Session database and is saved back to Collection database. To be noted: now Contact is unlocked in Collection database (Contact collection in mongo DB).

References : Sitecore documentation and Sugcon EU presentation from Dmytro Shevchenko

Verify Contacts and Page Visits in XDB|Sitecore implementaion

In last couple of weeks I was working to resolve XDB issue in a Sitecore implementation (V8.2).
For one of my analysis I was supposed to verify if all the anonymous contacts and their corresponding page visits are tracked in MongoDB.
After lot of findings and querying against MongoDBcollections came across excellent blog from jonathanrobbins which helped me to figure out the contacts in MongoDB and then I went ahead to query all the corresponding user visits.

Below is the summary of my findings which I would like to document so that its a handy documentation helpful for others and for me too if i want to query MongoDB for similiar verification. 

        1. Sitecore relies on cookies to track anonymous contacts – cookie name:SC_ANALYTICS_GLOBAL_COOKIE
          • Sample sc_analytics value : “43dd8a5d41c347cbb2bb0255da48d783|True”
          • cookie value contains Guid which is ContactId and a bool with a pipe identifier.
          • bool value is set by Sitecore as – IsClassificationGuessed (true or false)
          • for a new visitor its set to false(as can be human or robot). When visitor classification is determined , its set to true and now contact information can be pushed to MongoDB.
        2. For this blog purpose I used my sitecore 8.2 demo site- and visited below pages.
          Page VisitedDate Time
          http://sitecore8/08-06-2019| 22:28
          http://sitecore8/mypage108-06-2019| 22:29
          http://sitecore8/mypage208-06-2019|22:30
          http://sitecore8/mypage308-06-2019|22:31

          Cookie value for SC_ANALYTICS_GLOBAL_COOKIE:959d3c1789754dbcb9ff9757172af6df|True

        3. My intention is to capture behavior as same user returns to demo site later on and visits below Pages.(Technically speaking same user comes back to site in new browser session)
          Page VisitedDate Time
          http://sitecore8/08-06-2019| 22:34 
          http://sitecore8/mypage208-06-2019| 22:37 
          http://sitecore8/mypage308-06-2019| 22:37 
          http://sitecore8/mypage608-06-2019| 22:37 

          Cookie value for SC_ANALYTICS_GLOBAL_COOKIE:959d3c1789754dbcb9ff9757172af6df|True

          • As you see from Date time in # 2 and #3-  I have immediately visited the site before the default session would have cleared. To mimic session clear-  I have one custom admin Page added which just kills the current browser session with a line of C# code.  Session.Abandon();
          • Feel free to grab sessionkill.aspx file and place it in your web-root folder – (Website\sitecore\admin\sessionkill.aspx)
          • I was able to kill current session using- http://sitecore8/sitecore/admin/sessionkill.aspx 
        4. Next I need to verify if my contact and corresponding page visits in both sessions were saved in Mongo DB.
            •  

              • I have used Robo 3T to connect to my mongo DB
                https://kumarsaurabhrajpoot.files.wordpress.com/2019/06/robo-3t-1.3-2019-06-08-23.03.43.png
              • Contact is saved inside Contacts collection. 
              • Now we need to have mongoDB query to fetch data from Contacts Collection and verify it for the above anonymous user.
                • Contact Id from sc_analytics needs to be encoded to be used in the mongoDB query.
                • Grab SC_ANALYTICS_GLOBAL_COOKIE value and get the query using C# snippet available at dotnetfiddle.netcontactRoboQuery
              • Grab the query from output window and execute it against Contacts Collection. Robomongodb.getCollection('Contacts').find({_id:new BinData(3, 'FzydlXWJvE25/5dXFyr23w==')})
                contactCollection
                Contact Collection

                So we can now confirm that our anonymous Contact has been saved in Contact Collection which we have verified based on sc_analytics_global_cookie value.

              • All the user Page visits are saved to Interactions collection. Grab the ContactId from above output and place it in below snippet(just replace the orange text: #Your ContactId based on above output) to generate query for retrieval of user interactions. db.getCollection('Interactions').find({"ContactId" : LUUID("173c9d95-7589-bc4d-b9ff-9757172af6df")})
                Execute Query:
                interactions
                Interaction Collection

                Above output confirms both sessions has been tracked in interaction Collection.

              • Expand each interaction record in the above output window to verify if the corresponding Pages were tracked.
                interactions
                Interaction Collection:Record 1

                Verified:All the Pages user visited in #2 – can be verified from this output.


                interactions
                Interaction Collection:Record 2

                Verified:All the Pages user visited in #3 – can be verified from this output.

So, in this blog I tried penning down my experience of figuring out the corresponding anonymous contact and their corresponding Page visits in Mongo DB.

Happy Sitecoring !!