The first and foremost need is to have access logs, Since in this tutorial, we’ll be logging in MySQL, we need to have a table in which we can maintain a good access log.
For an access log here are the following necessary data fields
- Accessed URL
- Referer to the Acessed URL
- User Agent accessing the document
- IP of the Client
- Datetime stamp
Access Log Table
Here is the table view_log
CREATE TABLE `view_log` (
`view_id` int(11) NOT NULL auto_increment,
`view_url` varchar(255) NOT NULL default '',
`view_ref` varchar(255) NOT NULL default '',
`view_ua` varchar(255) NOT NULL default '',
`view_ip` varchar(20) NOT NULL default '',
`view_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`view_id`)
) TYPE=MyISAM COMMENT='Saves the log of viewing log'
Search Engines Table
Now we need to have a list of the Search Engines that link us to the site. I’ve taken the most popular of them, and the ones that I know..
The most important field that we have in this table is the se_regex, this field stores the regular expression, that will parse out the keyword from the referered URL.
CREATE TABLE `search_engines` (
`se_id` int(11) NOT NULL auto_increment,
`se_name` varchar(255) NOT NULL default '',
`se_regex` varchar(255) NOT NULL default '',
PRIMARY KEY (`se_id`)
) TYPE=MyISAM COMMENT='Search Engines'
Now that we have a table structure in place lets populate some data into it.
I’ll explain the regular expressions for the engines in the next page.
INSERT INTO search_engines VALUES (1,'Google','/.*google.*?q=([^&]*).*/i');
INSERT INTO search_engines VALUES (2,'Yahoo','/.*yahoo.*?p=([^&]*).*/i');
INSERT INTO search_engines VALUES (3,'MSN','/.*msn.*?q=([^&]*).*/i');
INSERT INTO search_engines VALUES (4,'Netscape','/.*netscape.*?search=([^&]*).*/i');
INSERT INTO search_engines VALUES (5,'AOL','/.*aol.*?query=([^&]*).*/i');
INSERT INTO search_engines VALUES (6,'Alexa','/.*alexa.*?q=([^&]*).*/i');
INSERT INTO search_engines VALUES (7,'AltaVista','/.*altavista.*?q=([^&]*).*/i');
INSERT INTO search_engines VALUES (8,'AllTheWeb','/.*alltheweb.*?q=([^&]*).*/i');
INSERT INTO search_engines VALUES (9,'A9','/.*a9.*?search=([^&]*).*/i');
INSERT INTO search_engines VALUES (10,'DMoz','/.*dmoz.*?search=([^&]*).*/i');
INSERT INTO search_engines VALUES (11,'Lycos','/.*lycos.*?query=([^&]*).*/i');
INSERT INTO search_engines VALUES (12,'Terra Lycos','/.*terra.*?query=([^&]*).*/i');
INSERT INTO search_engines VALUES (13,'Alexa','/.*alexa.*?q=([^&]*).*/i');
INSERT INTO search_engines VALUES (14,'Rediff','/.*rediff.*?MT=([^&]*).*/i');
Keyword Statistics Table
This table will hold keyword and its hit counter.
CREATE TABLE `keyword_search_stats` (
`keyword_search_stats` int(11) NOT NULL auto_increment,
`keyword` varchar(255) default NULL,
`keyword_count` int(11) default NULL,
`update` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`keyword_search_stats`),
KEY `keyword_index` (`keyword`)
) TYPE=MyISAM
I’m creating an index on the keyword column, so that it is easier to search the keywords
Search Engine Statistics Table
This table will hold Search Engine and no. of referals from it.
CREATE TABLE `search_engine_stats` (
`search_engine_stats_id` int(11) NOT NULL auto_increment,
`search_engine` varchar(255) default NULL,
`search_engine_count` int(11) NOT NULL default '0',
`update` datetime default NULL,
PRIMARY KEY (`search_engine_stats_id`)
) TYPE=MyISAM