I am writing some code to track user behavior on my website in order to improve conversion rate by determining how users are interacting with my website.
This requires me to send some events like "Leaving Website" to Google analytics inside the unbeforeunload
handler. I have a few questions about the firing of this event.
target="_blank"
on my website?Are there any other instances where it fires?
Thanks.
onbeforeunload will fire right before a webpage is 'unloaded' in the current tab (By navigating to a new page), or the current tab is closed. This means:
- Does it fire if user switches tabs without opening a new link?
No
- Does it fire if a new tab is opened after users clicks a link with target="_blank" on my website?
No
- Does it fire if a users visits some other page of my own website?
Yes
- Does it fire on page reload?
Yes
The current version of analytics.js should (Using sendBeacon()) dispatch events to GA as a POST request, which should be honoured even when closing tabs or the browser.
However, I wouldn't be so sure that 'Leaving website' is appropriate. Perhaps something more like 'Leaving page'.