I am trying to track a redirect page with google analytics:
I have a page called redirect.php; when I visit an url like redirect.php?c=12345678 , a php function does a query on a sql lookup table to decode the code 12345678 with a previously inserted url, then does a 301 redirect.
(I'm doing a 301 redirect to avoid duplicate content.)
I wish to track every single visit to redirect.php with analytics, but I can't.
For example:
redirect.php?c=87654321 redirects to story.php.
Obviously in Analytics I can't find the redirect.php page. the story.php referer is organic/google so I can't rely on the referer.
Is it possible to track every redirect in some ways?
GA works in the browser. Whenever the user visits a page with the GA script - a request from the user to google is sent notifying about the visit.
Since you are making a 301 header redirect - no GA script is loaded and therefore google doesn't know the user has been on that page.
Options you have
Switch the redirect from 301 header to a page with meta redirect and the GA code
Switch to another analytics system such as Piwik and add data to it manually on the redirect page
Implement your own counter inside the redirect script, separated from GA
Of course you could merge the options and have, say a page with meta redirect and the GA code, which redirects to redirect.php script, which in turn redirects with a 301 header, but this is not the best solution.
You need to add your Analytics code BEFORE the redirect.
Related
Our website is currently using Google Universal Analytics to track goal conversions from our own private domain and a 3rd party payment processor. Currently our goal flow is:
/cart -> /checkout -> /receipt
On the /cart page which resides on our private domain, the user clicks "checkout". When they do, it goes to /precheckout which is a preprocessing script which finishes with header("Location: external.com/checkout"); exit;
Our suspicions lead us to believe that it's this intermediary page that is breaking our funnel. From what I understand, when header("Location: "); is used and no data is sent to the browser, that it should simply follow the redirect. What is happening?
I'm willing to bet that the server changing the location is causing the browser to not send the "Referer" header.
Try header('Referer: /cart'); in the /checkout.
Since the user is not clicking on anything in between precheckout and checkout, you need to manually set it. If the user isn't always going to be clicking that link to get to the checkout page (i.e. bookmarking it), it may be better to set a session variable to determine if they just came from the cart and if they did use the header code above.
The 301 redirect is the preferred way to redirect URLs, since it informs search engines that the URL has moved for good, and search engines should put the new URL page in the search results instead of the old URL page and transfer the new URL page, the page rank of the old URL page.
The 301 redirect can be done across domains or on the same domain.
Google ""analytics"" recommends to use 301 redirect.
<?php
// PHP permanent URL redirection
header("Location: http://www.example.com/new-page.php", true, 301);
exit();
?>
https://www.rapidtables.com/web/dev/url-redirect.html
I have multiple affiliate links which have there own PHP redirect file on my hosting server. The redirect page www.myredirectpageonserver.com is clicked
I have a simple php code on my redirect page:
< ?php
header("Location: myaffiliatesite.com");
? >
how do I add GA event tracking to this without losing the redirect. Please help, I keep getiing near the answer but the code never works?
You cannot add good analytics to file which contain redirection. It is because there can't be any content sent when you want to use header("location:").
You can add google analytics code to redirected page.
There may be also trick with opening another page with curl or file_get_contents(). You can do it and put the GA code inside this page. In this case when cookies are allowed(curl) you should be able to obtain statistics for this page.
I have a website that hosts content in English and in German: www.mysite.com. I bought a new domain with the same name (www.mysite.de) so that it would redirect to the German content on mysite.com. Here is what I have in the index page on mysite.de:
<?php
header("Location: http://www.mysite.com/index.php?la=de");
exit;
?>
How do I track my Google analytics on mysite.de, before it redirects? I have the Google analytics script, but don't know where to put it.
By sending the Location: header, you instantly redirect your viewer to a different website. My suggestion is to read up on the Refresh: header, show him some information like "You'll be redirected in 5 seconds, if it didn't happen, click here" and in the meantime, also display him the HTML code of the script.
How do I create a php which redirects with use of the URL. I.e. - Redirect-To.com/Change.php?=Google.com Then goes to google etc
I'm wanting to create a php page where a user goes to
Redirect-To.com/Change.php?=Google.com
or
Redirect-To.com/Change.php?=Youtube.com
or
Redirect-To.com/Change.php?=Yahoo.com
Then the visited page redirects to Google or whatever page is after ?=
This is basically what URL shorteners do, except they lookup the destination from a database and probably store some tracking information.
Check out the header function that lets you set HTTP headers. You want to set a 302 redirect, which means moved temporarily, and a Location header to set the new location.
http://php.net/manual/en/function.header.php
header('HTTP/1.1 302 Moved Temporarily');
header('Location: ' . $newLocation');
301 is another common redirect code, but it means "Moved Permanently" and a lot of browsers will cache that status and not hit your website if the person clicks your link again.
The HTTP spec says you should give the full URL with the redirect, though most browsers will work without it.
You may also want to white list destinations; a user might follow a link to your site and be redirected to a site owned by a a malicious third party. Thinking they were in the confines of your site, they may enter information or perform actions allowing the third party to gain access to their data.
In the URL, values without keys will be ignored, so you should write:
...change.php/?redirect=http://www.google.com
Then you can access the "redirect to" address with $_GET['redirect'].
To redirect to a given address, you can use to Location header (make sure nothing is sent to the output before calling the function):
header('Location: ' . $_GET['redirect']);
You have to start the redirecting URL with "http(s)://" like I wrote above.
If you want to stop the script (as it does not stop immediately after the redirect function), you have to call the die() or the exit() function.
(If a shorter URL is better for you, the URL can be just ...change.php/?http://www.google.com, then you can access the address with $_SERVER['QUERY_STRING'].)
I'm doing a 301 redirect on site A to site B - when the user arrives at site B it needs to find the page the user came from. This doesn't seem to be working though:
$_SERVER['HTTP_REFERER']
whereas if I do a link to the page
go
I get the referrer through. Is there a reason it doesn't come through after the redirect? If so can anyone offer any advice on how to do this. I want to avoid at all costs having a query string on the redirect.
Is there maybe another header I need to send with the page that redirects?
Thanks for any advice!
The thing is, the HTTP_REFERER is site A. That's just how a 301 works.
That said, the easy way to do this is to take the url of the referrer to site A onto the end of site B's URL as a variable. Then, at site B, any time you have a referral from site A, you can have it.
<?php
header("Location: http://site-b.com/?ref="
.urlencode($_SERVER['HTTP_REFERER']),TRUE,301);
?>
Then of course at site B, access urldecode($_GET['ref']) for your referrer.
However... if you're looking to avoid _GET variables, you have a few options.
A) Collect the _GET request, then re-munge the URL -- IE have site B redirect to a "clean" version of itself.
B) Have your redirecting page make a curl or stream_get_contents over to a "collection" page prior to issuing a header(), where you collect and store the any session information (like the refererer) and have it prepared to be processesed when they redirect.
You could try adding a CGI query string to the end of your URL when doing the redirect -- eg
http://www.site-b.com?redirectfrom=www.site-a.com
site-b.com would ignore the URL parameter, but it would be recorded in the logs and would be accessible from within PHP.
You can do it with javascript. Use the following script, but it between <head> and </head>
<script type="text/javascript">
location.href='http://www.site-b.com';
</script>
This will of course not make a proper HTTP 301 redir, but I just tested it, and it will send the referer (the referer being site-a).