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?
The header function in PHP can be used to redirect the user from one page to another. It is an in-built function that sends raw HTTP header to the destination (client).
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)
Since PHP is rendered and executed before any Javascript, Google Analytics tracker has no chance to send data to its server.
Considering that you cannot track a PHP redirection page, there are a number of possible alternatives:
window.location = "http://www.yoururl.com";
<meta http-equiv="refresh" content="5; url=http://example.com/">
ga('send', 'pageview', '/index.php?id=32');
/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With