const KeenTracking = require('keen-tracking');

const client = new KeenTracking({
  projectId: process.env.KEEN_PROJECT_ID,
  writeKey: process.env.KEEN_WRITE_KEY
});

module.exports = (req, res, next) => {
  // Record an event
  const eventData = {
    url: req.url,
    path: req.path,
    ip: req.ip,
    method: req.method,
    ips: req.ips,
    url: req.originalUrl,
    headers: req.headers,
    params: req.params,
    protocol: req.protocol,
    query: req.query,
    hostname: req.hostname
  };
  client.recordEvent('pageviews', eventData);
  next();
}

const KeenTracking = require('keen-tracking');

const client = new KeenTracking({
  projectId: process.env.KEEN_PROJECT_ID,
  writeKey: process.env.KEEN_WRITE_KEY
});

module.exports = (req, res, next) => {

  // Setup event data
  const eventData = {
    method: req.method,
    url: {
      string: `${req.protocol}://${req.headers.host}${req.originalUrl}`
    },
    ip: req.ip,
    userAgent: {
      string: req.headers['user-agent']
    },
    referrer: {
      url: req.headers.referer
    }
  };

  // Enable keen date time addon
  eventData.keen = {
    addons: [
      {
        name: 'keen:date_time_parser',
        input: {
          date_time: 'keen.timestamp',
        },
        output: 'timestamp_info'
      }
    ]
  };

  // Only enable other keen addons if we have the required input
  const keenAddons = [
    {
      name: 'keen:url_parser',
      input: {
        url: 'url.string'
      },
      output: 'url.components'
    },
    {
      name: 'keen:ip_to_geo',
      input: {
        ip: 'ip'
      },
      output: 'location'
    },
    {
      name: 'keen:ua_parser',
      input: {
        ua_string: 'userAgent.string'
      },
      output: 'userAgent.info'
    },
    {
      name: 'keen:referrer_parser',
      input: {
        referrer_url: 'referrer.url',
        page_url: 'url.string'
      },
      output: 'referrer.info'
    }
  ];
  keenAddons.forEach(addon => {
    const hasRequiredProperties = Object
      .keys(addon.input)
      .map(key => addon.input[key])
      .every(requiredProp => requiredProp.split('.').reduce((obj, i) => obj[i], eventData));

    if(hasRequiredProperties) {
      eventData.keen.addons.push(addon);
    }
  });

  // Send event data
  client.recordEvent('pageviewsv2', eventData);
  next();
}

Make use of Keen addons for event data
Make use of Keen addons for event data
