Search Meter WordPress plugin version 2.1

Saturday, 18 November 2006

Search Meter 2.1 is a new version of my WordPress blog search statistics plugin. This version fixes the dreaded “duplicate search” problem that a few people have reported. It’s interesting that there were two causes of this problem — the first was already fixed in version 2.0, but the fix for the second variant was much trickier.

You can go straight to the Search Meter page to download the plugin. As well, you might be interested in reading about how I was finally able to fix the “duplicate search” problem.

As I said, there were two separate problems that had the same cause: a single search would show up more than once in Search Meter’s statistics. The first problem was fixed the “right” way: I created a diagnostic version of the plugin, tried it out, discovered the problem, and fixed it. The problem turned out to be Google AdSense!

Every time somebody searches a WordPress blog, it return a search results page. If this page has AdSense ads on it, then they will get a duplicate request. Here’s why this happens.

  1. The browser requests the blog page.
  2. WordPress sends the search results page.
  3. The AdSense scripts on the page kick in and request ads from Google.
  4. Google requests the same search results page, so it can analyse the text and serve up relevant ads.

So Search Meter sees two search requests: the original request, and the second one from Google.

The way I decided to differentiate them is that the Google request naturally comes with no HTTP referrer. So I simply made Search Meter ignore searches made that have no referrer. Problem solved, and as a bonus the diagnostic code I wrote became Search Meter’s “Recent Searches” feature.

I released this fix in Search Meter 2.0, and patted myself on the back for fixing the problem. Not so fast. People still reported the problem, and I even saw it on one of my own blogs. These duplicate requests were exactly the same, even with the same referrer and source IP address, so it wasn’t caused by a third party like Google. In fact, it was almost as if the plugin was being called more than once for each single request.

The fix for this was obvious in retrospect: simply count the number of times the plugin gets called within a given request, and ignore the second and subsequent calls. This is the main change in this new version; testers have reported that it fixes the problem nicely.

The funny thing about this is that I never actually found the definitive reason why it happens at all. My guess is that it’s caused by certain plugins or themes that call WordPress’s the_posts function. Search Meter works by hooking a filter into this function, but previous versions assumed that the function was only called once per request. Normally this is true, but there nothing to stop a theme or plugin calling the_posts any number of times. That (I think) is why this new version does not exhibit the problem.

Thanks for reading — off you go now to the Search Meter page to download the fruit of my investigations.

Tags: ,

6 comments

You can leave a comment, or trackback from your own site.

  1. I’m using version 2.5 still I’m getting duplicate searches how to avoid these I’m using search meter in the right footer please visit my site and can you tell me the solution

Leave a comment