I’m working on including analytics in a project, and for the life of me, I can’t seem to be able to find a way to detect if the user has closed the tab where the game is running on. There’s the window.unload function, but I can’t seem to make it work, which I really need for stuff like session length and so on. Is there a trick to making that work? I put the function under the initialization like so:
I also tried the onbeforeunload event and found no joy, as well. Thanks in advance to anyone who could help me out. I’ve been racking Google for an answer for hours already.
I can verify that the browser I’m using supports beforeunload, so I’m not sure if there’s something with the PlayCanvas engine that overrides the behavior for beforeunload. Again, thanks for taking the time to respond.
[quote]
The HTML specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML specification for more details.[/quote]
You may want to check what you can call within the event
Edit: Actually, looking a little deeper, it can get pretty complicated if iframes are involved as well (which they are on PlayCanvas’ published builds on the server)
Okay, I’ll see how to go about doing this with the iframes in mind. I had no idea about the iframes, so that’s very helpful.
As an aside, would you know if there’s a similar functionality/event built-in with the PlayCanvas engine that I can listen to for when the application quits or something? I’m just wondering if there may already be one that I missed, being new to the engine and all.
Again, thanks for the help. At least I have a lead to follow with what I need to do.
I was hoping to track analytics events. Some of those events like session length and time spent on certain areas would have to be sent off to Google Analytics before the tab is closed if ever the user decides to just close the tab outright instead of clicking on a “quit” button or something. Ideas would be very much appreciated, if you have alternative methods in mind.