Просмотр исходного кода

Use isbot package to detect if visitor is bot

poeti8 7 лет назад
Родитель
Сommit
adfe46b10c
1 измененных файлов с 3 добавлено и 4 удалено
  1. 3 4
      server/controllers/urlController.js

+ 3 - 4
server/controllers/urlController.js

@@ -8,6 +8,7 @@ const geoip = require('geoip-lite');
 const bcrypt = require('bcryptjs');
 const subDay = require('date-fns/sub_days');
 const ua = require('universal-analytics');
+const isbot = require('isbot');
 const {
   createShortUrl,
   createVisit,
@@ -107,7 +108,6 @@ exports.urlShortener = async ({ body, user }, res) => {
 
 const browsersList = ['IE', 'Firefox', 'Chrome', 'Opera', 'Safari', 'Edge'];
 const osList = ['Windows', 'Mac Os X', 'Linux', 'Chrome OS', 'Android', 'iOS'];
-const botList = ['bot', 'dataminr', 'pinterest', 'yahoo', 'facebook', 'crawl'];
 const filterInBrowser = agent => item =>
   agent.family.toLowerCase().includes(item.toLocaleLowerCase());
 const filterInOs = agent => item =>
@@ -124,8 +124,7 @@ exports.goToUrl = async (req, res, next) => {
   const referrer = req.header('Referer') && URL.parse(req.header('Referer')).hostname;
   const location = geoip.lookup(req.realIp);
   const country = location && location.country;
-  const isBot =
-    botList.some(bot => agent.source.toLowerCase().includes(bot)) || agent.family === 'Other';
+  const isBot = isbot(req.headers['user-agent']);
 
   let url;
 
@@ -187,7 +186,7 @@ exports.goToUrl = async (req, res, next) => {
     });
   }
 
-  if (config.GOOGLE_ANALYTICS) {
+  if (config.GOOGLE_ANALYTICS && !isBot) {
     const visitor = ua(config.GOOGLE_ANALYTICS);
     visitor
       .pageview({