This example shows how the 51Degrees device detection engine deals with
evidence that does not match any known device.
In this scenario, 'HasValue' can be used to check if the property has been
populated by the engine. If it hasn't then 'NoValueMessage' can be used
to get the reason why.
Is user agent 'This is not a User-Agent' a mobile?
The results contained a null profile for the component which the required property belongs to.
Is user agent 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114' a mobile?
true
const FiftyOneDegreesDeviceDetection = require((process.env.directory || __dirname) + '/../../');
const myResourceKey = process.env.RESOURCE_KEY || "!!YOUR_RESOURCE_KEY!!";
if (myResourceKey == "!!YOUR_RESOURCE_KEY!!") {
console.log('You need to create a resource key at ' +
'https://configure.51degrees.com and paste it into the code, ' +
'replacing !!YOUR_RESOURCE_KEY!!');
console.log('Make sure to include the ismobile property ' +
'as it is used by this example.');
} else {
const pipeline = new FiftyOneDegreesDeviceDetection.DeviceDetectionPipelineBuilder({
resourceKey: myResourceKey
}).build();
pipeline.on('error', console.error);
const checkIfMobile = async function (userAgent) {
const flowData = pipeline.createFlowData();
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
const ismobile = flowData.device.ismobile;
console.log(`Is user agent '${userAgent}' a mobile?`);
if (ismobile.hasValue) {
console.log(ismobile.value);
} else {
console.log(ismobile.noValueMessage);
}
console.log(' ');
};
const iPhoneUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114';
checkIfMobile(iPhoneUA);
const corruptedUA = 'This is not a User-Agent';
checkIfMobile(corruptedUA);
}