This example shows how to retrieve property meta data from 51Degrees
on-premise device detection.
This feature can be used to get information such as the category that
a property belongs to or the possible values a property can have.
(As of the current data file. If the data file is updated,
the possible values for a property can change)
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.
Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support svg? : true
Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support video? : true
Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support supportswebgl? : true
Does user agent Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114 support webp? : False
const FiftyOneDegreesDeviceDetection = require((process.env.directory || __dirname) + '/../../');
const fs = require('fs');
const datafile = (process.env.directory || __dirname) + '/../../device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash';
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 + "'");
}
const pipeline = new FiftyOneDegreesDeviceDetection.DeviceDetectionPipelineBuilder({
performanceProfile: 'MaxPerformance',
dataFile: datafile,
autoUpdate: false
}).build();
pipeline.on('error', console.error);
const properties = pipeline.getElement('device').getProperties();
for (let property in properties) {
property = properties[property];
console.log(`Property ${property.name}, ${property.description}, of type ${property.type}`);
}
const getAllSupportedMedia = async function (userAgent) {
const flowData = pipeline.createFlowData();
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
const supported = flowData.getWhere('category', 'Supported Media');
Object.entries(supported).forEach(([key, result]) => {
console.log(`Does user agent ${userAgent} support ${key}? : `);
if (result.hasValue) {
console.log(result.value);
} else {
console.log(result.noValueMessage);
}
});
};
const iPhoneUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114';
getAllSupportedMedia(iPhoneUA);
const getMatchMetaData = async function (userAgent) {
const flowData = pipeline.createFlowData();
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
if (flowData.device.deviceid && flowData.device.deviceid.hasValue) {
console.log('Device ID: ' + flowData.device.deviceID.value);
}
if (flowData.device.useragents && flowData.device.useragents.hasValue) {
console.log('Matched useragents' + flowData.device.useragents.value);
}
if (flowData.device.difference && flowData.device.difference.hasValue) {
console.log('Difference' + flowData.device.difference.value);
}
if (flowData.device.method && flowData.device.method.hasValue) {
console.log('Method ' + flowData.device.method.value);
}
if (flowData.device.matchednodes && flowData.device.matchednodes.hasValue) {
console.log('Matched nodes' + flowData.device.MatchedNodes.value);
}
};
getMatchMetaData(iPhoneUA);