https://developer.mozilla.org/en-US/docs/Web/API/Navigation/...
You can do all of that server-side and much more reliably at that. The only reason to do any of this tracking client-side is advertisers trusting fake number go up more than sales numbers.