More thoughts on HOW to track
There seems to be a few possible ways to do the tracking and I think it broadly falls into two categories:
- use JavaScript to send a request
- request a resource (image, CSS) using an HTML tag or a CSS property
I talked before about the fact that requesting a resource wouldn’t send the details of the referring page. So I may have both as options with the resource request being easier to add in some cases, and the JS offering more functionality.
But some more questions have come up when thinking about this.
Do I need to send back valid response content?
I want the request and response to be as small as possible. So can I send back an empty response?
A quick bit of research seems to show that I at least need to set the content type (Aside: I wonder how web servers determine this?). And I’ll need to send an HTTP status code back. But if I set that, can the response content itself actually be empty?
How do I prevent caching of responses?
Browsers are pretty good a caching resources these days. So what do I need to do to ensure that a resource response is not cached.
How do I prevent blocking page load?
In what circumstances does loading a resource block a page loading? I don’t want to do this. My request should be asynchronous if possible. So how do I achieve that in all cases?
Requesting CSS doesn’t look like a great idea.
Copying an image-generating bit of PHP looks like a good idea though.
I’m suddenly starting to see that this maybe isn’t as easy as it first seems. Maybe this is why people don’t build their own analytics platforms.
One Response
Use `window.onbeforeunload(navigator.sendBeacon())` which will submit a POST request. POST requests aren’t cached in browsers or intermediary caches (unless they’ve specifically configured to do so).
It doesn’t work in all browsers, but you can do feature testing and fallback to something else for browsers where it isn’t supported. Send beacon doesn’t delay browser navigation so you can do it in the beforeunload event.
Importantly, it will give you the opportunity to count actual time on page (as early as possible store a timestamp in `window.pageloaded` and in onbeforeunload you can look at the time since that timestamp to accurately determine the time on page . Most analytics platforms counts time spent in between pageviews and not time on the page. Meaning that the last/only page you visit wouldn’t get counted.
As for responses: Send a `HTTP/1.1 204 No Content`. It’s the smallest possible response you can send as it doesn’t have a body. Browsers don’t treat it as a failure and it works great with intermediary proxies and such.