0

In my current project, I must to implement different site behavior depending on whether it was a visit by a direct link or a redirect from a another pages of site trough a react-router.

So... is there a way to detect this fact?

  • @josemartindev this is one of variants, but in a "already has answers" link guys sayd that documen.referer work not always. Thanks for your answer, I appreciated them, but that's not the whole answer. – Konstantin Pershin Nov 09 '20 at 08:32

1 Answers1

2

You can use document.referrer. The value is an empty string if the user navigated to the page directly (not through a link). Here is an example using React Hooks.

useEffect(() => { 
  if (document.referrer === "") { 
     console.log("User came directly"); 
  } else { 
     console.log("User did not came directly"); 
  } 
})
josemartindev
  • 1,413
  • 9
  • 17