Event for window close?


#1

Hi,

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:

Test.prototype.initialize = function() {
    window.addEventListener("unload", function(event) {
        alert("test");
    });
};

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. :smiley:


#2

Sounds like you want beforeunload event https://stackoverflow.com/questions/1631959/how-to-capture-the-browser-window-close-event


#3

Hi, thanks for taking the time to respond!

I tried the beforeunload you mentioned, and still can’t get it to fire. I’m not sure if I’m missing something in my set-up, but here’s the standalone project I’m using to test the code: https://playcanvas.com/project/602327/overview/test-html-events

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. :slight_smile:


#4

[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 :slight_smile:

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)


#5

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. :smiley:

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. :slight_smile:


#6

Unfortunately, not the event you are looking for in this case. You will have to hook onto a browser event somehow.

What are you trying to do in terms of functionality? Maybe there is another solution?


#7

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. :slight_smile:


#8

In which case, ‘beforeunload’ should be fine. The event will still fire for the browsers that support it so the analytics hook should execute as well.

It’s just in the case of window.alert, window.confirm etc won’t work in the callback of the unload event but the function still executes.

Should be easy enough to test though with your analytics platform.


#9

Thanks for the help! I was able to get it done. :slight_smile: