HTTP Referrer on Redirection

2019-06-11 05:21发布

I have a small script that redirects users to main site if they come from a banner on my/other remote sites.

<?
.
..
...
....
header("location:$golink"); 
?>

But google analytics will not show the referrer site (where the script is working) instead it shows the url where the banner is clicked. Obviously I can not keep a track of all sites where banner appears and dont want to. I want the refferer to be the site where the script is working. How do I have to use the $_SERVER['HTTP_REFERER']; in order to do this ?

2条回答
时光不老,我们不散
2楼-- · 2019-06-11 05:52

Yes, G.A is blind to this kind of server-side stuff. And their PHP Api is not helpful either.

However, you could have a short redirection page, holding the GA tag inside like this :

<html>
<head>
<title>A web page that points a browser to a different page after 2 seconds</title>
<meta http-equiv="refresh" content="2; URL=<?php  echo $golink; ?>">
<meta name="keywords" content="automatic redirection">
<script>var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'))</script>
</head>
<body>
If your browser doesn't automatically go there within a few seconds, 
you may want to go to 
<a href="<?php echo $golink ?>">the destination</a> 
manually.
</body>
</html>

Notice the $golink variable in the meta tag.

If you use this, do not forget to replace UA-XXXXX-X by your real account number.

Credits : optimized GA tag goes to Mathias Bynens

[EDIT : javascript only version]

<html>
<head>
<title>Redirecting you...</title>
<script>var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'))</script>
<script>
 <!--
 if (window.addEventListener) 
    window.addEventListener('load', function() { window.location="<?php echo $golink; ?>"; }, false);
 else
    window.attachEvent('onload', function() { window.location="<?php echo $golink; ?>"; });
 // -->
 </script>
</head>
<body>
</body>
</html>
查看更多
虎瘦雄心在
3楼-- · 2019-06-11 06:11

GA has a method that will let you to override the default referring URL (document.referrer) with a specified value.

So if you want to keep the redirect server-side, you can append the referring URL as a query string param in your header() call, and then look for it on the target page and specify it as the referring URL.

I don't know how you are building your $golink variable, but basically you would add something along the lines of:

$golink .= "?ref=" . $_SERVER['HTTP_REFERER'];

Use a & instead of ? if there are already URL params, and the code above assumes using ref as the URL param, so use whatever var you want.

Then on your target pages, before your _trackPageview call, you would add

_gaq.push(['_setReferrerOverride', ref]);

ref would be a javascript variable with the value of the ref=xxx query string param. For some weird reason Javascript does not have a native way to grab URL param values, nor does GA provide an (exposed) solution. If you already have a solution on your pages for grabbing URL params (like something from a framework or a function you've already made) then use that. Otherwise it's pretty easy to find a javascript function that will do it for you.

There are a couple benefits to doing it this way:

  1. You don't have to worry about the visitor seeing an interstitial page.
  2. You don't have to worry about GA not getting a chance to fully load before redirect
  3. You can see the referrers tied directly to your landing pages, because with the interstitial page, you will always see that interstitial page as the referrer, and will have to look at referring url reports for the interstitial page.
查看更多
登录 后发表回答