This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from john-doherty/bot-detection
Added bot detection
- Loading branch information
Showing
6 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
var path = require('path'); | ||
var puppeteer = require('puppeteer'); | ||
var utils = require('./utils'); | ||
|
||
var url = 'file://' + path.join(__dirname, 'environment.html'); | ||
var page = null; | ||
var browser = null; | ||
|
||
describe('mixpanel-lite: .bot', function () { | ||
|
||
// create a new browser instance before each test | ||
beforeEach(async function () { | ||
|
||
// Launch a new browser instance | ||
browser = await puppeteer.launch({ | ||
headless: true, | ||
args: ['--no-sandbox', '--disable-setuid-sandbox'] | ||
}); | ||
|
||
// get page | ||
page = (await browser.pages())[0]; | ||
|
||
// Navigate to the desired URL | ||
await page.goto(url); | ||
}); | ||
|
||
afterEach(async function () { | ||
|
||
return page.evaluate(function () { | ||
return localStorage.removeItem('mixpanel-lite'); | ||
}) | ||
.then(function() { | ||
return utils.sleep(250); | ||
}) | ||
.then(function() { | ||
return browser.close(); | ||
}); | ||
}); | ||
|
||
it('should send .bot=true to /track endpoint if bot detected', async function () { | ||
|
||
var now = (new Date()).getTime(); | ||
var token = 'test-token-' + now; | ||
var eventName = 'test-event-' + now; | ||
var botUserAgents = [ | ||
'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', | ||
'Mozilla/5.0 (compatible; Bingbot/2.0; +http://www.bing.com/bingbot.htm)', | ||
'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)', | ||
'DuckDuckBot/1.0; (+http://duckduckgo.com/duckduckbot.html)', | ||
'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)', | ||
'Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)', | ||
'Sogou web spider/4.0(+http://www.sogou.com/docs/help/webmasters.htm#07)', | ||
'Mozilla/5.0 (compatible; Exabot/3.0; +http://www.exabot.com/go/robot)', | ||
'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' | ||
]; | ||
var randomUserAgent = botUserAgents[Math.floor(Math.random() * botUserAgents.length)]; | ||
|
||
// bypass automation detection | ||
page.evaluate(function (randomUa) { | ||
|
||
// override navigator.userAgent (include `HeadlessChrome` for puppeteer) | ||
Object.defineProperty(navigator, "userAgent", { | ||
get: function () { return randomUa; } | ||
}); | ||
|
||
}, randomUserAgent); | ||
|
||
await page.setRequestInterception(true); | ||
|
||
// setup mixpanel | ||
await utils.setMixpanelToken(page, token); | ||
|
||
// listen for track requests | ||
var trackRequests = utils.waitForPuppeteerRequests(page, 1, 'https://api.mixpanel.com/track'); | ||
|
||
// send event (in offline mode) | ||
await utils.sendMixpanelEvent(page, eventName); | ||
|
||
// Now wait for requests to be sent | ||
var results = await trackRequests; | ||
|
||
// decode the data and convert to JSON object so we can inspect | ||
var eventPayload = utils.getJsonPayloadFromMixpanelUrl(results[0].url); | ||
|
||
// check the tracking data we sent is correct | ||
expect(eventPayload?.event).toEqual(eventName); | ||
expect(eventPayload?.properties?.bot).toEqual(true); | ||
}); | ||
|
||
it('should NOT send .bot=true to /track endpoint if not a bot', async function () { | ||
|
||
var now = (new Date()).getTime(); | ||
var token = 'test-token-' + now; | ||
var eventName = 'test-event-' + now; | ||
|
||
await page.setRequestInterception(true); | ||
|
||
// bypass automation detection | ||
page.evaluate(function () { | ||
|
||
// override navigator.userAgent (include `HeadlessChrome` for puppeteer) | ||
Object.defineProperty(navigator, "userAgent", { | ||
get: function () { return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'; } | ||
}); | ||
|
||
// return false for `navigator.webdriver` check | ||
Object.defineProperty(navigator, "webdriver", { | ||
get: function () { return false; } | ||
}); | ||
}); | ||
|
||
// setup mixpanel | ||
await utils.setMixpanelToken(page, token); | ||
|
||
// listen for track requests | ||
var trackRequests = utils.waitForPuppeteerRequests(page, 1, 'https://api.mixpanel.com/track'); | ||
|
||
// send event (in offline mode) | ||
await utils.sendMixpanelEvent(page, eventName); | ||
|
||
// Now wait for requests to be sent | ||
var results = await trackRequests; | ||
|
||
// decode the data and convert to JSON object so we can inspect | ||
var eventPayload = utils.getJsonPayloadFromMixpanelUrl(results[0].url); | ||
|
||
// check the tracking data we sent is correct | ||
expect(eventPayload?.event).toEqual(eventName); | ||
expect(eventPayload?.properties?.bot).toBeUndefined(); | ||
}); | ||
}); |