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.
This example requires a local data file. Free data files can be acquired by
pulling the submodules under this repository or from the
device-detection-data
GitHub repository.
Is user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4. (KHTML, like Gecko) Mobile/15C114 a mobile? true
Is user agent Mozilla/5.0 (iPhone; CPU iPhone OS 99_2 like Mac OS X) AppleWebKit/604.4.(KHTML, like Gecko) Mobile/15C114 a mobile? true
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.
require((process.env.directory || __dirname) +
'/../../deviceDetectionOnPremisePipelineBuilder');
const datafile = (process.env.directory || __dirname) + '/../../device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash';
const fs = require('fs');
if (!fs.existsSync(datafile)) {
console.error("The datafile required by this example is not present. Please ensure that the 'device-detection-data' submodule has been fetched.");
throw ("No data file at '" + datafile + "'");
}
performanceProfile: 'MaxPerformance',
dataFile: datafile,
autoUpdate: false
}).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 modifiediPhoneUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 99_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114';
checkIfMobile(modifiediPhoneUA);
const corruptedUA = 'This is not a User-Agent';
checkIfMobile(corruptedUA);