import { UAParser } from 'ua-parser-js' ; import Logger from '../logger/Logger' ; import Versioning from '../versioning/Versioning' ; import UserAgentParser from './UserAgentParser' ; /**
 * [[DefaultUserAgentParser]] uses UAParser to parse the browser's user agent.
 * It is responsible to hold and provide browser, OS and device specific information.
 */ export default class DefaultUserAgentParser implements UserAgentParser { private static readonly UNAVAILABLE = 'Unavailable' ; private parserResult : UAParser . IResult ; private browserName : string ; private browserVersion : string ; private deviceName : string ; private browserMajorVersion : string ; constructor ( logger ) { try { this . parserResult = navigator && navigator . userAgent ? new UAParser ( navigator . userAgent ) . getResult ( ) : undefined ; } catch ( error ) { logger . error ( error . message ) ; } this . browserMajorVersion = this . parserResult ?. browser ?. version ?. split ( '.' ) [ 0 ] || DefaultUserAgentParser . UNAVAILABLE ; this . browserName = this . parserResult ?. browser . name || DefaultUserAgentParser . UNAVAILABLE ; this . browserVersion = this . parserResult ?. browser . version || DefaultUserAgentParser . UNAVAILABLE ; this . deviceName = [ this . parserResult ?. device . vendor || '' , this . parserResult ?. device . model || '' ] . join ( ' ' ) . trim ( ) || DefaultUserAgentParser . UNAVAILABLE ; } getParserResult ( ) { return { browserMajorVersion : this . browserMajorVersion , browserName : this . browserName , browserVersion : this . browserVersion , deviceName : this . deviceName , osName : this . parserResult ?. os . name || DefaultUserAgentParser . UNAVAILABLE , osVersion : this . parserResult ?. os . version || DefaultUserAgentParser . UNAVAILABLE , sdkVersion : Versioning . sdkVersion , sdkName : Versioning . sdkName , } ; } }