Questions tagged [sendbeacon]

33 questions
1
vote
1 answer

proper way of using beforeunload to send a beacon on window close using angular 13

Im trying to send a beacon when a user closes the browser window that they are viewing on my site. I am using Angular 13 and using the window and navigator javascript apis to do it. First I know call back crazy in my init post. Its going to get…
1
vote
0 answers

Why does the navigator.sendBeacon() not follow Refresh header in php?

Navigator.sendBeacon() does not follow Refresh header redirects, unlike regular 3xx server redirects. For example, in PHP: header('Location: https://destination.url',true,3xx); The above server code is followed without any issues by sendBeacon().…
1
vote
0 answers

sendBeacon not working in beforeunload handler

I have the following useEffect hook in a React component: useEffect(() => { const handleUnload = function (event) { navigator.sendBeacon("https://webhook.site/8c3a42ce-c525-4a84-8783-f65175ed1297", JSON.stringify({ "foo": "bar" })); …
Alex
  • 97
  • 1
  • 1
  • 7
1
vote
1 answer

navigator.sendBeacon not called from unload

According to MDN and the specs, navigator.sendBeacon is intended to be called from window unload. Now, it does not seem to work anymore if you close the last tab of your browser, or your entire browser window. Can anyone confirm if this is by…
Casper
  • 53
  • 8
0
votes
0 answers

Is it possible to get the sendBeacon HTTP result?

I am sending data using sendBeacon. Every time I send a beacon, I also know that the user's login session was extended, so I also update a timestamp in a sessionStorage key whenever I send a beacon to the current time. But, sometimes a user returns…
pileup
  • 1
  • 2
  • 18
  • 45
0
votes
0 answers

How to convert navigator.sendBeacon to a fetch call?

I've seen quite a few docs/articles suggesting that fetch with the keepAlive flag is equivalent to sendBeacon. fetch('/track', { method: 'POST', body: getData(), keepalive: true, }); // Same as navigator.sendBeacon('/track',…
Wallace
  • 107
  • 4
0
votes
0 answers

add X-XSRF-TOKEN to the request header of sendBeacon()

I have an Angular application and to send analytics data to the server I defined the service that used the navigator.sendBeacon(). I wanted to add X-XSRF-TOKEN into the request header of navigator.sendBeacon(). But as you know, it's not possible, or…
FarbodKain
  • 229
  • 3
  • 15
0
votes
1 answer

Call API on tab close/browser close in Angular

I'm trying to invoke an API call as soon as the tab or browser closes. For this I'm using 'onBeforeUnload' event. Currently, as soon as the browser is closed, the API is not getting called always. It works 2-3 times out of 10 times. Is there a way…
05_pingpong
  • 77
  • 1
  • 2
  • 13
0
votes
0 answers

Update variable value for onbeforeunload

In my application, I'm trying to use sendBeacon to send data to my remote server. One of the data that I need is how many clicks its been on the page and I'm doing it as follows: var clickNumber = 0; document.addEventListener("mouseup", function ()…
Rubioli
  • 670
  • 6
  • 21
0
votes
0 answers

Using navigator.sendBeacon() to call a Firebase cloud function

I had this idea of using navigator.sendBeacon() to call a cloud function to update a certain document. Why I am using navigator.sendBeacon(), is because I thought I could combine it with an event listener as…
0
votes
1 answer

How to use navigator.sendBeacon() to execute a function?

I'm currently using a Firestore backend. What I want to achieve is to update a document on Firestore when the window closes. I know we could use Navigator.sendBeacon() to call an API when a window closes because it receives url and data (optional)…
Owenn
  • 638
  • 1
  • 6
  • 23
0
votes
0 answers

NodeJs stop receiving post requests after 6 requests

I am trying to use navigator.sendBeacon() when user is inactive. At the same time I will be sending to server side when user is inactive. If I switch from the browser after the 6th sent beacon, my backend don't take the requests. Client side: …
Alsakka
  • 155
  • 10
0
votes
1 answer

using sendBeacon in WordPress ajax

I am trying to use an ajax request on page unload/visibilityChange. I found navigator.sendBeacon is a best option for that. I tried using it with WordPress, the request is being sent but I get 'POST 400 bad request' error. Here is my js code: let…
toddash
  • 167
  • 2
  • 17
0
votes
0 answers

iOS Safari track users when switching pages via link clicking

I am trying to track how long an user stays on a specific page on my website. I am using sendBeacon, AJAX as Fallback and the Page Visibility API. It works in all browsers and even in Safari when closing or switching the tab. The problem now is that…
Hans Martin
  • 337
  • 1
  • 13
0
votes
1 answer

How to run POST request when user exits tab or browser?

I have this so far window.addEventListener("beforeunload", function(e){ $.post('insert.php', {'postmessage':'CALL bor.update_records_exit(\'' + authUser + '\');'}, function(data) { e.returnValue = null; return null; …