Welcome Guest, Not a member yet? Register   Sign In
MeNeedz Visitor tracking
#1

[eluser]davidbehler[/eluser]
Recently I was wondering about the number of hits Google Analytics shows for one my pages (http://www.rapidseries.tv) and wrote my own small tracking script to count unique hits.

At first it was really simple, insert a new entry into a database table for each unique combination of ip address, user agent and date and additionally to that saving the url the user was viewing and where he was coming from (referrer).

But then I got more interested and wanted to make something more complex. I'm still working on it, but right now I can track a users movement upon my page and distinguish between bots and normal users and wether users come from a search engine (only checking for google right now) or from somewhere else.

It's quite easy to use, only need to call one method to add a hit to the database. But let's start with the code first before we talk about using it:

Right now there are two tables needed to make this work:
Code:
CREATE TABLE IF NOT EXISTS `visitor` (
  `visitor_id` bigint(20) NOT NULL auto_increment,
  `visitor_agent` varchar(255) collate utf8_unicode_ci NOT NULL,
  `visitor_platform` varchar(255) collate utf8_unicode_ci NOT NULL,
  `visitor_user_agent_string` varchar(255) collate utf8_unicode_ci NOT NULL,
  `visitor_ip_address` varchar(255) collate utf8_unicode_ci NOT NULL,
  `visitor_is_mobile` tinyint(1) NOT NULL,
  `visitor_is_browser` tinyint(1) NOT NULL,
  `visitor_is_robot` tinyint(1) NOT NULL,
  PRIMARY KEY  (`visitor_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;


CREATE TABLE IF NOT EXISTS `visit` (
  `visit_id` bigint(20) NOT NULL auto_increment,
  `visit_visitor_id` bigint(20) NOT NULL,
  `visit_visit_date` datetime NOT NULL,
  `visit_is_external_referral` tinyint(1) NOT NULL,
  `visit_is_search_referral` tinyint(1) NOT NULL,
  `visit_referrer` varchar(255) collate utf8_unicode_ci NOT NULL,
  `visit_uri` varchar(255) collate utf8_unicode_ci NOT NULL,
  `visit_entry_visit_id` bigint(20) NOT NULL,
  `visit_is_direct_access` tinyint(1) NOT NULL,
  PRIMARY KEY  (`visit_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Table names can be changed at will as you can set them in the model.

Speaking of the model, you can download it from here: http://www.davidbehler.de/download/tracker_model.zip

There are multiple ways for you to use this:

If you have your own controller that all other controllers extend, e.g. MY_Controller, you can put it in the constructor:
Code:
function __construct()
{
  parent::__construct();
  $this->load->library('user_agent');
  $this->load->model('Tracker_model');
  $this->Tracker_model->add_visit();
}

You can even have it all be done automatically by setting
Code:
var $auto_add_visit = TRUE;
in the model and then adding the model and the user_agent library to config/autoload.php
Code:
$autoload['libraries'] = array('database', 'user_agent');
$autoload['model'] = array('tracker_model');

Right now there is only one function to help you analyse the collected data, but I'm working on more:
Code:
this->tracker_model->get_hits();
This function takes 3 parameters:
- $from_date: The begin of the period in Y-m-d format (defaults to yesterday - 1 month)
- $to_date: The end of the period in Y-m-d format (defaults to yesterday)
- $unique: Only show unique hits or not
Examples
Code:
// #1
$this->tracker_model->get_hits(FALSE, FALSE, TRUE);
// This would return all unique hits from yesterday -1 month till yesterday

// #2
$this->tracker_model->get_hits('2010-01-01', '2010-01-20', FALSE);
// This would return all hits from Januar 1st 2010 till January 20th 2010

// #3
$this->tracker_model->get_hits(date('Y-m-d'), date('Y-m-d'), TRUE);
// This would return all unique hits of today
Hits are grouped by year, month and day and you will get a row for each day where there was atleast one hit. Days without hits are currently not accounted for ... I might implement that later.

So tell me what you think. I thought this might be usefull Smile

David


Messages In This Thread
MeNeedz Visitor tracking - by El Forum - 01-24-2010, 05:49 PM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 03:30 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 03:50 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 04:40 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:09 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:15 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:20 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:28 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:33 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:54 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 06:00 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 06:45 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 06:54 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 06:59 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 07:08 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 07:19 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 09:41 AM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:20 PM
MeNeedz Visitor tracking - by El Forum - 01-25-2010, 05:22 PM
MeNeedz Visitor tracking - by El Forum - 01-26-2010, 01:02 AM
MeNeedz Visitor tracking - by El Forum - 01-26-2010, 04:10 AM
MeNeedz Visitor tracking - by El Forum - 01-26-2010, 04:41 AM
MeNeedz Visitor tracking - by El Forum - 01-26-2010, 04:55 AM
MeNeedz Visitor tracking - by El Forum - 01-27-2010, 04:54 PM
MeNeedz Visitor tracking - by El Forum - 02-18-2010, 05:22 AM
MeNeedz Visitor tracking - by El Forum - 02-18-2010, 05:31 AM
MeNeedz Visitor tracking - by El Forum - 02-18-2010, 05:57 AM
MeNeedz Visitor tracking - by El Forum - 02-18-2010, 11:23 AM
MeNeedz Visitor tracking - by El Forum - 03-02-2010, 10:51 AM
MeNeedz Visitor tracking - by El Forum - 09-18-2013, 04:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB