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
#2

[eluser]Sbioko[/eluser]
Thanks for a great contribution. But, it's too late for me. I done such library a few weeks ago :-) but, thanks!
#3

[eluser]davidbehler[/eluser]
Well...feel free to share your libray aswell Smile
#4

[eluser]brianw1975[/eluser]
I've often thought about doing this, not because I wanted to target content or anything like that, i was just curious how people moved throughout my site.

Can't wait to implement this and see how things go.

Thanks!
#5

[eluser]hugle[/eluser]
[quote author="waldmeister" date="1264398590"]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.

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

David[/quote]

I've done smth similar but with a lot less functionality earlier, but I see yours is full of functions I may need Smile

It will be very usefull, thank youSmile
please add it to your signatureSmile))

Thanks
#6

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

You have a miss type here Smile 2010-01-10 > 2010-01-01
#7

[eluser]davidbehler[/eluser]
Miss type fixed Wink

I'll try to put it in my signature, but I'm afraid I'm running out of space Big Grin
#8

[eluser]hugle[/eluser]
[quote author="waldmeister" date="1264440007"]Miss type fixed Wink

I'll try to put it in my signature, but I'm afraid I'm running out of space Big Grin[/quote]

Imho you've forgot smth,
topic title should be:
MeNeedz Visitor Tracking Smile))

thanks for good job
#9

[eluser]davidbehler[/eluser]
Right...how could I forget that? Big Grin
#10

[eluser]Phil Sturgeon[/eluser]
Collecting this data isnt the hard part, its keeping it maintained well enough to stop your server exploding and still provide useful enough information.

If you get a few thousand hits a day your database is gonna get big.




Theme © iAndrew 2016 - Forum software by © MyBB