1

Just wanted to ask if it's possible to track session length on a Progressive Web Application? Any advice or ideas on how I can track the user's session length?

I'm using Mixpanel library for our analytics/statistics.

I can track the session length only if the user clicks on the logout button, however there are cases wherein the user will just close the app/browser directly causing an issue where I cannot determine when did the user ended their app usage, so it's crucial for me to send a track event to the API that the user closed the app/browser.

Is it possible with the current state of progressive web apps to detect if the app or browser was closed? or not?

Rene Padillo
  • 2,250
  • 4
  • 26
  • 39

1 Answers1

2

You can detect if the page is left via the onunload and onbeforeunload events.

Check out the answer here:
Detect browser or tab closing

I would also check out the Beacon API which will let you send any analytics requests that you need when the user leaves the app:
https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API

Peter Fernandes
  • 307
  • 1
  • 9
  • ah. yes, I also have looked into this solution.However the problem with this is that I'm using an API for session tracking, upon triggering the event the API call will be canceled before it gets to the API server. I was thinking if it's possible to have it as a background call using Service Worker so that even if the browser was closed the API call will still be executing in background to complete the request. – Rene Padillo Jul 05 '18 at 16:56
  • 1
    I think using the Beacon API is going to be the canonical solution here. Maybe you can use it to make a call to your webserver which will subsequently talk to Mixpanel? – Peter Fernandes Jul 05 '18 at 19:47
  • I see. I can try the Beacon API implementation, however I was concerned that it's not yet supported on iOS Safari and we badly needed this to be supported. – Rene Padillo Jul 06 '18 at 04:28