How to track with Google Analytics on a redirection page with PHP?

22,558

Solution 1

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.

Solution 2

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:

Solution 3

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.

Share:
22,558
Radical_Activity
Author by

Radical_Activity

Updated on October 05, 2020

Comments

  • Radical_Activity
    Radical_Activity over 3 years

    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?