I have it in a site/ready.php (if you don't have this file just create one)
function bot_detected() {
if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
return TRUE;
} else {
return FALSE;
}
}
if($session->get('visit_counter_flag') === 0 && !$user->isSuperuser()) {
if (bot_detected()) return;
/* if the user is NOT logged in and not counted */
/* turn of output formating so PW do not give an error when we change the value */
$page->of(false);
/* save the visitor_counter field */
$page->set('visitCounter', ($page->visitCounter ?? 0) + 1);
$page->save('visitCounter', array('quiet' => true, 'uncacheAll' => false, 'resetTrackChanges' => false));
/* turn on output formating so PW work as it should */
$page->of(true);
/* set a visit counter flag to 1 so next load do not count */
$session->set('visit_counter_flag', 1);
}
As a extra bonus this script exludes bots from statistics.