<p>I notice that StackOverflow has a views count for each question and that these view numbers are fairly low and accurate. </p>

<p>I have a similar thing on one of my sites. It basically logs a "hit" whenever the page is loaded in the backend code. Unfortunately it also does this for search engine hits giving bloated and inaccurate numbers.</p>

<p>I guess one way to not count a robot would be to do the view counting with an AJAX call once the page has loaded, but I'm sure there's other, better ways to ignore search engines in your hit counters whilst still letting them in to crawl your site. Do you know any?</p>

[{'answer_id': 45825, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 0, 'selected': False, 'text': '<p>The reason Stack Overflow has accurate view counts is that it only count each view/user once.</p>\n\n<p>Third-party hit counter (and web statistics) application often filter out search engines and display them in a separate window/tab/section. </p>\n'}, {'answer_id': 45830, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 0, 'selected': False, 'text': '<p>You are either going to have to do what you said in your question with AJAX.  Or exclude out User-Agent strings that are known search engines.  The only sure way to stop bots are with AJAX.</p>\n'}, {'answer_id': 45835, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 4, 'selected': True, 'text': '<p>An AJAX call will do it, but usually search engines will not load images, javascript or CSS files, so it may be easier to include one of those files in the page, and pass the URL of the page you want to log a request against as a parameter in the file request.</p>\n\n<p>For example, in the page...</p>\n\n<p><a href="http://www.example.com/example.html" rel="noreferrer">http://www.example.com/example.html</a></p>\n\n<p>You might include in the head section</p>\n\n<pre><code>&lt;link href="empty.css?log=example.html" rel="stylesheet" type="text/css" /&gt;\n</code></pre>\n\n<p>And have your server side log the request, then return an empty css file. The same approach would apply to JavaScript or and image file, though in all cases you\'ll want to look carefully at what caching might take place.</p>\n\n<p>Another option would be to eliminate the search engines based on their <a href="http://en.wikipedia.org/wiki/User_agent" rel="noreferrer">user agent</a>. There\'s a big list of possible user agents at <a href="http://user-agents.org/" rel="noreferrer">http://user-agents.org/</a> to get you started. Of course, you could go the other way, and only count requests from things you know are web browsers (covering IE, Firefox, Safari, Opera and this newfangled Chrome thing would get you 99% of the way there).</p>\n\n<p>Even easier would be to use a log analytics tool like <a href="http://awstats.sourceforge.net/" rel="noreferrer">awstats</a> or a service like <a href="http://www.google.com/analytics/" rel="noreferrer">Google analytics</a>, both of which have already solved this problem.</p>\n'}, {'answer_id': 45844, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 1, 'selected': False, 'text': '<p>You don\'t really need to use AJAX, just use JavaScript to add an iFrame off screen.  KEEP IT SIMPLE<br/></p>\n\n<pre><code>&lt;script type="javascript"&gt;\ndocument.write(\'&lt;iframe src="myLogScript.php" style="visibility:hidden" width="1" height="1" frameborder="0"&gt;\');\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 45923, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 2, 'selected': False, 'text': '<p>To solve this problem I implemented a simple filter that would look at the User-Agent header in the HTTP request and compare it to a list of known robots. </p>\n\n<p>I got the robot list from <a href="http://www.robotstxt.org/dbexport.html" rel="nofollow noreferrer">www.robotstxt.org</a>. It\'s downloadable in a simple text-format that can easily be parsed to auto-generate the "blacklist".</p>\n'}, {'answer_id': 61076, 'author': 'TomG', 'author_id': 6315, 'author_profile': 'https://Stackoverflow.com/users/6315', 'pm_score': 1, 'selected': False, 'text': '<p>An extension to Matt Sheppard\'s answer might be something like the following:</p>\n\n<pre><code>  &lt;script type="text/javascript"&gt;\n  var thePg=window.location.pathname;\n  var theSite=window.location.hostname;\n  var theImage=new Image;\n  theImage.src="/test/hitcounter.php?pg=" + thePg + "?site=" + theSite;\n  &lt;/script&gt;\n</code></pre>\n\n<p>which can be plugged into a page header or footer template without needing to substitute the page name server-side.  Note that if you include the query string (window.location.search), a robust version of this should encode the string to prevent evildoers from crafting page requests that exploit vulnerabilities based on weird stuff in  URLs.   The nice thing about this vs. a regular <code>&lt;img&gt;</code> tag or <code>&lt;iframe&gt;</code> is that the user won\'t see a red x if there is a problem with the hitcounter script.\nIn some cases, it\'s also important to know the URL that was seen by the browser, before rewrites, etc. that happen server-side, and this give you that.  If you want it both ways, then add another parameter server-side that inserts that version of the page name into the query string as well.</p>\n\n<p>An example of the log files from a test of this page:</p>\n\n<pre><code>10.1.1.17 - - [13/Sep/2008:22:21:00 -0400] "GET /test/testpage.html HTTP/1.1" 200 306 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16"\n10.1.1.17 - - [13/Sep/2008:22:21:00 -0400] "GET /test/hitcounter.php?pg=/test/testpage.html?site=www.home.***.com HTTP/1.1" 301 - "http://www.home.***.com/test/testpage.html" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16"\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/45824', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3404/']