-2

I need the user to be directed to a certain URL according to the website he came from.

<script>
    function abc(){
    var ref = document.referrer;
    if (ref = /site1/) {
    window.location.href = "https://www.site1.wow";
    } else if (ref = /site2/) {
    window.location.href = "https://www.site2.wow";
    } else {
    window.location.href = "https://www.site.wow";
 }
}
</script>

New version, but still not working, returns undefined

function getParam(_parameter){
const parameters = new URLSearchParams(window.location.search);
if (parameters.get("utm_medium") && parameters.get("utm_medium") === "facebook") {
window.location.href = 'https://link1.wow', '_blank';
} else if (parameters.get("gclid=") && parameters.get("gclid=") === "gclid=") {
window.location.href = 'https://link2.wow', '_blank';
} else {
window.location.href = 'https://link.wow', '_blank';

} }

1 Answers1

0

You have to do it different way:

const parameters = new URLSearchParams(window.location.search);

This will take all parameters from actual site. And now you have to test if it has parameters that you are looking for for example:

if(parameters.get("utm_source") && parameters.get("utm_source") === "abc"){
 console.log("It has this parameter")
}
Sowam
  • 1,674
  • 3
  • 12
  • 30
  • I need to know if the site has the parameters utm_medium or gclid. – Luiz Felipe Machado Apr 08 '21 at 18:43
  • @LuizFelipeMachado parameters? what do you mean by parameters? that it has this in the url? for example "http://something/gclid/test.com" or something? – Sowam Apr 08 '21 at 19:52
  • something like "https://www.site1.ai/?utm_source=google&utm_medium=cpc&utm_campaign=brand-search-global&utm_term=windsor" if the URL has utm_source = abc, it directs you to the first URL, if it has a gclid = parameter, it directs to another URL, if it has no parameter, to a third URL. – Luiz Felipe Machado Apr 08 '21 at 20:03
  • @LuizFelipeMachado utm_source = abc? you mean something like "http://example.com/?utm_source=abc" so a parameter inside url? – Sowam Apr 08 '21 at 20:07