I use redirect pages for my affiliate links. So, I have file.php that contains redirect to my affiliate link. I would like to track it with Google Analytics. Should I just place js tracking code before php redirect? Should I strip <script type="text/javascript"> ... </script> Should I place tracking code before or after opening code: <?php Here is exapmle of what I want to put together, could someone show me the right way: GA js code (new asynchronous tracking code): <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_setDomainName', '.example.com']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; ga.setAttribute('async', 'true'); document.documentElement.firstChild.appendChild(ga); })(); </script> and PHP redirect: <?php header("Location: http://www.example.net/xxxxxxxxxxxxxxxx"); ?>
Make your PHP file look like this: <html> <head> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_setDomainName', '.example.com']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; ga.setAttribute('async', 'true'); document.documentElement.firstChild.appendChild(ga); })(); window.location = "http://www.example.net/xxxxxxxxxxxxxxxx"; </script> </head> </html> PHP: Hope it helps!
Thanks for your help. I guess it should then be named file.htm. But I'd rather go with php redirect instead of js. Because I'll add referrer info from cookie to link, like this: <?php header("Location: http://www.example.com/xxxxx?tracking=".$_COOKIE['RefererCookie']); ?> I don't know how to do that with js.
You can do it simply like this: <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-x']); _gaq.push(['_setDomainName', '.example.com']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; ga.setAttribute('async', 'true'); document.documentElement.firstChild.appendChild(ga); })(); </script> <?php header("Location: http://www.example.com/xxxxx?tracking=".$_COOKIE['RefererCookie']); ?> PHP:
Yes, I tried that before posting this thread. I have placed js tracking code before php redirect, named file somename.php and placed it on subdomain - _gaq.push(['_setDomainName', '.example.com']); should make it work across subdomains. Since it isn't tracking I thought that I can't just put js code in php file. Maybe I should first try with old GA tracking code.