8bitOrange

  • Archive
  • RSS
  • Ask me anything

Nerd Post: User tracking in CakePHP

So with this portal we are building one of the things we wanted to do was track users actions in an organized useful way. Why? To see where people are clicking, to be able to see what they are doing when they call and need help, and to help them remember and track their own actions over days. So here is what it does, very minimal, very lightweight and very simple:

    if($this->Auth->user('User.id')){
        CakeLog::config('custom_path', array(
            'engine' => 'FileLog',
            'path' => APP . 'logs' . DS . $this->Auth->user('User.email') . DS
        ));
                
        $what = null;        
        $url = $this->params['url']['url'];
        $what .= $url;
            
        if(isset($this->data)){
            $changed = serialize($this->data);
            $what .= '.:||:.' . $changed;
        }
            
        CakeLog::write('activity', $what);
    }	

So what’s happening? First we check to see if there is a user logged in, if someone is, we setup our custom pathing to create a folder in the logs folder with the users email, so that we can easily and legibly understand who’s logs are where. Then we set the url of where they are and check if they are submitting any data. If they are, we serialize that array, attach it to the url with a unique symbol that we can seperate the data by and then we write that data to the log. This is fired in the beforeFilter of the app_controller.

What we end up with is something like this:

2010-06-03 10:36:11 Activity: items/edit/1.:||:.a:1:{s:4:”Item”;a:6:{s:2:”id”;s:1:”1”;s:4:”name”;s:6:”Portal”;s:3:”url”;s:6:”Portal”;s:6:”parent”;s:0:”“;s:5:”Group”;s:1:”1”;s:4:”User”;s:1:”6”;}}

Not to bad right? We get a date, a marker, the url and the data that moved through. Now we have an “actions” controller that will let us sift through this data and use it as needed. When I get that finished out I will post it to my github account with the other things we have come up with in this project.

    • #code
    • #action
    • #logging
    • #cakephp
  • 1 year ago
  • 5
  • Permalink
  • Share
    Tweet

5 Notes/ Hide

  1. physicsforpoets reblogged this from 8bitorange
  2. physicsforpoets liked this
  3. 8bitorange posted this
← Previous • Next →

About

I'm a nerd lost on the internet. I build things with code and write things with words. Fork me on GitHub

Pages

  • I am Matt Caron and I am addicted to the internet
  • Code Blog
  • Resume
  • Stack Overflow
  • My Photography
  • Nerd Appropriate
  • I'm Google-able

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr