Google Analytics Tracking Using Zend Framework
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:
- Get Google Analytics Tracking Number
- Paste Code Into Site
- 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 || [];
_gaq.push(['_setAccount','GA-123445-01']);
_gaq.push(['_trackPageview']);
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;?>']);
Enjoy.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows