I want to do logic: if the user clicks on my website in Google results and goes to my website then when they click one of links on website then they are redirected to some URL (e.g. https://stackoverflow.com)
I try this way:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
var ref = document.referrer;
document.write('document.referrer: ' + ref)
$("a").click(function() {
if (ref === "https://www.google.com/" || ref === "https://www.google.pl/") {
window.location.href = "https://stackoverflow.com";
}
});
</script>
But this version redirects to normal link target not to stackoverflow.com. How can I improve this?
Now this works only when my link on website has target="_blank" then it redirects to stackoverflow.com in main window and in new window redirects to website link location.