How do Analytics tool provide referer accurately?

134 views Asked by At

As pointed in HTTP_REFERER blank, need alternative and $_SERVER["HTTP_REFERER"] is not working on Mozilla, PHP's $_SERVER["HTTP_REFERER"] and Javascript's document.referer can be null, above all in the case of HTTPS request.

For example, traffic coming https://github.com/ gives me a blank string with both the PHP and JS method.

How do Google Analytics (or other analytics tool) do to log the referer accurately, for example in the case of https://github.com referer?

Note: this is not a duplicate question of the previously mentioned questions, the question here is to study how analytics tool (such as Google Analytics) do it (probably in a very clever way).

1

There are 1 answers

2
Kemen Paulos Plaza On BEST ANSWER

To put you in context,in Google Analytics (the universal JS) is setted by the following function:

function(a) {
            var b = I.referrer;   //I equals documents
            if (/^https?:\/\//i.test(b)) { //check if b.referrer is a page
                if (a) return b; // "A" is the value to force to always send the referer
                a = "//" + I.location.hostname; //extract the actual location
                var c = b.indexOf(a);
                if (5 == c || 6 == c)
                    if (a = b.charAt(c + a.length), "/" == a || "?" == a || "" == a || ":" == a) return;
              //if this is not the same, return null, in other hands returns the referral domain
                return b
            }

As you can see, the only resource used is the document.referrer. So this comes empty there is nothing to do. That is one of the reasons to use UTM parameter when you can edit the link (to determine the source manually)

Now HTTPs due a security reason will not pass the reference, so maybe use parameter in the URL looks the best solution (replicate the UTM by you)