2

Ive got domainA which has a 301 redirect setup to redirect traffic from domainA to domainB, Id like to track how much traffic is coming through as a redirect from domainA.

How could i track this in Google Analytics ?

Further info my 301 is set in the .htaccess file

1

3 Answers 3

3

Solution that matches your query exactly:

https://web.archive.org/web/20161211202057/www.lanevance.com/web/web-development/tracking-301-redirects-with-google-analytics

You can set up a campaign

Sample:

www.domaina.example -> http://www.domainb.example/?utm_source=domainaredirect&utm_medium=&utm_campaign=301_Redirects

Now you can track this traffic in referrals.

0
1

You wont actually be able to log this with Google analytics. This would require that the page is loaded before it is redirected.

You could try using meta refresh tags on these pages and place your tracking code before the refresh tag.

Alternately, parse your server logs with something like AWstats http://www.awstats.org/ Your .htaccess is instructing the server to do the redirect before anything else so it will be the only service logging this. (if it is a default configuration)

2
  • In my mind i was thinking of having it logged in DoaminB's google analytics, ideally it would be able to show up under the referrer tab, even though its a 301
    – sam
    Jun 7, 2015 at 23:07
  • 3
    Sorry - that makes more sense. You should be able to treat these redirects as "campaigns" and append query string specific tracking to them. support.google.com/analytics/answer/1033867 Jun 8, 2015 at 1:50
1

Use a server-side scripting language. Create this PHP script:

<?php
$handle=fopen("301tracking.log","a+");
fwrite($handle,"1 guest from IP".$_SERVER['REMOTE_ADDR']."arrived at ".time()."\n");
fclose($handle);
$newhost="http://domainB.com";
$url=$newhost.$_SERVER['REQUEST_URI'];
header("HTTP/1.1 301 Moved Permanently",true);
header("Location: ".$url,true);
?>
<html>
<head>
<title>Redirect</title>
</head>
<body>
<p>Document moved <a href="<?php echo $url; ?>">here</a></p>
</body>
</html>

Save this as index.php and upload it to document root of domainA. Also, use mod_rewrite to redirect all requests on domainA to the script like so:

RewriteEngine On
RewriteRule ^(.*)$ /index.php [L]

You may have to add a check for the redirect status environment variable right after "RewriteEngine On" just incase the above lines go in an endless loop and/or you get error 500 (internal server error). An alternative is to upload the script to all folders on domainA. To customize the log, change the fwrite line. I apologize for poor grammar in the text part of the code, but you'll understand how the code works when you try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.