forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IntentIQ Analytics Adapter: browser blacklist (prebid#12119)
* add browser blacklist to analytics * remove log * fix browser blacklist lowercase * fix browserblocklist lower case issue, add unit tests * remove log * remove unnecessary tests, improve testing logic * remove code duplication * export detectbrowser * description fix * move detect browser util functions to library * fix unit test * update tests * update tests * fix test error
- Loading branch information
1 parent
f0b45e8
commit fe19361
Showing
5 changed files
with
121 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { logError } from '../../src/utils.js'; | ||
|
||
/** | ||
* Detects the browser using either userAgent or userAgentData | ||
* @return {string} The name of the detected browser or 'unknown' if unable to detect | ||
*/ | ||
export function detectBrowser() { | ||
try { | ||
if (navigator.userAgent) { | ||
return detectBrowserFromUserAgent(navigator.userAgent); | ||
} else if (navigator.userAgentData) { | ||
return detectBrowserFromUserAgentData(navigator.userAgentData); | ||
} | ||
} catch (error) { | ||
logError('Error detecting browser:', error); | ||
} | ||
return 'unknown'; | ||
} | ||
|
||
/** | ||
* Detects the browser from the user agent string | ||
* @param {string} userAgent - The user agent string from the browser | ||
* @return {string} The name of the detected browser or 'unknown' if unable to detect | ||
*/ | ||
export function detectBrowserFromUserAgent(userAgent) { | ||
const browserRegexPatterns = { | ||
opera: /Opera|OPR/, | ||
edge: /Edg/, | ||
chrome: /Chrome|CriOS/, | ||
safari: /Safari/, | ||
firefox: /Firefox/, | ||
ie: /MSIE|Trident/, | ||
}; | ||
|
||
// Check for Chrome first to avoid confusion with Safari | ||
if (browserRegexPatterns.chrome.test(userAgent)) { | ||
return 'chrome'; | ||
} | ||
|
||
// Now we can safely check for Safari | ||
if (browserRegexPatterns.safari.test(userAgent) && !browserRegexPatterns.chrome.test(userAgent)) { | ||
return 'safari'; | ||
} | ||
|
||
// Check other browsers | ||
for (const browser in browserRegexPatterns) { | ||
if (browserRegexPatterns[browser].test(userAgent)) { | ||
return browser; | ||
} | ||
} | ||
|
||
return 'unknown'; | ||
} | ||
|
||
/** | ||
* Detects the browser from the NavigatorUAData object | ||
* @param {NavigatorUAData} userAgentData - The user agent data object from the browser | ||
* @return {string} The name of the detected browser or 'unknown' if unable to detect | ||
*/ | ||
export function detectBrowserFromUserAgentData(userAgentData) { | ||
const brandNames = userAgentData.brands.map(brand => brand.brand); | ||
|
||
if (brandNames.includes('Microsoft Edge')) { | ||
return 'edge'; | ||
} else if (brandNames.includes('Opera')) { | ||
return 'opera'; | ||
} else if (brandNames.some(brand => brand === 'Chromium' || brand === 'Google Chrome')) { | ||
return 'chrome'; | ||
} | ||
|
||
return 'unknown'; | ||
} |
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
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