IP to country geocoding in Ruby on Rails

If you read my last post you’ll know that I’m moving from google ads to banner ads for affiliate programs run by online retailers. But most retailers only target a single country. Showing ads for an American store to visitors from the UK would simply be a waste of ad space.

So I needed some way to target the ads to the visitors country. Enter geocoding. Using one of a number of online services which will take the visitors IP address and return a two letter country code for them.

In Rails there’s a handy gem which can do this, GeoKit. For IP to country coding it can use one of two services, both of which are free: hostip.info and geoplugin.com.

In Rails the process goes as follows: Firstly install the GeoKit gem:

config.gem 'geokit'

In the application.rb file for the application controller, add the following before_filter:

before_filter :get_geo_loc

Now you’re ready to create the before filter which will run before every page request. Since you don’t want to be hitting the third party servers for every request you will want to store the result of the loop-up in a cookie (in this case called geo-country).

I’ve also added the ability to send a country code with a request for testing purposes. I do this by adding a ?geo=ID to the end of a URL (replacing ID with a country code).

Testing showed one more fix was needed. GeoKit uses one of two services by default with fail over between the two. I found that one was returning the code ‘GB’ for the United Kingdom, the other was returning ‘UK’, so I added a quick fix to convert between the two.

The finished code follows:

def get_geo_loc
  cookie = true
  if params['geo']
    @geo_country = params['geo']
    cookie = false
  elsif cookies['geo_country']
    @geo_country = cookies['geo_country']
  else
    @geo_country = GeoKit::Geocoders::IpGeocoder.do_geocode(request.remote_ip).country_code
    @geo_country = 'GB' if @geo_country == 'UK'
  end
  if cookie
    cookies['geo_country'] = {:value => @geo_country, :expire =>; 30.days.from_now}
  else
    cookies.delete 'geo_country'
  end
end

If you want to see the banners in action and you’re from the US or UK, just visit http://plantality.com, if not click on http://plantality.com?geo=GB or http://plantality.com?geo=US for the USA (note at the time of writing I’ve only created ads for the UK, US ads will be added shortly).

Ditching Google

Last week I wrote a blog posting on the site about a common shrub called Spiraea Snowmound. It’s typical of posts I write on there describing a particular plant variety. The plant name is placed in the pages URL, the title, the text and even the photograph file title and alt text.

An search of google for ‘ Spiraea’ shows a paid result. So, you’d expect that google would show that in the AdSense ads on that page. No. Okay, so maybe they’ve elected not to show that at on the content network.

But surely google can pick up from the general content of the site and the article in particular that the article is about plants and show something relevant. So what is google showing? Well, a couple of days ago when I looked it  had picked up on the word ‘snowmound’ and was exclusively showing ads related to ice and snow.

Looking again now, shows that google has at least picked up that the item is about gardening (if you exclude the ad for artificial turf), but the ads are still pretty generic, with a lot of ads for local gardeners in a particular area .

I have another website I started a few years ago, blogging about model railways. Despite being a niche market, on that site google does a great job of displaying relevant ads and, per page view, I’m earning ten times what plantality pays.

So, looking for an alternative to google ads I’ve signed up with a couple of affiliate marketing companies. Now I’d always though that affiliate marketing was about marketing someone else’s scam. You know, all those really long web pages packed full of dodgy testimonials about how to cure some common but embarrassing  medical condition.

But it turns out that there are some really big and trusted players selling through affiliate marketing these days. I’ve so far seen affiliate programs for Crocus, B&Q,  Thompson & Morgan and Jersey Plants Direct, who are all big players in the mail order gardening market.

The affiliate companies I’ve signed up with, Trade Doubler and Affiliate Window are quoting EPC (earnings per thousand clicks) or around £50 ($80), or about 50p (80c) per click, which is certainly higher that a single click on a google ad.

I’m expecting that the click through rate may be slightly lower than I’d get with google. But even so, that sounds like a pretty good earnings per click and I’m now working on a geo-targeting system (so viewers get ads appropriate for their country) and banner rotater.

My Start-up Costs

With the site released I thought now would be a good time it give a breakdown of the costs of getting to this stage. To convert any costs to US$ multiply by about 1.5.

Laptop: £666.13
First up I needed a new notebook. I know I should really do development on my desktop machine, but I find that I am far more creative working in the lounge on a laptop. My old one was far too slow and never would have coped with a modern development machine. I also wanted something with the largest practical screen, so I went for a 17″ widescreen machine. However, I could have created the site without it and it will find many more uses that just plantality, so I will ignore this cost.

3rdRail: £81.65
I’m not really a command line sort of guy, so an IDE was essential.

Domain Registration: £66.32
Logo and graphic design: £187.23
Website design: £746.82

Total cost: £1082.02, or $1748.15 if you include the laptop.

I haven’t included above ongoing costs such as webhosting ($12 per month) because I don’t consider that a start up cost, or press releases, the costs of which have yet to filter through to my accounting system, but where about $339.

The costs above, of course, do not include my development time. I have no record of the amount of hours taken but from cutting the first line of code took about six months of evenings and weekends. But actually the site itself was ready after three months, and from then on I was mostly waiting for web designers.

Money at Last!!

I’m pleased to report that I got the first click on a google ad today. ‘ve now recouped about 0.017% of my expenses. Wahey!!!! I’ll admit I was getting a little worried that I’d never get a click, so this has really pleased me.

I just really wish that google would give some kind of feedback on what advert was clicked, or what page was clicked, or anything that would let me know what I did to encourage an ad click.

On a more or less unrelated note, logging in to wordpress today I noticed that I had received ten comments on the blog, and akismet had determined that ten of those where spam. It was proudly displaying a 100% success rate, though since there’s no way to know if it made any false positives, it’s hard to know how it calculates it’s success rate.

All the best, Mike.

May 2010 Stats

So, May was my first month after the proper release. Here are my stats:

Visitors: 152

Pages: 574

Active users: 6

Photos: 21

Blog posts: 15

Page rank: 0

Alexa ranking: 7,375,933

Earnings: £0.00 (about $0.00)

Not much to write home about, but then it was my first month, I only sent a press release out towards the end of the month and that was mainly to ‘old media’ editors, so it will take a while to feed through to press and readers.

My release via prweb only went out on Tuesday (1st June) and is giving me a small blip in visitors.

My thoughts at the moment:

  • I’ve been so busy at the day job over the last couple of months that I haven’t had any time to devote to the site. That’s due to change in the next week or so (and I’m writing this at home on a Thursday afternoon, which is something I haven’t been able to do for a while).
  • I’m realising that creating and generating buzz around an online community is hard. I’m certainly not going to give up on the community idea – the site as it is now, but my ideas for plantality V2.0 will make it into something which is: a) very effective at drawing in ‘long tail’ visitors, and b) of use to individual users. Whereas a community is only of use to a group of people (and a large one at best), my new developments will be something which, even if only one user signs up, he or she will be able to get great usage out of it.

As Winston Churchill once said: “Never give up. Never, never give up. Never, never, never give up.”

Starting the Marketing Offensive

It’s mid May. The site isn’t quite ready for release but, as far as I’m concerned, I can’t wait any longer and it’s time to start on some promotion.

The first step will be familiar to many mISVs – Using Al Harberg at dpdirectory.com to send a press release. Al normally sends releases to the computer press, but when I contacted him he was able to add a number of editors at some very high profile gardening magazines to the distribution list.

Stage two is to contact bloggers. I’ve been reading the advice posted by Peldi at balsamiq and have come up with a message which I hope is enticing to bloggers. To find gardening blogs I started searching for ‘garden blogs’ on google. The first few results where good but this quality rapidly went downhill.

I noticed in those search results a number of blog directories appearing, so my next stage was to search on ‘garden blog directories’. The first result was a rather barren site with most of it’s categories being empty, but the next result was much better. It was a page for the gardening category at blogged.com. 24 pages of gardening related blogs I doubt I’ll ever get through gthe entire list, and the further I get through it the more junk I’m finding, but it gives me a great starting place.

My final stage, for now, will be to send a press release through prweb. Whereas dpdirectory largely reaches traditional print media, prweb has very good reach among blogger and other new media sites. Well that’s the claim, anyway. I also hope it will garner me some high pagerank links, from the prweb site itself if nowhere else.

Creating a mini-CMS in Rails

I’m continually amazed about how fast it is developing in Rails.

When I created plantality I knew I needed some static pages for things such as privacy policy and help pages. As a temporary measure I created a controller called ‘static’ and added each page as a separate view.

This worked but it meant that every time I wanted to modify or add a page I would have to do a deployment. So, the time was nearing to send my first press release, and I wanted to create a press area for the site which would include artwork, press releases and other information about the site. The time had come to bite the bullet and write a mini-CMS for the static pages.

In 3rdRail I added a new model, Static with the fields ‘name’ (for the web address), ‘title’, ‘category’, and ‘text’. I then opened routes.rb to create the new URLs:
map.connect ‘static/edit/:page’, :controller => ‘static’, :action => ‘edit’
map.connect ‘static/create’, :controller => ‘static’, :action => ‘create’
map.connect ‘static/list’, :controller => ‘static’, :action => ‘list’
map.connect ‘static/:page’, :controller => ‘static’, :action => ‘index’
map.connect ‘press’, :controller => ‘static’, :action => ‘index’, :page => ‘press’

The first parameter, in quotes, gives the URLs and the rest of the line specifies the controller and action to use. Parts of the URL starting with a ‘:’ – :page here – are where parameters are passed in. in this case the value for the name field in the database. Note the final line which creates the URL http://plantality.com/press as a synonym for http://plantality.com/static/press

Now I went to the static controller, and added the views ‘index’ (display a page), ‘create’ (create a page), ‘edit’ (edit a page) and ‘list’ (list available pages) and then the actions to go with them.

The coding is fairly basic, with only 49 lines for the actions and each view being fairly straight forward. And within two hours I have my mini-CMS ready to go.

How not to Choose a Web Designer

When I started plantality I didn’t have either a graphic designer or web designer I could turn to. I’d seen recommendations for using Design Outpost and posted a request on there for a designer to create a logo for the site. I had a number of replies, all with great ideas, but while refining those into my finished ideal a designer called Carmen posted an entry which blew me away: it simply said everything I wanted the logo to say. The finished logo is below.

Plantlity logo

With this success I decided to to back to design outpost for a website design. This was less successful with only one entry being posted. On the upside, it was a design which I loved. I selected the design as my chosen design and began working with the designer, but it soon turned out that while her design skills where first class, her responsiveness was dreadful and it took about three weeks to get a single page to the stage where we where ready to ‘cut HTML’.

So, while I had a basic site design I was back at step one as regards finding a designer to carry on with development. After much googling I came across a British based forum for website designers. They had a sub-forum for job postings so I made my request there. This elicited quite a few responses, most of which had read the proposal and a few who clearly hadn’t, even suggesting that I use a CMS for the site.

I chose a designer and set him to work on the site but this time, while his willingness to work and responsiveness where great, he clearly had little experience at web design and lacked both creativeness and graphic skills and some of his work was just plain sloppy.

I’ve managed to salvage out of the work an almost complete site design, but there are still a few rough edges which need sorting out. So, I have gone back over the offers from the designers at http://www.webdesignerforum.co.uk/ with a different set of selection criteria and chosen another designer. I’m hoping that in web design the third time lucky rule applies.

plantality, Ruby and Rails

plantality is written in Ruby on Rails and is my first project using this language and framework. I can’t really tell you my reasons for choosing Ruby on Rails, other than the fact that there was quite a bit of buzz about both Ruby and Rails, and when I read the now deleted ‘Poignant Guide to Ruby‘ by the interestingly named ‘Why the Lucky Stiff’ it just seemed to be a language which made a lot of sense. Above all it not one the ‘curly brace’ languages like PHP and c# which I utterly detest.

My first experience with Rails was that it’s a bit like climbing Mount Everest without and ice-pick. There’s certainly a lot which needs to be learnt in one go before you can get anywhere serious with it. But once you’re over the initial learning curve you realise it is a lot tidier than other solutions such as PHP (the only other web development language I have experience in).

Since I’m not a command line kind of guy wanted an IDE to help me. I chose 3rdRail by Embarcadero, but having used of for six months I would have a hard time recommending it. It is very slow, has many bugs which make it feel like a beta release and I am constantly being annoyed by the code completion features (for some reason if I type an apostrophe into plain text in a view it insists on adding a second one. Grrr). If I was starting over again I would take a serious look at RubyMine from JetBrains which, judging by it’s marketing materials looks like it fulfils on what 3rdRail promised. Once I have proved that plantality.com is a viable proposition I intend to take a serious look at switching.

Rudabet and plantality

plantality.com is my attempt at creating a dynamic web 2.0 style user driven website. Rudabet.com (this site) is my behind the scenes blog about plantality.com.

plantality.com is an interactive website for gardeners to relax and socialise. The first release contains social networking features ala facebook including a social activity stream, messaging, status updates, photo uploads and blogging. The website is entirely bootstrapped.

The total costs before release are less than $2000 and I intend to make it profitable as soon as possible and from then on it should be self funding. I am the sole designer and developer but I will be sub-contracting out for those areas where I do not have the skill or time, such as graphic and website design.

On Rudabet.com I intend to blog about my experiences creating and running plantality.com. I’ve recently been reading Patrick McKenzies blog at Kalzumeus.com and intend to follow his model of posting about marketing, technical issues and, above all, being open about costs and revenues. If you are interested in bootstrapping a website, then I hope you will find something of interest in here.

Mike