Google Analytics: Implement JavaScript Code Snippet in PHP?
Posted: Tue Jul 20, 2010 12:57 pm
Hi Peoples,
Is there a way to implement the Google Analytics (GA) code snippet functionality in PHP?
Basic GA web page tracking requires insertion of the following JavaScript code snippet right before the </head> tag:
In our web site structure, we do a lot of redirects, and each of them need to be tracked separately. Like some of you, I never trust the client, and whenever possible process things server-side. Thus, I would prefer to redirect using the php header() command, as opposed to loading the page and using some form of JavaScript-body-onload() process.
For example, imagine a set of URL's as follows:
http://www.example.com/coffee/colombia
http://www.example.com/coffee/kenya
http://www.example.com/coffee/costa_rica
Each of them log a visit via the GA code snippet before redirecting the user to http://www.coffee.com/. How can I implement the GA code using PHP before sending them to the new site via the header() command?
Thanks for your help!
Love, tangoking
Is there a way to implement the Google Analytics (GA) code snippet functionality in PHP?
Basic GA web page tracking requires insertion of the following JavaScript code snippet right before the </head> tag:
Code: Select all
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
For example, imagine a set of URL's as follows:
http://www.example.com/coffee/colombia
http://www.example.com/coffee/kenya
http://www.example.com/coffee/costa_rica
Each of them log a visit via the GA code snippet before redirecting the user to http://www.coffee.com/. How can I implement the GA code using PHP before sending them to the new site via the header() command?
Thanks for your help!
Love, tangoking