rafaell-lycan/sabesp-mananciais-api

View on GitHub
src/api/middlewares/analytics/index.ts

Summary

Maintainability
A
0 mins
Test Coverage
import ua from 'universal-analytics'
import { Request, Response, NextFunction } from 'express'

export const analytics = ({ headers, path }: Request, _res: Response, next: NextFunction) => {
  if (process.env.ANALYTICS) {
    const visitor = ua(process.env.ANALYTICS)
    const options = {
      dp: path,
      dh: headers.host,
      uip: headers['x-forwarded-for'] || headers['x-real-ip'],
      ua: headers['user-agent'],
    }

    visitor.pageview(options).send(() => next())
  }

  next()
}