Created
March 20, 2024 01:48
-
-
Save ak--47/e79f27aa7464d3b2c078877d6f5d09ab to your computer and use it in GitHub Desktop.
blocking user agents mixpanel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const Mixpanel = require('mixpanel') | |
const mixpanel = mixpanel.init('your token') | |
var BLOCKED_UA_STRS = [ | |
'googlebot', | |
'googleweblight', | |
'mediapartners-google', | |
'storebot-google' //etc... list can be expanded as necessary. | |
]; | |
app.use((req, res, next) => { | |
const userAgent = req.headers['user-agent'].toLowerCase(); | |
for (let blockedUA of BLOCKED_UA_STRS) { | |
if (!userAgent.includes(blockedUA)) { | |
//user is not a bot | |
mixpanel.track('foo') | |
} | |
} | |
next(); | |
}); | |
const PORT = 3000; | |
app.listen(PORT, () => { | |
console.log(`Server running on port ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment