Sitecore xDB – flush data to MongoDB

When debugging Sitecore xDB issues, it is a pain that data is not written to MongoDB directly, but you have to wait for your session to end.

The legend said that you could either set the session timeout to 1 minute, or call Session.Abandon() to write to MongoDB. None of this have ever worked for me, until my colleague Lasse Pedersen found the final, bullet-proof way of having the xDB session data written to MongoDB.

Create a page with the following contents, and call it when you wish to flush data from xDB to MongoDB:

Sitecore.Analytics.Tracker.Current.EndTracking();
HttpContext.Current.Session.Abandon();

We will all pray to the debugging gods that this fix will work in every scenario.

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#, General .NET, Sitecore 8 and tagged , , , . Bookmark the permalink.

5 Responses to Sitecore xDB – flush data to MongoDB

  1. nickhillstc says:

    When testing something similar I found sometimes Sitecore would abort the commitSession pipeline due to either thinking I was a robot or having no pages (IgnoreVisitsWithoutPages).

    I’ve not had issues with the minute timeout but would happily speed up the wait with the example above :)

    Like

  2. jammykam says:

    Interestingly EndVisit() was temperamental for me, but Session.Abandon() worked :)

    With regards to robots, update the setting to false to not detect:

    Like

  3. Stefan Sluijter says:

    Hello Brian,

    We use FlushContactToXdb2 and seems to be flawless so far.

    public Contact Flush()
    {
    return this.contactManager?.FlushContactToXdb2(this.Contact);
    }

    Let me know if this works out the same =)

    Like

  4. Pingback: Sitecore MongoDB – What do we know about a contact? | Brian Pedersen's Sitecore and .NET Blog

  5. Örvar says:

    You are the man Brian!

    Like

Leave a comment

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