is intended to bring easy to follow video tutorials to the cakephp world. It is in no way affiliated to however offers it support and encouragement where it can.
Our homepage can be found here.

If you are looking to download cakephp or find more information please visit the official website found here > Tutorials > Tracking User Activity

Tracking User Activity Clicks in CakePHP:

Introduction: This is actually a real simple operation and thus this tutorial is also very simple, so you should have your user activity tracking in no time. Today we are just going to look at how to find out who's on your cakephp site, where they came from, which models and actions they are using and what browser they are using to do all this. We won't discuss how to display all the results here, that will be Part 2 of this tutorial so for now if you wish to view your results why not use PHPMyAdmin or WebYog or something so you can see the results - For purpose of this tutorial I will be using PHPMyAdmin to show you the results.

Need Help? If you need help or get stuck with this tutorial, or just have feedback or notice something I could be doing better then please by all means let me know, you can find my loitering over at the UnOfficial CakePHP forum, so make a thread there.

Let's get going ...

Step 1: The Database

We are storing the activity logs in a MySQL database seeing as CakePHP has such good support for it, so create a mysql database if you don't have one already. I have created the database named ' example' .
If you already have a database in place for your CakePHP application then use that. Next we need to create a table in our database, I have called it 'activity'. To help you with this, below is the SQL. Just run it on your server to create the table we need.

CREATE TABLE `activity`
( `activity_id` int(11) NOT NULL auto_increment,
`user_browser` varchar(255) NOT NULL,
`user_ip` varchar(15) NOT NULL,
`created` datetime NOT NULL,
`model` varchar(50) NOT NULL,
`action` varchar(50) default NULL,
`clicked_from` varchar(200) default NULL,
PRIMARY KEY (`activity_id`)


Step 2: The Core Code - app_controller.php

O.K it is time to create the beef of our activity tracker here. Open up your app_controller.php. Copy and paste the code below right into your app_controller file. Please see the comments next to the code below, they will allow you to actually understand what's happening in the code.


class AppController extends Controller

// Below we create a function called recordActivity, you call this to trigger the activity tracker but we look at how you call it in step 3.
function recordActivity()

// The following line makes this script work for CakePHP installations that use either mod_rewrite or CakePHP's own URL shortening tricks.
$pages = str_replace("/index.php",'', $_SERVER['PHP_SELF']);
$pages = explode("/", $pages);

// You will probably all reconise this, we are just getting all the values we need to store and assigning them to CakePHP.
$this->data['Home']['model'] = $pages[2];
$this->data['Home']['action'] = $pages[3];
$this->data['Home']['user_ip'] = $_SERVER['REMOTE_ADDR'];
$this->data['Home']['user_browser'] = $_SERVER['HTTP_USER_AGENT'];
$this->data['Home']['clicked_from'] = $_SERVER['HTTP_REFERER'];

// $this->data['Home']['user_accessed'] = date("Y-m-d H:i:s");
// In the original tutorial we manually added the date of the activity but I have sinced changed the DB schema.
// Previously the datetime field we had was named 'user_activity'. It is now named 'created'.
// CakePHP knows to populate this field automatically by the name.

// O.K, now we just need to call the insert query:

// The following line is a fix by Termnial13 (thanks). It removes tracker stuffs from $this->data as it was causing some users issues.


That's it for the core code, small, simple, quick - easy. Next step, actually calling the above code into action.


Step 3: Telling CakePHP to use our core code and start recording activity

This will be a short step, believe me ;) You need to copy and paste the following code into each of the controller files that you wish to track activity for. Well actually, you can put this code anywhere you want to track activity, so if you put it in the top of your controller file, it will record usage for every action in that controller whereas, if you put it in an action within a controller file, it will only record activity for that action. In this example I have put it at the top of my home_controller.php.


// the beforeFilter() function when present, is ran before any other code that is available within that controller and/or action.
function beforeFilter()
// All we want to do is call our function from earlier, recordActivity, remember?

That's it, we're done!

No really, that is all there is too it. Now you will be recording activity on your CakePHP application. Take a look at the screen shot I have made below, it shows what the information in the database.

Allow me to elaborate ...

.. what we have here is:

- activity_id: this is just the database id that it assign each record, you can forget about this until we move onto the reviewing data tutorial.
- user_browser: this is the internet browser the user is using and hopefully their operating system too.
- user_ip: this is the IP address of the users computer on the internet
- user_accessed: this is the date and time the activity took place
- model: this is the controller the user is accessing, sorry for any confusion over the name of the column here
- action: this is the action the user is in, if they have just arrived at a the controller this will be empty.
- clicked_from: this is where they were before they accessed the current controller/action. If they typed in the URL directory, this will be NULL.

Lets look at the next click I did ....

As you can see, much of the same only this time the time is slightly later than the first, as I spent a few seconds reading the first page before I moved on. Also here i'm now in the 'test' action of the 'Home' controller, so you see the action column now has been populated. Additionally, the referring address is populated as this time I clicked I didn't manually type the URL in directly.


I hope some of you find this useful, questions, comments and feedback welcome, as I mentioned at the top of this tutorial you can find my over at the UnOfficial CakePHP Forum. See you there!

Need a CakePHP developer? Or website design services? Hire us, we wrote this tutorial.