24

I have a website where I want to track who has clicked on specific links with GA.

Let's say I have this page: /index.php?id=32

On this page I run some query based on the ID variable (in this case: 32), and I get the URL of the 32 id item from the Database to redirect the visitor.

I'm using a PHP function: header('Location: http://www.example.com');. Before I'm redirecting the user, I want Google to capture the visitor's information and only then redirect to the desired webpage.

I have tried to paste the GA code and ECHO it just before the redirection, however it did not work. How is it possible to track these kind of pages with GA?

Radical_Activity
  • 2,618
  • 10
  • 38
  • 70
  • 1
    So you want the user to be stuck on a blank screen for a second and annoyed? Because if you could do it, that would be the outcome. Google Analytics operates by Javascript on the client, so you have to pause on the client before redirecting for it to catch the hit. – developerwjk Dec 12 '14 at 23:45

5 Answers5

18

Generally speaking

If your page uses redirects, the redirecting page becomes the landing page's referrer. For example, if you've changed your site so that index.html now redirects to home.html, then index.html becomes the referrer for home.html. If someone reached your site via a Google search that sent them first to index.html, you won't have any data regarding the Google search.

For this reason, you should place the Google Analytics tracking code on the redirecting page as well as on the landing page. This way, the redirecting page will capture the actual referrer information for your reports.

Note, some browsers may actually redirect before the JavaScript call from the code can be made.

(cf. https://support.google.com/analytics/answer/1009614?hl=en)

Your specific case

Since PHP is rendered and executed before any Javascript, Google Analytics tracker has no chance to send data to its server.


Solutions

Considering that you cannot track a PHP redirection page, there are a number of possible alternatives:




  • Campaign tracking (I wouldn't personally use this method in this specific case.)
    • /products.php?utm_source=index&utm_medium=redirection-page&utm_campaign=32

The last two items in the list are implemented on the individual links on the initial page before you get on the PHP redirection page.

Community
  • 1
  • 1
carlodurso
  • 2,886
  • 4
  • 24
  • 37
10

I would suggest something like the following. It uses hitCallback to do the redirect so the event will be sent to GA and then the redirect will occur. It also sets a Timeout so that should analytics.js be unavailable (for whatever reason) the viewer is still (eventually) sent to the correct place.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>example</title>
        <script>
            setTimeout(redirect, 2000);
            var redirected = false;
            function redirect() {
                if (!redirected) {
                    redirected = true;
                    location.href = 'http://your.website.here.com';
                }
            }
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
            ga('create', 'UA-NNNNNNN-N', 'auto');
            ga('send', 'event', 'Event Name', 'redirect', {
                hitCallback: redirect
            });
        </script>
    </head>
    <body>
    </body>
</html>

tracking codes and urls should of course be updated to match yours.

References:

sfosdal
  • 796
  • 11
  • 23
  • btw, I understand that my solution is not literally using PHP. I left it to the reader to sprinkle the appropriate `` into the example. Specifically for `location.href` and maybe also `UA-NNNNNNN-N` – sfosdal Dec 13 '18 at 00:34
2

If you would look at your error log you'd see an error that roughly says "Cannot send redirect header, header was already sent". It is not possible to do a redirect via location header when you already have had HTML/Javascript output before your PHP snippet (because at that moment you have already implicitly send your http headers).

It would be possible to use Googles measurement protocol to create a server-side solution that tracks the page before redirect. In that case you would need to create a client_id yourself and, in case the properties you redirect are your own, append it to the redirect url for cross-domain tracking.

If that's not concern you could use Curl or fopen to send a call to GA via a Url like this:

https://www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=XXXXXXXXt=pageview&dp=%2redirect

Where UA-XXXX-Y ist the account id from google, cid is the client id you generated and /redirect is the name of your page. If you want to recognize recurring users you need to save the client id to a cookie and check for every visit if the cookie exists and retrieve the id if it does.

Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
0

if you disable the redirect, and inspect the page, do you see the call being made to GA in your network tab? I've found that sometimes the push events don't show up in GA for like 24 hours.

If you see that it is indeed sending out to google then I would leave it enabled, and wait a day before checking GA to see if anything has been logged.

Shan Robertson
  • 2,742
  • 3
  • 25
  • 43
0

Since the page that is sending visitors to the redirect is also under your control, I would suggest a totally different approach: use event tracking on the source page.

On the page that has the /index.php?id=32 link, measure all clicks on this link by attaching an onClick handler to the link and executing something like

ga('send', 'event', {
  eventCategory: 'Redirect Link',
  eventAction: 'click',
  eventLabel: event.target.href
});
Peter
  • 2,874
  • 2
  • 31
  • 42