2
2
210mev2021-01-22 11:57:02
JavaScript
210mev, 2021-01-22 11:57:02

How to make a request from domain A to domain B only if the user has closed the tab / left domain A?

Good time of the day.

A question.
There is domain A as an external service, i.e. there is no access to the backend on it, you can only add JS to the frontend, for example, JS code with a GET or POST request to domain B.
The user comes to domain A on the first page, then goes to the second, then to the third, etc., that is, the sequence is always the same from page 1 to page 4.
You need to track these transitions, if the user left the site to page 2 or 3, then you need to execute third-party analytics scripts on the page from which he left, because these third-party analytics scripts should work once.
How can I do that? As I understand itbeforeunloadit will work only if the user himself closed the tab, but it will not work if the Internet is gone or the user has closed the browser entirely or the computer is frozen. I found a recommendation to make AJAX requests at intervals, which is like a ping, in order to understand that the user is still on the site. But then how to run third-party analytics scripts, maybe you need to send cookies with an AJAX request, and everything that is needed for analytics scripts, and if the ping suddenly breaks off, then execute analytics scripts with cookies already on domain B?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question