Showing Different Ads To Different Visitors

Just today, I decided to run an impromptu experiment to test if visitors who come from search engines are really more likely to click on ads. It’s considered  “common knowledge” by many bloggers who advise everyone to only show ads to search engine visitors (as a quick Google search will illustrate), but my own AdSense stats made me doubt that particular piece of advice. Hence the experiment.

So how does one show different ads to different visitors? Normally you could use the excellent Who Sees Ads plugin, but that doesn’t work for my site. I’m using the WP-SuperCache caching plugin which is incompatible with Who Sees Ads.

Instead, I wrote a JavaScript function that can analyse the HTTP referrer to distinguish between four types of visitors :

  • People who accessed your page directly, e.g. by typing in the URL (no referrer).
  • People who clicked an internal link to access the page (referrer from the same domain).
  • Search engine visitors; people who arrived via Google or Yahoo! (referrer matches one of the most popular search engines).
  • External visitors. This is basically a catch-all category for everyone who found your link on a third-party site that’s not a search engine. For example, this would include external forums and web directories.

Here’s the script :

function get_referrer_type(){
	var ref = document.referrer;
	if ( ref.length == 0 ){
		return 'none';
	function getHostname(str) {
		var re = /^(?:f|ht)tp(?:s)?\:\/\/([^\/]+)/im;
		var matches = str.match( re );
		if ( matches ){
			return matches[1].toString();
		return '';
	if ( getHostname(ref) == ){
		return 'internal';
	var SE = ['/search?', '.google.', '', 'search.', 
		'', '', '/search/', '.yahoo.',
	for ( var source in SE){
		if (ref.indexOf(SE[source]) !=-1) {
			return 'search';
	return 'external';

The function get_referrer_type() returns one of “none”, “internal”, “search” or “external” based on the referrer info.

And here’s the script that I used to show different  AdSense ads to different people :

function get_top_adsense_ad(){
	var default_slot = '1111111111';
	var code = '<'+'script type="text/javascript"><!--\ngoogle_ad_client = "pub-0000000000000000";google_ad_slot = "%ad_slot%";google_ad_width = 336;google_ad_height = 280;\n//--><'+'/script><'+'script type="text/javascript" src=""></'+'script>';
	var slots = {
		'none' : '2222222222',
		'internal' : '3333333333',
		'external' : '4444444444',
		'search' : '5555555555',
	var ref_type = get_referrer_type();
	if ( typeof(slots[ref_type]) != 'undefined' ){
		code = code.replace('%ad_slot%', slots[ref_type]);
	} else {
		code = code.replace('%ad_slot%', default_slot);
	return code;


Unlike get_referrer_type(), this script is optimized for my site/AdSense account. Don’t try using it without modification 😉 If you want to use it, replace “pub-XXXXXXXX”, the ad slot IDs and ad width/height settings with your own values (you can find them in your AdSense ad code).

As for the experiment, I’ll post the results sometime next week.

Related posts :

5 Responses to “Showing Different Ads To Different Visitors”

  1. Hi W-shadow,

    My site is one of those listed in the Google search (and many of the others ones are republishing my code), so I have an interest in your experiment.

    One of my friends recently experimented with this and found that he made more by serving ads to everyone. However, that was only the case when the percentage of traffic from search engines was 80% or more. 6 months earlier, when only 50% of visitors were from search, he ran into the smart pricing problem and made less money.

    Or of course it could be Google has changed the way they calculates things. Know one really knows with Google. Looking forward to your results!

  2. White Shadow says:

    This site gets ~70% of its traffic from search engines, so it might be somewhere in between. I’ll keep the script running for a week or so, which should let me accumulate a reasonable amount of data to draw conclusions from.

  3. […] Experiment Results document.write(get_top_adsense_ad()); Last week, I announced I would be conducting a little AdSense experiment to gain insight into how often different kinds of […]

  4. […] the original announcement and the first summary for more […]

  5. Hi White Shadow,

    This is the code i am searching for this , i found many codes in search result but none of them works with else conditions, you made it to the next level with multiple variables of ref .
    This code will help many people in web,

    Thanks for the code i will use it for my site.

Leave a Reply