Skip to content

Commit 8c4b464

Browse files
authored
Added uad to bot detection (#467)
* user agent data * removed member comment * added to internals
1 parent 2997069 commit 8c4b464

File tree

6 files changed

+105
-75
lines changed

6 files changed

+105
-75
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 23.12.4
2+
- Enhanced userAgentData detection for bot filtering
3+
14
## 23.12.3
25
- Added bot detection for workers
36
- Added the ability to clear stored device IDs in the workers

cypress/integration/bridge_utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function initMain(name, version) {
1616
}
1717

1818
const SDK_NAME = "javascript_native_web";
19-
const SDK_VERSION = "23.12.3";
19+
const SDK_VERSION = "23.12.4";
2020

2121
// tests
2222
describe("Bridged SDK Utilities Tests", () => {

cypress/integration/user_agent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ describe("User Agent tests ", () => {
5555
hp.haltAndClearStorage(() => {
5656
initMain();
5757
// setting ua value to strings that can pass the regex test
58+
expect(Countly._internals.userAgentSearchBotDetection("")).to.equal(false);
5859
expect(Countly._internals.userAgentSearchBotDetection("123")).to.equal(false);
5960
expect(Countly._internals.userAgentSearchBotDetection("Googlebot")).to.equal(true);
6061
expect(Countly._internals.userAgentSearchBotDetection("Google")).to.equal(false);
6162
expect(Countly._internals.userAgentSearchBotDetection("HeadlessChrome")).to.equal(true);
6263
expect(Countly._internals.userAgentSearchBotDetection("Chrome-Lighthouse")).to.equal(true);
64+
expect(Countly._internals.userAgentSearchBotDetection("Lighthouse")).to.equal(true);
6365
});
6466
});
6567
});

lib/countly.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
statusCode: "cly_hc_status_code",
197197
errorMessage: "cly_hc_error_message"
198198
});
199-
var SDK_VERSION = "23.12.3";
199+
var SDK_VERSION = "23.12.4";
200200
var SDK_NAME = "javascript_native_web";
201201

202202
// Using this on document.referrer would return an array with 15 elements in it. The 12th element (array[11]) would be the path we are looking for. Others would be things like password and such (use https://regex101.com/ to check more)
@@ -519,23 +519,38 @@
519519
return uaOverride;
520520
}
521521
var ua_raw = navigator.userAgent;
522-
// check if userAgentData is supported and userAgent is not available, use it
522+
// check if userAgentData is supported and userAgent is not available, then use it
523523
if (!ua_raw) {
524-
if (navigator.userAgentData) {
525-
// turn brands array into string
526-
ua_raw = navigator.userAgentData.brands.map(function (e) {
527-
return e.brand + ":" + e.version;
528-
}).join();
529-
// add mobile info
530-
ua_raw += navigator.userAgentData.mobile ? " mobi " : " ";
531-
// add platform info
532-
ua_raw += navigator.userAgentData.platform;
533-
}
524+
ua_raw = currentUserAgentDataString();
534525
}
535526
// RAW USER AGENT STRING
536527
return ua_raw;
537528
}
538529

530+
/**
531+
* Forms user agent string from userAgentData by concatenating brand, version, mobile and platform
532+
* @memberof Countly._internals
533+
* @param {string} uaOverride - a string value to pass instead of ua value
534+
* @returns {string} currentUserAgentString - user agent string from userAgentData
535+
*/
536+
function currentUserAgentDataString(uaOverride) {
537+
if (uaOverride) {
538+
return uaOverride;
539+
}
540+
var ua = "";
541+
if (navigator.userAgentData) {
542+
// turn brands array into string
543+
ua = navigator.userAgentData.brands.map(function (e) {
544+
return e.brand + ":" + e.version;
545+
}).join();
546+
// add mobile info
547+
ua += navigator.userAgentData.mobile ? " mobi " : " ";
548+
// add platform info
549+
ua += navigator.userAgentData.platform;
550+
}
551+
return ua;
552+
}
553+
539554
/**
540555
* Returns device type information according to user agent string
541556
* @memberof Countly._internals
@@ -547,7 +562,7 @@
547562
// TODO: refactor here
548563
if (uaOverride) {
549564
userAgent = uaOverride;
550-
} else if (navigator.userAgentData.mobile) {
565+
} else if (navigator.userAgentData && navigator.userAgentData.mobile) {
551566
return "phone";
552567
} else {
553568
userAgent = currentUserAgentString();
@@ -581,9 +596,17 @@
581596
*/
582597
function userAgentSearchBotDetection(uaOverride) {
583598
// search bot regexp
584-
var searchBotRE = /(CountlySiteBot|nuhk|Googlebot|GoogleSecurityScanner|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver|bingbot|Google Web Preview|Mediapartners-Google|AdsBot-Google|Baiduspider|Ezooms|YahooSeeker|AltaVista|AVSearch|Mercator|Scooter|InfoSeek|Ultraseek|Lycos|Wget|YandexBot|Yandex|YaDirectFetcher|SiteBot|Exabot|AhrefsBot|MJ12bot|TurnitinBot|magpie-crawler|Nutch Crawler|CMS Crawler|rogerbot|Domnutch|ssearch_bot|XoviBot|netseer|digincore|fr-crawler|wesee|AliasIO|contxbot|PingdomBot|BingPreview|HeadlessChrome|Chrome-Lighthouse)/;
585-
// true if the user agent string contains a search bot string pattern
586-
return searchBotRE.test(uaOverride || currentUserAgentString());
599+
var searchBotRE = /(CountlySiteBot|nuhk|Googlebot|GoogleSecurityScanner|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver|bingbot|Google Web Preview|Mediapartners-Google|AdsBot-Google|Baiduspider|Ezooms|YahooSeeker|AltaVista|AVSearch|Mercator|Scooter|InfoSeek|Ultraseek|Lycos|Wget|YandexBot|Yandex|YaDirectFetcher|SiteBot|Exabot|AhrefsBot|MJ12bot|TurnitinBot|magpie-crawler|Nutch Crawler|CMS Crawler|rogerbot|Domnutch|ssearch_bot|XoviBot|netseer|digincore|fr-crawler|wesee|AliasIO|contxbot|PingdomBot|BingPreview|HeadlessChrome|Lighthouse)/;
600+
601+
// check override first
602+
if (uaOverride) {
603+
return searchBotRE.test(uaOverride);
604+
}
605+
606+
// check both userAgent and userAgentData, as one of them might be containing the information we are looking for
607+
var ua_bot = searchBotRE.test(currentUserAgentString());
608+
var uaData_bot = searchBotRE.test(currentUserAgentDataString());
609+
return ua_bot || uaData_bot;
587610
}
588611

589612
/**
@@ -5161,6 +5184,7 @@
51615184
processScrollView: processScrollView,
51625185
processScroll: processScroll,
51635186
currentUserAgentString: currentUserAgentString,
5187+
currentUserAgentDataString: currentUserAgentDataString,
51645188
userAgentDeviceDetection: userAgentDeviceDetection,
51655189
userAgentSearchBotDetection: userAgentSearchBotDetection,
51665190
getRequestQueue: getRequestQueue,

0 commit comments

Comments
 (0)