Set Previous Page Variable for 404 pages when source is external to owned site.
Hello.
I am trying to find the best way to track the the previous page URL for 404 pages when the source is external to my owened site. To find the previous page URL , I set a variable using JavaScript via Dynamic Tag Manager:
var ex=document.referrer
which allows me to get the previous page URL, however this function does not always work as sometimes the link comes from external sources (email, banner, google search ...)
What is the best way to proceed.

