This example shows how to access the 'match metrics' assocaited with
a result from 51Degrees device detection.
Match metrics are various properties that indicate the level of confidence
that the supplied evidence corresponds to the result that has been returned.
This example requires a local data file. The free 'Lite' data file can be acquired by
pulling the git submodules under this repository (run `git submodule update --recursive`)
or from the device-detection-data
GitHub repository.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
Find out about the more capable data files that are available on our
pricing page
require((process.env.directory || __dirname) +
'/../../../deviceDetectionOnPremisePipelineBuilder');
const ExampleUtils = require(__dirname +
'/../exampleUtils').ExampleUtils;
const LITE_V_4_1_HASH = '51Degrees-LiteV4.1.hash';
const args = process.argv.slice(2);
const datafile = args.length > 0 ? args[0] :
ExampleUtils.
findFile(LITE_V_4_1_HASH);
const fs = require('fs');
var pipeline;
const initPipeline = (dataFilePath) => {
if (!fs.existsSync(dataFilePath)) {
console.error('Failed to find a device detection ' +
'data file. Make sure the device-detection-data ' +
'submodule has been updated by running ' +
'`git submodule update --recursive`.');
throw ("No data file at '" + dataFilePath + "'");
}
dataFile: dataFilePath,
performanceProfile: 'MaxPerformance',
autoUpdate: false,
updateMatchedUserAgent: true,
usePredictiveGraph: true,
usePerformanceGraph: false
}).build();
pipeline.on('error', console.error);
};
const displayMatchMetrics = async function (userAgent) {
const flowData = pipeline.createFlowData();
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
const device = flowData.device;
console.log('User-Agent: ' + userAgent);
console.log('Matched User-Agent: ' + device.userAgents.value);
console.log('Matched Nodes: ' + device.matchedNodes.value);
console.log('Id: ' + device.deviceID.value);
console.log('Difference: ' + device.difference.value);
console.log('Drift: ' + device.drift.value);
console.log('Iterations: ' + device.iterations.value);
console.log('Method: ' + device.method.value);
Object.keys(device.flowElement.properties).forEach(function (property) {
if (device[property].hasValue) {
console.log(property + ': ' + device[property].value);
} else {
console.log(property + ': ' + device[property].noValueMessage);
}
});
};
const runExample = async function (dataFilePath) {
const desktopUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36';
initPipeline(dataFilePath);
await displayMatchMetrics(desktopUA);
};
if (process.env.JEST_WORKER_ID === undefined) {
runExample(datafile);
};
module.exports = {
runExample: runExample
};