Uses Google Github

mbaersch/bot-detection-variable-gtm.js

function () {//----------------------------------------------------------------// Detection of crawlers based on user agent or feature detection //----------------------------------------------------------------// Should a test passed once within the session be sufficient? Then switch on here varcache2Session = true;// --- End of setup --- // Reuse the last result for multiple calls on a pageif ((typeof (window.gtmCachedBotScore)! == 'undefined')) returnwindow.gtmCachedBotScore;// Check and return the existing value if session caching is activecache2Session = cache2Session && (window.sessionStorage! = undefined);if (cache2Session == true) {varsessionCachedBotScore = sessionStorage.getItem ("sessionCachedBotScore");if (sessionCachedBotScore == "OK") return "OK";}//-----------------------------------------------------// Check for user agents of known rendering crawlers //-----------------------------------------------------varagentString = window.navigator.userAgent;varbots_ua = newArray ("Googlebot | GoogleBot","Google Search Console","Chrome-Lighthouse","DuckDuckBot","JobboerseBot","woobot","PingdomPageSpeed","PagePeeker","Refindbot","HubSpot","Investment Crawler","BingPreview","Baiduspider","Yandex","Sogou","SISTRIX","facebookexternalhit","Site-Shot","wkhtmltoimage","SMTBot","PetalBot","AhrefsBot","avalex","RyteBot","Cookiebot","Seekport Crawler");varrs = "";for (i = 0; i varbt = bots_ua [i];if (RegExp (bt) .test (agentString)) {rs = bt; break;}}if (rs) {rs = rs.split ("|") [0] .toUpperCase () + "_ UA";} else {// Search for crawlers, spiders, bot in the agent stringif (/crawler/.test (agentString.toLowerCase ())) rs = "POTENTIAL_CRAWLER_UA";elseif (/spider/.test (agentString.toLowerCase ())) rs = "POTENTIAL_SPIDER_UA";elseif (/bot/.test (agentString.toLowerCase ()) &&! /cubot/.test (agentString.toLowerCase ())) rs = "POTENTIAL_BOT_UA";}//------------------------------------------------------------// Detect headless browsers based on user agent or characteristics//------------------------------------------------------------// Sources used: // - https://github.com/antoinevastel/fpscanner/blob/master/src/fpScanner.js// - https://github.com/infosimples/detect-headless// For effectiveness see: https://de.slideshare.net/SergeyShekyan/shekyan-zhang-owaspfunctioniFrameChrome () {// The test can only be carried out if a body already exists, otherwise it can be passed over with a return valueif (! document.body) return'too_early ';variframe = document.createElement ('iframe');iframe.srcdoc = 'blank page';document.body.appendChild (iframe);varresult = typeofiframe.contentWindow.chrome;iframe.remove ();returnresult;}if (rs == "") {varcanvas = document.createElement ('canvas');vargl = canvas.getContext ('webgl');varvendor = '';varrenderer = '';try {varcanvas = document.createElement ('canvas');vargl = canvas.getContext ('webgl');vardebugInfo = gl.getExtension ('WEBGL_debug_renderer_info');vendor = gl.getParameter (debugInfo.UNMASKED_VENDOR_WEBGL);renderer = gl.getParameter (debugInfo.UNMASKED_RENDERER_WEBGL);} catch (e) {}variScreenAvailWidth = window.screen.availWidth;variScreenAvailHeight = window.screen.availHeight;variScreenWidth = window.screen.width;variScreenHeight = window.screen.height;// Landscape test if ((iScreenAvailWidth> iScreenAvailHeight && iScreenWidth iScreenHeight)) {variScreenAvailWidth = window.screen.availHeight;variScreenAvailHeight = window.screen.availWidth;}if (/HeadlessChrome/.test (agentString)) rs = "HEADCHR_UA";elseif (/ MSIE 5.0 / .test (agentString)) rs = "IE5_UA";elseif (/PhantomJS/.test (agentString)) rs = "PHANTOM_UA";elseif ((/ Chrome / .test (agentString)) && (navigator.webdriver)) rs = "WEBDRIVER";// The following "iframe" test generates "In App Browser hits", which apparently come from FB and Instagram etc. // User or Preview? Not clear. See https://firmhouse.com/blog/filtering-facebook-search-spiders-bots-and-other-automated-requests-fb_iab/// It sounds like a bot here, but I don't believe in the derivation and tests have included real visits as false positives, // hence the additional check for FB_IAB in the user agentelseif ((/ Chrome / .test (agentString)) && (! / FB_IAB / .test (agentString)) && (iFrameChrome () === 'undefined')) rs = "HEADCHR_IFRAME";elseif (! / Trident | MSIE | Edge / .test (agentString) && (navigator.languages ​​=== "")) rs = "PHANTOM_LANGUAGE";elseif (window.external && window.external.toString && window.external.toString (). indexOf ('Sequentum')> - 1) rs = "SEQUENTUM";elseif (iScreenAvailWidth> iScreenWidth && iScreenAvailHeight> iScreenHeight) rs = "PHANTOM_WINDOW_HEIGHT";// Missing hits cannot be ruled out here, as this result can alternate with "OK" within a session.// For this reason, caching on a session basis is provided above, as soon as all tests have been passed once with "OK" elseif ((window.outerHeight === 0) && (window.outerWidth === 0)) rs = "NO_OUTER_DIMENSION";elseif ((window.screen.height <= 100) && (window.screen.width <= 100)) rs = "DEVICE_TOO_SMALL";elseif (('_ Selenium_IDE_Recorder'inwindow) || (' callSelenium'inwindow) || ('_ selenium'inwindow) ||('__webdriver_script_fn'indocument) || (' __ driver_evaluate'indocument) ||('__webdriver_evaluate'indocument) || (' __ selenium_evaluate'indocument) || ('__ fxdriver_evaluate'indocument) ||('__driver_unwrapped'indocument) || (' __ webdriver_unwrapped'indocument) ||('__selenium_unwrapped'indocument) || (' __ fxdriver_unwrapped'indocument) ||('__webdriver_script_func'indocument) || (document.documentElement.getAttribute ("selenium")! == null) ||(document.documentElement.getAttribute ("webdriver")! == null) ||(document.documentElement.getAttribute ("driver")! == null)) rs = "SELENIUM_DRIVER";elseif (('callPhantom'inwindow) || (' _ phantom'inwindow) || ('phantom'inwindow)) rs = "PHANTOM_PROPERTIES";elseif ('__ stopAllTimers'inwindow) rs = "JSDOM_PROPERTIES"