Google Analytics is good at what it does. I mean really good. So good that I've been using it for over five years (exactly how long is a mystery to me but I know it's been at least this long) and I haven't ever thought about switching. The thing I like most about it is how brain dead simple it is to setup. It's basically a three step process:

  1. Get Google Analytics Tracking Number
  2. Paste Code Into Site
  3. Browse Reddit for a couple hours

But what if you need to track something a little more complicated? Like a Zend application that has URLs like this:

  • /user/view/id/1
  • /user/view/id/42
  • /user/view/id/1023223f/somethingElse/anotherValue

GA sees that as three different pages instead of being the UserController's viewAction(). Which is annoying because if you don't care about specific items then it's a little misleading about what the "top" pages are on your site.

It turns out that fixing this problem is *depressing *easy. The normal JavaScript code that Google gives you has a section that looks like this:

var _gaq = _gaq || []; 

This setups the various settings used on the page including the account (_setAccount...). The _trackPageview setting is what actually sets the page your tracking. If you leave this setting blank it uses the current URI. But you can set it to anything you want:

_gaq.push(['_trackPageview', '/user/view']);

I have the Google Analytics code in my layout so I just add the following to my Generic Controller (the controller all my other Controllers extend).

$params = $this->getRequest()->getParams();
$this->view->controller = $params['controller'];
$this->view->action = $params['action'];

Then in my layout.phtml I add:

_gaq.push(['_trackPageview', '/<?php echo $this->controller;?>/<?php echo $this->action;?>']);


Like this post? Don't forget to follow us on Twitter and Facebook for updates.