At Help Scout today I’m working on a tracking script to help us track where our visitors are coming from. One of the ways it identifies that is using
document.referrer which returns the referring URL.
For example, if you go to Google, click on a link, then check out the value of
> document.referrer "https://www.google.com/"
For testing the script I’m working on I needed a way to manually set the value of the referrer to quickly test that the script is handling it correctly.
Unfortunately you can’t directly overwrite the value:
> document.referrer = "test.com" "test.com" > document.referrer "https://www.google.com/"
Good news is that there’s a hacky way you can do it by overwriting what
document.referrer returns. After modifying the code slightly (setting
configurable: true), I was able to turn it into a bookmarklet that prompts you for what you want to set the referrer to:
Then when you click this bookmarklet, you can set the new referrer value:
document.referrer will return the new value:
> document.referrer "test.com"