?
Solved

Including Google Analytics Code in 301 Redirect

Posted on 2011-05-12
17
Medium Priority
?
1,069 Views
Last Modified: 2013-12-07
I am using a 301 Redirect that looks like this....

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.org/filename.aspx");
exit();
?>


I want to add  Google Analytics code to report the traffic to our analytics...

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>


I am having problems getting this code to work inside my PHP.  Can someone show me the what the combined should look like?

Thanks,
Dave
0
Comment
Question by:hrhs
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 17

Expert Comment

by:nplib
ID: 35747999
your header redirect is redirecting before the java can fire.

remove

header("Location: http://mydomain.org/filename.aspx");
exit();


and at the end of your javascript function put
window.location.href= 'http://mydomain.org/filename.aspx';
0
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 35748486
Just place the analytics code ahead of the php code.
0
 
LVL 17

Expert Comment

by:nplib
ID: 35748608
if you put the javascript above header code you will get error that says something about output sent before header and it won't redirect.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Tony McCreath
ID: 35754718
The two concepts are incompatible.

The 301 redirect is in the header and the JavaScript is in the content.

The 301 tells any browser to move on before it even considers looking at the content.

You can't alter the header after you've written content as its already been sent, hence the error.

If you provide more information on what you want to do, then we may be able to provide an alternate solution.

If you have to do the 301 and want to track it happened you could either track based on the referrer or have your redirect URL include tracking information.
0
 

Author Comment

by:hrhs
ID: 35771483
Tiggerito,

I am looking to redirect properly so that I don't suffer from the engines for trying to trick.  It's really a technique I want to do to simplify a URL so I can keep track of the traffic from ads.

I don't want to just make the page URL the /pagename, but rather use the simplified URL to redirect to the true page name.  If I can log the use of the simplified URL, then can monitor the ROI from an ad and the actual (true) page traffic separately.

So, I was trying to redirect properly, but wanting to know when someone used the simple URL.

Does this make sense?

Thanks everyone for the advice.
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 35774076
One way to do this is to add tracking code in the redirect URL. This code will identify where the redirect came from.

I do this for QR Codes. My QRCode contains a short URL . e,g. min.to/QR1 This 301 redirects to a tracking URL using Googles campaign tracking:

http://www.google.com/support/analytics/bin/answer.py?answer=55578

Which integrates with Google Analytics.

Another recent idea I've heard is tracking via hash tags. e.g. the tracking url is domain.com/#from=QR1
I think the idea is that search engines will ignore the # info so you won't have indexing problems. For this to work your tracking system cannot ignore the # data.
0
 

Author Comment

by:hrhs
ID: 35780249
We run ads (billboards, radio, television, print)  that offer a URL like hrhonline.org/heart which then redirects to http://hrhonline.org/HealthServices/KeyServices/HeartCardiology.aspx

I am just looking to record the traffic to /heart in GA, then redirect the traffic legitimately without ticking off the engines for the redirect.

Tiggerito, I think the URL tool you've offered is for Adwords ad traffic.

Is there some legitimate proper redirect that the engines deem as proper other than 301 I should be using?
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 35782704
The URL tool is used by AdWords but is for Google Analytics. It's how Analytics can track Adverts.

You can invent your own personal campaigns with it.

On another note, if these are hand typed URLs it doesn't really matter what type of redirect you do. Those URLs should never be found by search engines, in fact you should block them from search engines to ensure your data gathering is not tainted. This means a 301 is not important as you're only doing a 301 to keep the search engines happy.

If that's the case then you could probably just do JavaScript based redirects which run after the analytics code has been fired.

There's probably better experts out there in how to track off-line adverts.

Personally, I would have those URLs block spiders and use 301s and Google's URL builder to track them via campaigns.

0
 

Author Comment

by:hrhs
ID: 35785292
I think you have a point Tiggerito.  I should be blocking them since the engines finding them will taint the math.  Good thought...  I was more worried about getting on some sort of black list because we actually used redirects.  I am not a PHP guy so I will try to see if I can get a file to work properly and forget about the 301.
0
 

Author Comment

by:hrhs
ID: 35785721
Can someone actually write the file the way it should look?  I have tried several combinations of the script before and even outside the php, I get errors.

There are the two things I need to wrap together.

<?php
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
window.location.href= 'http://hrhonline.org/HealthServices/Services/ImagingRadiology.aspx';
exit();
?>

Thanks...
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 35785962
What are the errors?

With the async Google code you can get race conditions. i.e. your location change may happen before the Analytics code gets loaded and run.

Google recommends the location change is done after a timeout:

http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55527
0
 

Author Comment

by:hrhs
ID: 35789336
I believe I found a solution.  I couldn't get it done with PHP.  Since I am hosting on a Windows platform, I started looking at a solution using an aspx page.  Here is what I got working working and is showing up in  Google Analytics.

<html>
<head>
<title>Radiology | Imaging | XRay | Hannibal, MO | Doctor</title>
<script type="text/javascript">
<!--
function delayer(){
window.location = "http://hrhonline.org/HealthServices/Services/ImagingRadiology.aspx"
}
//-->
</script>

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxx-xx']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head>
<body onLoad="setTimeout('delayer()', 1)">

</body>
</html>

I named the page default.aspx and oddly it's showing up in GA as index.html.

Thanks for everyone's help trying to get this working in PHP.

Dave
0
 
LVL 31

Expert Comment

by:gwkg
ID: 36044382
You can not send a 301 status code via Javascript. Since the HTTP status code of the page will remain 200 OK, most search engines will continue to think the location is valid. Any page rank of the old location will not be transferred to the new location.
0
 
LVL 31

Accepted Solution

by:
gwkg earned 1000 total points
ID: 36044387
What you could do is include tracking info in the url you are redirecting to

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.org/filename.aspx?utm_source=olddomain&utm_medium=redirect");
exit();
?>
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36240597
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question