I'm trying to logout the current visitor/contact from the website. What I want to achieve is to save all the current information into xDB and then let the visitor to continue on the website as Anonymous. What are the steps to achieve this?
DISCLAIMER: This is possible, yet untested and is a very non-standard way of using xDB. It may potentially lead to strange errors and you'll have no idea where they are coming from or how to fix them. Use at your own risk.
End the session
First, you'll need to end the current session:
This will make ASP.NET start from scratch by allocating a new session on the user's next request. The current session will be ended and the current interaction will be saved to the collection DB.
Unlock the contact
Note that, although the xDB contact will be saved to the xDB after the previous step, it will not be immediately unlocked in the collection database. There will be a certain time period before the xDB shared session expires. If you want that to happen immediately, try using the following code:
Guid contactId = Tracker.Current.Contact.ContactId; // ... defer ... ContactManager contactManager = Factory.CreateObject("tracking/contactManager", true) as ContactManager; contactManager.SaveAndReleaseContactToXdb(contactId);
Make sure that you don't execute this code directly in a page request, as xDB's request and session end pipelines will expect the contact to still be present in the shared session. So, to be on the safe side, you'll need to defer the execution of
SaveAndReleaseContactToXdb until ~30 seconds later.
Unset the contact cookie
Finally, you'll need to make sure that the user is not recognized as the same contact on the next request. The recognition is done based on the cookie
SC_ANALYTICS_GLOBAL_COOKIE that holds, among other values, the ID of the contact. You can invalidate this cookie by setting its expiration date in the past:
HttpCookie contactCookie = new HttpCookie("SC_ANALYTICS_GLOBAL_COOKIE"); contactCookie.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(contactCookie);