• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple stats collection with CodeIgniter hooks

#1
Hi. After hours of googling I find an idea to track users journey throughout the site and to get some quantitative data on on the usage of the application. For example, I want to know when they get connected, the pages they visited (even the functions they called). This is how I proceed.

1)  in application/config/config.php  I replace this line :
Code:
$config['enable_hooks'] = FALSE;
by this one:
Code:
$config['enable_hooks'] = TRUE;
2) In application/config/hooks.php, I add these lines:

Code:
$hook['post_controller_constructor'] = array(
'class'    => 'Track',
'function' => 'track_user',
'filename' => 'track.php',
'filepath' => 'controllers'
3) I create my table looking like :
CREATE TABLE  `audit` (
 `auditId` int(11) NOT NULL AUTO_INCREMENT,
 `userId` varchar(100) NOT NULL,
 `adresseIp` varchar(100) NOT NULL,
 `referer` varchar(200) DEFAULT NULL,
 `controller` varchar(50) DEFAULT NULL,
 `fonction` varchar(100) DEFAULT NULL,
 `url` varchar(250) DEFAULT NULL,
 `navigator` varchar(100) NOT NULL,
 `dateAction` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `domain` varchar(100) DEFAULT NULL,
 `pageCourant` varchar(200) DEFAULT NULL,
 PRIMARY KEY (`auditId`)
)
4) Here is my controller:
PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class 
Track extends CI_Controller{
    
    
    public 
function __construct(){
        parent::__construct();
        //    Chargement des ressources pour tout le contrôleur
        $this->load->model('audit_model''AuditManager');
        
    
}
 
   public function track_user(){
 
      // Start off with the session stuff we know
 
       $options_echappees_audit = array();
 
       $options_non_echappees_audit = array();
 
 
       // getting the user
 
      $options_echappees_audit['agentId'] = $this->session->userdata('login');
 
 
    // getting the client IP address (we first asks whether it is behind a proxy)
 
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
 
         $options_echappees_audit['adresseIp'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
 
     }
 
     elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
 
          $options_echappees_audit['adresseIp' $_SERVER['HTTP_CLIENT_IP'];
 
     }
 
     else {
 
       $options_echappees_audit['adresseIp'] = $_SERVER['REMOTE_ADDR'];
 
     }
 
      //getting client's domain
 
      // $options_echappees_audit['domaine'] = gethostbyaddr($options_echappees_audit['adresseIp']);

 
 
     $options_echappees_audit['navigator'] = $_SERVER['HTTP_USER_AGENT'];

 
    // REFERER
 
     if (isset($_SERVER['HTTP_REFERER'])) {
 
         if (eregi($_SERVER['HTTP_HOST'], $_SERVER['HTTP_REFERER'])) {
 
               $options_echappees_audit['referer'] ='';
 
          }
 
          else {
 
              $options_echappees_audit['referer'] = $_SERVER['HTTP_REFERER'];
 
          }
 
     }
 
     else {
 
          $options_echappees_audit['referer'] ='';
 
    }

 
    // active page 
 
    if ($_SERVER['QUERY_STRING'] == "") {
 
        $options_echappees_audit['pageCourant'] = $_SERVER['PHP_SELF'];
 
     }
 
     else {
 
       $options_echappees_audit['pageCourant'] = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
 
     }
 
 
     // Next up, we want to know what page we're on, use the router class
 
     $options_echappees_audit['controller'] = $this->router->class;
 
     $options_echappees_audit['fonction'] = $this->router->method;

 
 
    // We don't need it, but we'll log the URI just in case
 
     $options_echappees_audit['url'] = uri_string();

 
    // And write it to the database
 
    $this->AuditManager->create($options_echappees_audit$options_non_echappees_audit);
 
  }
 



But I get this error:
Unable to locate the specified class: Session.php


Why? Please help.
Here is the tutorial i follow:http://7degrees.co.uk/blog/simple-stats-...ter-hooks/
Reply

#2
Please increase your log_treshold in your config and paste here a log
Reply

#3
(07-13-2015, 02:10 AM)gadelat Wrote: Please increase your log_treshold in your config and paste here a log

Thanks gadelat. but i'm a newbie in CI. Would you like me to post the contents of the config file?
Reply

#4
You don't follow the tutorial.
You can't extend CI_Controller.
Do you want to write tests for your apps? Our book, CodeIgniter Testing Guide would help you.
Reply

#5
(07-13-2015, 03:55 AM)kenjis Wrote: You don't follow the tutorial.
You can't extend CI_Controller.


Thank you a lot. I did not notice it. Now it works. thank you
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.