I want to do some logic if user click my website in google results and go to my website then when he click one of links on website then he is redirect to some url (eg https://example.com)
I tried 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://example.com";
}
});
</script>
but this version redirect to normal link target not to example.com. How can I improve this?
Now this works only when my link on website have target="_blank" then it redirects to example.com in main window and in new window redirect to website link location.