5

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?

1 Answer 1

4

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:

Session.Abandon();

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);
5
  • That's great - Thanks. It is working except when I logged in with Facebook - I reckon that there is a config saying to autologin (Will have a look issue separately). Just a quick (dummy) question - if I shouldn't do this in a page request, when should I do it? Thanks once again
    – Snapper
    Commented Nov 25, 2016 at 8:05
  • 1
    @Snapper Facebook authentication has its own cookie. You'll need to log out of Facebook using the API of the module you're using for Facebook logins. Commented Nov 25, 2016 at 8:09
  • 1
    @Snapper In regard to when to run the contact releasing code—first of all, you don't have to run it. The contact will be released by itself sooner or later, and I don't see any problems with it staying locked for a while. But if you want to unlock it ASAP, just spawn a thread that will sleep for 30 seconds and then execute the code. Remember to save the contact ID in the page request and then use it in the thread. Commented Nov 25, 2016 at 8:11
  • Makes sense. Will try that approach instead. By default, I reckon that the contact will be release after something like 20 min? Cheers @Dmytro Shevchenko
    – Snapper
    Commented Nov 25, 2016 at 8:17
  • @Snapper yes, the timeout is ASP.NET Session Timeout + 1 minute, so by default it'll be 21 minutes after the last access. Commented Nov 25, 2016 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.