How can I determine if the document.referrer is from my own site?
Asked Answered
A

5

18

Each time a page is requested I get the referrer of the page it came from. I need to track just referrer from other sites, I don't want to track going from one page to another within my site. How can I do that?

Anthropo answered 6/12, 2010 at 2:46 Comment(5)
Wow, asked the question, then answered it in under 1 minute with a link to your own site?Repulsion
I figure posting a solution is a good thing. I'm pretty sure others hit this same problem.Anthropo
@Richard actually StackOverflow recommends posting helpful Q&A this way. Once you get enough points, you can do so in a single submission.Anthropo
@David Yes, this was one of the original reasons for StackOverflow. So programmers can paste Q&As like this without having to write blog posts.Lucrative
Actually, writing your own Q & A pairs is recommended in SO. It just doesn’t like answers having nothing but external links.Epifocal
P
43
document.referrer.indexOf(location.protocol + "//" + location.host) === 0;
Phosphor answered 6/12, 2010 at 3:13 Comment(2)
@Eric: Nope. This isn't a regular expression, it's a string. "//" does not need escaping.Phosphor
It will not count sub-domain referrers. We have bags.website.com and furniture.website.com, in which case, this would be false even though the user is on the same platform / main domain.Kindless
A
1

Originally posted at JavaScript - Am I the Referrer?

When someone comes to our website for the first time, we store the referrer in a cookie. This way, if they download our demo, we can get the original referrer from the cookie and we learn what sites are effective in driving leads to us.

Of course, every subsequent page a visitor hits on our website will show the referrer as our website. We don't want those. What we first did to avoid this was look for the text "windward" in the referrer and if so, assume that was from our site. The problem with this is we found a lot of referrer urls now have windward in them, either as a search term or part of a url that talks about Windward. (This is good news, it means we are now a well known product.)

So that brought me to our most recent approach. This should work for any site and should only reject referrers from the same site.

function IsReferredFromMe()
{

    var ref = document.referrer;
    if ((ref == null) || (ref.length == 0)) {
        return false;
    }
    if (ref.indexOf("http://") == 0) {
        ref = ref.substring(7);
    }
    ref = ref.toLowerCase();

    var myDomain = document.domain;
    if ((myDomain == null) || (myDomain.length == 0)) {
        return false;
    }
    if (myDomain.indexOf("http://") == 0) {
        myDomain = myDomain.substring(7);
    }
    myDomain = myDomain.toLowerCase();

    return ref.indexOf(myDomain) == 0;
}
Anthropo answered 6/12, 2010 at 2:47 Comment(2)
Doesn't that break for https?Iambus
@Pureferret - yes, needs to handle https too. This was written back in 2010 when public websites were almost all http.Anthropo
D
0

Solutions presented works in case of no sub domain in website in case of sub domain is there then we have to check just before the domain itself if any sub domains presented:

document.referrer.replace("http://", '').replace("https://", '').split('/')[0].match(new   RegExp(".*" +location.host.replace("www.", '')))

this solution will add .* before the domain to detect that sub domain is from same domain.

Declinate answered 14/8, 2013 at 19:26 Comment(0)
P
0

If pages of “the same website” you think have the same origin (the same protocol, host, and port.),

URL syntax diagram

check it this way:

function the_referrer_has_the_same_origin() {
    try {
        const referrer = new URL(document.referrer);
        return (referrer.origin === location.origin);
    } catch(invalid_url_error) {
        return false;
    }
}
// Works as intended for `https://www.google.com` and `https://www.google.com:443`.

.

If you’d like a short one and not to consider unlikely situations, try this:

document.referrer.startsWith(location.origin)
// Fails for `https://www.google.com` and `https://www.google.com:443`.

.

Pyrrha answered 2/7, 2019 at 12:24 Comment(0)
B
-2
document.referrer.includes(location.host);
Broeder answered 20/12, 2017 at 11:38 Comment(2)
Nice solution, but be aware that IE doesn't support the includes() method (developer.mozilla.org/nl/docs/Web/JavaScript/Reference/…)Benzene
This isn't secure. You should only be testing from the start of the string, as the referrer can include your host as a subset of itself: e.g. "notactuallygoogle.com" will match for "google.com" with your method.Phosphor

© 2022 - 2024 — McMap. All rights reserved.