Hey everyone! I've been trying to figure out how to find out the website a user visited before arriving at mine. I came across a discussion on this topic where someone mentioned using the "Referer" parameter. However, when I tried testing it in the console after clicking a link, it returned an empty string. According to the documentation, it shouldn't be empty, so I'm a bit confused. Does anyone have insights on how this actually works?
5 Answers
When you say you 'entered this link', did you just change the URL directly in your browser? If so, the referrer will be blank since that action doesn't carry over a referer. The header is only sent when a link on another site is clicked to navigate to yours. Also, external sites can block the referrer via the referrer policy, so it can be hit or miss.
To track where users are coming from, you want to look at the "Referer" header. Check out the MDN documentation for more details here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Referer
You're looking for the referer website or page, and that's standard in analytics tools. Google Analytics and others show this info too. But just remember, you can pull it directly from the request header without using any analytics, but it is optional, so not all sites will provide it.
Checking the Referer header is a good start, but keep in mind that it's not very reliable since browsers block it in certain situations for security reasons. This is usually a good measure to protect user privacy.
Boooo! Sounds complicated!
Oh, just a heads up! The misspelling of "referer" is actually part of the web specs from a long time ago. Makes for some confusion for newcomers!