Google Analytics: Implement JavaScript Code Snippet in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tangoking
Forum Newbie
Posts: 9
Joined: Tue Sep 22, 2009 4:31 pm

Google Analytics: Implement JavaScript Code Snippet in PHP?

Post by tangoking »

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:

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>
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
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Google Analytics: Implement JavaScript Code Snippet in P

Post by PHPHorizons »

You could use a socket and connect to the google site, but it looks like that connection is used to inject a javascript script into the webpage. Therefore, a browser would be needed in order to execute that script. You could attempt to do it server side, but you'd need to first have a server side capability to execute javascript. And the problem I foresee with doing that is that the google code expects to be run on the client's computer and not the server and you would be polluting your own ga results.

So it seems to me that the best thing to do is to let the ga execute on the client's computer in order to avoid mucking up the ga results.

Cheers
tangoking
Forum Newbie
Posts: 9
Joined: Tue Sep 22, 2009 4:31 pm

Re: Google Analytics: Implement JavaScript Code Snippet in P

Post by tangoking »

Yes... this article discusses the issue in a bit more depth, and he makes exactly the same point:
PHP (I’ll focus on PHP in this example) can also request a file from the internet without executing or showing it. Just requesting the analytics image is enough to trigger a hit in your report and it will show up in your account. The downside of requesting the image from your server is: You lose the IP address and other data that Google records at the moment the image is requested. The server IP of your web server will be recorded.
http://www.vdgraaf.info/google-analytic ... cript.html

I suppose if I was really motivated I could find a way to grab the user data when they make a page request, replace the server IP et al with this user data, and then send the image to GA...
Post Reply