-1

For analytics purposes, I'd like to catch when a user transitions from one page of my site (which will be running the JS) to another within my site, or if they left entirely.

If they transitioned between pages on my site, ie. Page1 -> Click link -> Page 2 , I'd like to catch the URL of the page they are transitioning to (Page 2).

Is this possible by listening to some unloading events or something?

deeBo
  • 836
  • 11
  • 24
  • Does this answer your question? [How can I get the destination URL for the onbeforeunload event?](https://stackoverflow.com/questions/1686687/how-can-i-get-the-destination-url-for-the-onbeforeunload-event) – Studocwho Nov 02 '19 at 23:19
  • 1
    in Page 2 you can see where they came *from* - since you're only interested in navigation within your site, isn't this the same information anyway? – Bravo Nov 02 '19 at 23:23
  • Yes, I think I'll switch to a method like this. I wasn't confident it was possible, but no harm in checking I guess. Thanks for the responses – deeBo Nov 02 '19 at 23:24

1 Answers1

0

No, for privacy reasons no browser will allow you to capture the URL of the page a user is transitioning to. And if you find a way I suggest you report it to the browser dev team.

To capture the transition, you can use beforeunload / unload events, but they will not provide you the user destination.

EDIT: If you want to capture this in the same site, I suppose you should consider using a SPA concept or send this information via session storage.

glneto
  • 527
  • 3
  • 10
  • That's what I feared. I thought it'd be worth asking tho because the transitions are between pages on my own site, the same domain. Not anything that would, say, fail a CORS request. – deeBo Nov 02 '19 at 23:19
  • navigating would never *fail a CORS request* @DeeBo – Bravo Nov 02 '19 at 23:21
  • I know, just an example of the kind of URLs I'm trying to look at. I'm not, for example, trying to see where the user goes after leaving my site – deeBo Nov 02 '19 at 23:22
  • For the same site I suggest you consider using a session storage or something to pass this data to the next page. Since you own both pages code, you can be creative and workaround the browser security :) @DeeBo – glneto Nov 02 '19 at 23:22