Exceptionless.JavaScript icon indicating copy to clipboard operation
Exceptionless.JavaScript copied to clipboard

Simplify session events and heartbeats

Open srijken opened this issue 9 years ago • 2 comments

It should be as easy as possible to get started with session start/end and heartbeats. I think we should try to achieve one of these situations

  • All I need to do is call setUserIdentity and useSessions (opt-in for session usage)
  • All I need to do is call setUserIdentity (handle everyting automatically

Right now when useSessions is called, the session only really starts when another event comes in. For users that don't do full feature logging, or submit logging in common cases, this might cause the heartbeat plugin to start its interval later than the actual session start; and might event cause sessions to be missed altogether (when no exceptions occur, and nothing else gets logged).

I'm currently working around this by storing the user identity in sessionStorage. Whenever it changes I call submitSessionStart, whenever it changes from a non-null value to another value, I call submitSessionEnd on the old identity. I also manually schedule heartbeats.

Sample code / workaround

    function submitHeartbeat() {
        var client = exceptionless.ExceptionlessClient.default;
        var user = client.config.defaultData['@user'];
        if (user && user.identity) {
            var cachedId = sessionStorage.exceptionlessUserId;
            if (cachedId !== user.identity) {
                sessionStorage.exceptionlessUserId = user.identity;

                // TODO: maybe check this also when user.identity is null                
                if (cachedId) {
                    // user changed from one value to another, call end with the old id
                    client.submitSessionEnd(cachedId)
                }
                // user changed. call start to create a new sessions
                client.submitSessionStart();
            } else {
                // user didn't change -> heartbeat
                client.submitSessionHeartbeat(user.identity);
            }
        }
    }
    setInterval(submitHeartbeat, 30000); // heartbeat every 30 seconds
    submitHeartBeat(); // submit the start event right away

srijken avatar May 12 '16 20:05 srijken

I think we could make this a lot better but it will take some feedback and playing around. I do wish we would store some kind of guid (session storage) or user id (local storage) and when the client loads up look at that and auto start submitting heartbeats? I think that would solve a lot of this, but do we do that once you call useSessions()? The problem with storing a user id or guid is that once the user is authenticated we currently have no way to merge the two sessions. So it would almost have to be two different sessions, or we create a job that updates existing events with new session user (but you might want to see that they were a anon user).

Also, the .NET client has a internal session identifier that gets set to either the user specified session id or the current user and we look at that when submitting. What do we do when the user calls SubmitSessionEnd? Should we clear that and the default user stored in configuration?

Also, when we get to submitting page views, I think a lot of these issues will go away, one shouldn't need to do much to get value out of the system and it's not currently clear to end users that they shouldn't be needing to submit a session start event (only when the user logs in, I know a session might be long lived),

niemyjski avatar May 16 '16 13:05 niemyjski

Did you ever find a better solution for this?

niemyjski avatar Sep 23 '16 21:09 niemyjski

@srijken I think this is much better solved in the new version, please let me know if this is not the case and we can reopen and work to improve this.

I would do the following for session management:

import { Exceptionless } from "@exceptionless/browser";

await Exceptionless.startup((c) => {
  c.apiKey = "MY_API_KEY";
  c.setUserIdentity("12345678", "Blake");
});

await Exceptionless.submitSessionStart();

One could argue we would need to make session end submission easier via browser lifecycle events.

niemyjski avatar Feb 23 '23 01:02 niemyjski