{"id":456,"date":"2009-08-31T13:31:28","date_gmt":"2009-08-31T17:31:28","guid":{"rendered":"http:\/\/www.whoisgregg.com\/blog\/?p=456"},"modified":"2009-08-31T13:32:24","modified_gmt":"2009-08-31T17:32:24","slug":"hacking-lessn-to-track-statistics","status":"publish","type":"post","link":"http:\/\/www.whoisgregg.com\/blog\/2009\/08\/hacking-lessn-to-track-statistics\/","title":{"rendered":"Hacking Lessn To Track Statistics"},"content":{"rendered":"<p>When <a href=\"http:\/\/shaun.in\/man\/\">Shaun Inman<\/a> shared his <a href=\"http:\/\/www.shauninman.com\/archive\/2009\/08\/17\/less_n\">URL shortening PHP script Lessn<\/a>, I was pretty excited.<\/p>\n<p>Right now I use <a href=\"http:\/\/kl.am\/\">kl.am<\/a>, a free service from <a href=\"http:\/\/raven-seo-tools.com\/\">Raven SEO Tools<\/a>, for all my URL shortening needs. It&#8217;s awesome and 99.5% perfect. But I&#8217;ve really wanted to host my own for three reasons:<\/p>\n<ol>\n<li>I want bulk uploads with custom short URLs so I can have predictable, product SKU short URLs.<\/li>\n<li>I want to be able to repoint a short URL if it&#8217;s old URL changes or 404s.<\/li>\n<li>I don&#8217;t want to compete with other users for custom URLs.<\/li>\n<\/ol>\n<p>Unfortunately, rolling my own will mean I miss out on all the amazing coolness of kl.am. Mine wouldn&#8217;t be as slick or easy to use. However, Lessn is slick and easy to use. But it doesn&#8217;t do custom URLs and it doesn&#8217;t track how often people visit a short URL. So I added those features.<\/p>\n<h3>Tracking clicks<\/h3>\n<p>In the file \/-\/db.php, add this statement to create a second table:<\/p>\n<blockquote><p><code>mysql_query('CREATE TABLE IF NOT EXISTS `'.DB_PREFIX.'stats` ( '.<br \/>\n\t'`id` int(11) unsigned NOT NULL auto_increment, '. <br \/>\n\t'`url_id` int(11) unsigned NOT NULL, '. <br \/>\n\t'`ts` datetime NOT NULL, '. <br \/>\n\t'`ua` text character set utf8 collate utf8_unicode_ci NOT NULL, '. <br \/>\n\t'`ref` text character set utf8 collate utf8_unicode_ci NOT NULL, '. <br \/>\n\t'`ip` int(4) unsigned NOT NULL, '. <br \/>\n\t'PRIMARY KEY  (`id`), '. <br \/>\n\t'KEY `url_id` (`url_id`) '. <br \/>\n\t') ENGINE=MyISAM DEFAULT CHARSET=utf8;');<\/code><\/p><\/blockquote>\n<p>Then, in the \/index.php file, add this line right after the header(&#8216;Location:&#8217;); line:<\/p>\n<blockquote><p><code>mysql_query(\"INSERT INTO `\".DB_PREFIX.\"stats` SET `url_id`='\".$row['id'].\"', `ts`='\".date(\"Y-m-d H:i:s\").\"', `ua`='\".mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']).\"', `ref`='\".mysql_real_escape_string($_SERVER['HTTP_REFERER']).\"', `ip`='\".sprintf(\"%u\", ip2long($_SERVER['REMOTE_ADDR'])).\"' \");<\/code><\/p><\/blockquote>\n<p>To test, go visit one of your short URLs, and then go check the stats table. You should see a new entry in the table.<\/p>\n<p>Now, how you choose to display the data from this table is up to you. :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When Shaun Inman shared his URL shortening PHP script Lessn, I was pretty excited. Right now I use kl.am, a free service from Raven SEO Tools, for all my URL shortening needs. It&#8217;s awesome and 99.5% perfect. But I&#8217;ve really wanted to host my own for three reasons: I want bulk uploads with custom short&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/posts\/456"}],"collection":[{"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/comments?post=456"}],"version-history":[{"count":7,"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"predecessor-version":[{"id":463,"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions\/463"}],"wp:attachment":[{"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.whoisgregg.com\/blog\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}