In my Previous post I provided some ideas for how with a bit of PHP programming magic you can add some oomf to your ads. I got a bit of feedback that people would like more ideas, so here I am with a part 2!
First up I was asked what we can do about recognising first-time readers versus regular readers. The idea is a first timer is more likely to click ads and less likely to sign up, while a regular reader is more likely to subscribe to your feed and also more likely to get annoyed by over the top advertising.
The key to recognising repeat visits versus first time visits is Cookies and sessions.
Take a look at the code below. The first block of code handles cookies, the second sessions.
Cookies are small pieces of text that can be passed between the visitors browser and your blog. They can last until the browser is closed (sometimes called a session cookie) or for a time period that you set through setting the expiry parameter (sometimes called a permanent cookie, though obviously it is anything but).
Sessions are a built in way for PHP to help you manage visitor information based around an automatic session cookie (or sometimes those crazy looking session ID urls that search engines hate so much). Effectively each visitor gets a unique identifier and a space in the web servers memory that expires when the visitor closes their browser or when 20 minutes is up since their last page view.
In the first block we work out our expiry time stamp by working out the seconds, minutes, hours, days etc to come to our desired length of time. If there is already a cookie present we know that they have visited before, otherwise they are a new visitor and we need to drop a cookie onto their machine. We give it a name and a value (just ‘true’ right now) plus the expiry. We could also give it a domain and path in case their might be confusion over your blog URL.
So there we can detect new versus existing visitors. Why bother with sessions?
Well, it occurred to me that someone who visits one page and naffs off might well be unlikely to be loyal but someone who visits for the first time and looks at more than a couple of pages might just be the sort of visitor you are looking for?
This session code counts how many pages they have viewed in this visit, combined the two pieces of code can allow you to work out some logic where the advertising is heavy if they are a first time visitor and have visited under X pages.
Then I thought “hang on Chris, remember last time – not all first time visits are created equal!”.
As we discussed in the previous post, a visit from another blog is going to be a better visit than one from a search engine. Thankfully it is quite simple to detect the majority of search engine visitors with a variation on the following code.
stristr() function looks in the referer environment variable (this time case insensitively which is more appropriate) and looks to see if they are being referred from Google (for example). This could be even more useful now we can combine it with “and are also a first time visitor”.
If they are visiting from Google then it could be useful to detect the search query they used to find you. We can do this using “regular expressions” as explained below.
This could be useful to pre fill blog search boxes or apply as keywords to chitika ads. Or you might make certain advertising more prominent if certain words are in the search phrase or you might put up a big message saying “looking for cheap widgets?, they are right over here –>”.
Again we look at the referrer, first to see if the visitor is coming from Google, then using a regular expression to discover the search phrase. Google puts the search in a parameter that looks like “q=search”, each search engine uses a different parameter so you would need to tweak for each.
Regular expressions are like formulas for matching a piece of text. This expression says “give me the text that is between ‘q=’ and the end of the line or ‘&'”. This result is put into our $search variable and displayed.
Final idea for this post is detecting the visitors country.
Why would you want to do this? The use I have put this to is to customise amazon affiliate links. I discovered people in the UK didn’t like to order from Amazon.com, they wanted to order from the UK store so would click the link, pull out the ISBN or ASIN and paste it into the UK store to make their purchase. Lost sales!
Detecting the visitors country is quite easy using the Maxmind GeoIP Database. It is pretty accurate too even with the free version, apart from AOL users are misidentified due to the way their network is setup. Can’t have everything I guess.
The code above includes the GeoIP code (making sure it is only included once) and tells it where it can find the IP database (you would need to change that path to where you uploaded it).
As an aside, putting your code into includes this way is a really good idea, especially if you are going to upgrade your blog or templates any time soon!
The code next translates the visitors IP address into a country. If the country returned is “GB” then they are shown the British version of the Amazon button, if not they are shown the Amazon USA version. Obviously you could add on code for “CA”, “FR”, etc quite easily.
So that’s it for this post, do let me know if you have other ideas of advertising or other types of template logic you might find useful.