How do I recreate the accesslog? In Drupal 7, every time a node is viewed the following columns are populated in the {accesslog} table:


aid: unique accesslog ID
sid: browser session ID of user that visited page
title: title of page visited
path: internal path to page visited (relative to Drupal root)
url: referrer URI
hostname: hostname of user that visited the page
uid: user users.uid that visited the page
timer: time in milliseconds that the page took to load
timestamp: timestamp of when the page was visited


In Drupal 8, the accesslog was removed from the statistics module. I would like to recreate some this functionality in a Drupal 8 custom module. I need:
- A hook to fire on every node view 
- Capture the relevant information
- Write it to a database table

My plan was to copy from statistics module, because it still counts node views. However, the code is not straight forward. In Drupal 8 the mechanism appears to be:


Implement hook_ENTITY_TYPE_view(), which attaches some JavaScript and settings to the page build

/**
 * Implements hook_ENTITY_TYPE_view() for node entities.
 */
function statistics_node_view(array &amp;$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  if (!$node-&gt;isNew() &amp;&amp; $view_mode == 'full' &amp;&amp; node_is_page($node) &amp;&amp; empty($node-&gt;in_preview)) {
    $build['#attached']['library'][] = 'statistics/drupal.statistics';
    $settings = array('data' =&gt; array('nid' =&gt; $node-&gt;id()), 'url' =&gt; Url::fromUri('base:' . drupal_get_path('module', 'statistics') . '/statistics.php')-&gt;toString());
    $build['#attached']['drupalSettings']['statistics'] = $settings;
  }
}

Implement the following code in statistics.js

/**
 * @file
 * Statistics functionality.
 */

(function ($, Drupal, drupalSettings) {

  'use strict';

  $(document).ready(function () {
    $.ajax({
      type: 'POST',
      cache: false,
      url: drupalSettings.statistics.url,
      data: drupalSettings.statistics.data
    });
  });
})(jQuery, Drupal, drupalSettings);

Implement statistics.php, which where information gets written to the database

/**
 * @file
 * Handles counts of node views via AJAX with minimal bootstrap.
 */

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

chdir('../../..');
$autoloader = require_once 'autoload.php';
$kernel = DrupalKernel::createFromRequest(Request::createFromGlobals(), $autoloader, 'prod');
$kernel-&gt;boot();
$views = $kernel-&gt;getContainer()
  -&gt;get('config.factory')
  -&gt;get('statistics.settings')
  -&gt;get('count_content_views');

if ($views) {
  $nid = filter_input(INPUT_POST, 'nid', FILTER_VALIDATE_INT);
  if ($nid) {
    \Drupal::database()-&gt;merge('node_counter')
      -&gt;key('nid', $nid)
      -&gt;fields(array(
        'daycount' =&gt; 1,
        'totalcount' =&gt; 1,
        'timestamp' =&gt; REQUEST_TIME,
      ))
      -&gt;expression('daycount', 'daycount + 1')
      -&gt;expression('totalcount', 'totalcount + 1')
      -&gt;execute();
  }
}



Can anyone explain this code?
There is no official way to do your own thing in statistics.php, it is on purpose kept as light as possible.

But of course, you can duplicate its functionality and do whatever you want. The whole trick is adding a JS file that sends an uncached POST request to the server. That's the only reliable way to get access statistics for anonymous users, as those requests might be cached by the page cache or an external CDN/Varnish.

Given that you already posted all the relevant code, I'm not sure what you are asking exactly. You even linked to the d.o issue that removed that feature, which links to the change record (https://www.drupal.org/node/1900384), which in turn links to the contrib module that provides similar functionality: https://www.drupal.org/project/better_statistics. So instead of doing it yourself, you could build upon that.