0

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 ?

Justin T.
  • 3,643
  • 1
  • 22
  • 43
hasbehas
  • 15
  • 1
  • 6

2 Answers2

3

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.
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • My domain redirects to another domain, and I'm worried even if I set the referrer ( and I do have access to `setRefererOverride` on the second domain ), maybe all the parameters which google creates won't be the same ( isnt it page specific? ) or should I only really worry about the referring domain? – meder omuraliev May 14 '12 at 22:14
1

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>
Justin T.
  • 3,643
  • 1
  • 22
  • 43
  • I see, thanks.. but I dont want to use meta refresh.Even I use 0 secs, I still can see the page that redirects which I do not want to see at all. Any other way ? perhaps javascript ? – hasbehas Dec 29 '11 at 14:42
  • Yes, you could do full Javascript redirection, but user will still see a blank page for a split second, because some JS files have to load no matter what. Do you want me to update the page ? – Justin T. Dec 29 '11 at 14:47
  • ie8 did not redirect. Chrome+ff did.. so changed this part; Its faster than meta refresh though.. Thanks again – hasbehas Dec 29 '11 at 15:07
  • @hasbehas : I edited the code, it is cross browser now (because IEx likes attachEvent over addEventListener). You should keep it this way. If you remove it, you will lost redirections if "ga.js" is not loaded fast enough. – Justin T. Dec 29 '11 at 15:18