{"id":96,"date":"2006-11-18T21:01:47","date_gmt":"2006-11-18T11:01:47","guid":{"rendered":"http:\/\/www.thunderguy.com\/semicolon\/2006\/11\/18\/search-meter-wordpress-plugin-version-21\/"},"modified":"2007-08-27T14:34:28","modified_gmt":"2007-08-27T04:34:28","slug":"search-meter-wordpress-plugin-version-21","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2006\/11\/18\/search-meter-wordpress-plugin-version-21\/","title":{"rendered":"Search Meter WordPress plugin version 2.1"},"content":{"rendered":"<p>Search Meter 2.1 is a new version of my WordPress blog search statistics plugin. This version fixes the dreaded &#8220;duplicate search&#8221; problem that a few people have reported. It&#8217;s interesting that there were two causes of this problem &#8212; the first was already fixed in version 2.0, but the fix for the second variant was much trickier.<\/p>\n<p>You can go straight to the <a href=\"http:\/\/www.thunderguy.com\/semicolon\/wordpress\/search-meter-wordpress-plugin\/\">Search Meter page<\/a> to download the plugin. As well, you might be interested in reading about how I was finally able to fix the &#8220;duplicate search&#8221; problem.<\/p>\n<p>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&#8217;s statistics. The first problem was fixed the &#8220;right&#8221; 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!<\/p>\n<p>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&#8217;s why this happens.<\/p>\n<ol>\n<li>The browser requests the blog page.<\/li>\n<li>WordPress sends the search results page.<\/li>\n<li>The AdSense scripts on the page kick in and request ads from Google.<\/li>\n<li>Google requests the same search results page, so it can analyse the text and serve up relevant ads.<\/li>\n<\/ol>\n<p>So Search Meter sees two search requests: the original request, and the second one from Google.<\/p>\n<p>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&#8217;s &#8220;Recent Searches&#8221; feature.<\/p>\n<p>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&#8217;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.<\/p>\n<p>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.<\/p>\n<p>The funny thing about this is that I never actually found the definitive reason why it happens at all. My guess is that it&#8217;s caused by certain plugins or themes that call WordPress&#8217;s <code>the_posts<\/code> 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 <code>the_posts<\/code> any number of times. That (I think) is why this new version does not exhibit the problem.<\/p>\n<p>Thanks for reading &#8212; off you go now to the <a href=\"http:\/\/www.thunderguy.com\/semicolon\/wordpress\/search-meter-wordpress-plugin\/\">Search Meter page<\/a> to download the fruit of my investigations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Search Meter 2.1 is a new version of my WordPress blog search statistics plugin. This version fixes the dreaded &#8220;duplicate search&#8221; problem that a few people have reported. It&#8217;s interesting that there were two causes of this problem &#8212; the first was already fixed in version 2.0, but the fix for the second variant was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[18,83],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-web-development","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":0,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}