These days, I am using CakePHP more and more for many of my web projects. All my CakePHP projects are linked to my Google Analytics accounts and I integrate Google Events so that I track all important events on my web applications. For those who are not aware, Google Analytics Events are a way to track special user interactions with your site. According to Google, Events are user interactions with content that can be tracked independently from a web page or a screen load. Downloads, mobile ad clicks, gadgets, Flash elements, AJAX embedded elements, and video plays are all examples of actions you might want to track as Events. The following tutorial will explain how to integrate Google Analytics Event Tracking with CakePHP.
With just a small amount of extra code, you can start collecting data on various activities that happen on your website which cannot be tracked with the standard Google Analytics code. Now, this seems trivial except that, in the case of CakePHP, and many other PHP frameworks, the same page may be used for different things. For example, your dashboard page may be the page you redirect people to after a successful signup but it may also be your default page whenever something fails on your system. These 2 separate events need to be treated separately using Google Events.
In order to get event tracking done properly, I use CakePHP’s flash messages. CakePHP’s flash messages make it easy to display a one-time notification message to the user after processing a form or acknowledging data. Most CakePHP developers will display a flash message after an important event has occurred. This could be a new signup, a form error or a user login. These are all events that you may want to track through Google Analytics. So let’s go ahead and add Google Event tracking to our flash messages.
The first thing we need to do is modify the flash message layout. With CakePHP, you can replace the default flash markup by creating a new flash layout and placing it inside the views/Elements folder. In our example, we will create a flash message file called tracker
_flash.ctp. This flash layout will contain the actual flash message as well as the Google Event tracking code. Here is the content of the tracker_flash.ctp file”
<?php echo $message; ?>
You can then create flash messages using this layout by making the following call in any of your controller
$this->Session->setFlash('Some important thing just happened','tracker_flash');
All of this is pretty basic usage of the flash message. Now let’s go ahead and add event tracking to our flash messages. In order to do this, we will use a little known fact of CakePHP: The flash message has an additional third parameter that you can pass which is basically an array. You can put whatever you want in this array. This array will be used to store the Google Analytics events data. So, we can modify the flash message above to include the Google events data like so:
$this->Session->setFlash(Some important thing just happened.',' tracker_flash',array('google_event' => 'Important', 'google_event_action' => 'important Action', 'google_event_desc' => 'Something important just happened’));
Now all that remains is to modify the flash message layout. Here is what needs to be changed in the tracker_flash.ctp file:
What this added piece of code does is call the _gaq.push() function of Google if the $google_event variable is passed as an extra parameter in our flash message. Note that the additional variables of $google_event_action and $google_event_desc need to also be passed. And that’s all there is to tracking Google events through CakePHP flash messages. Although this tutorial focused on Google Analytics, the above code could be modified to work with any event tracking software. I should also note that this tutorial is for CakePHP 2.0. I am sure that it can be modified for CakePHP 1.3