\r\n

51Degrees Device Detection Node.js  4.3Newer Version 4.4

51Degrees Device Detection for Node.js

onpremise/metadata-console/metaData.js

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 is available in full on GitHub.

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

Expected output:

[list of properties with descriptions, categories and types]

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

[List of metrics about the match]

/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
require((process.env.directory || __dirname) +
'/../../../deviceDetectionOnPremisePipelineBuilder');
const fs = require('fs');
const ExampleUtils = require(__dirname + '/../exampleUtils').ExampleUtils;
// In this example, by default, the 51degrees "Lite" file needs to be in the
// fiftyone.devicedetection.onpremise/device-detection-cxx/device-detection-data,
// or you may specify another file as a command line parameter.
//
// Note that the Lite data file is only used for illustration, and has
// limited accuracy and capabilities.
// Find out about the Enterprise data file on our pricing page:
// https://51degrees.com/pricing
const LITE_V_4_1_HASH = '51Degrees-LiteV4.1.hash';
// Load in a datafile
const sliceLen = process.env.JEST_WORKER_ID === undefined ? 2 : process.argv.length;
const args = process.argv.slice(sliceLen);
// Use the supplied path for the data file or find the lite
// file that is included in the repository.
const datafile = args.length > 0 ? args[0] : ExampleUtils.findFile(LITE_V_4_1_HASH);
if (!fs.existsSync(datafile)) {
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 '" + datafile + "'");
}
// Create a new Device Detection pipeline and set the config.
performanceProfile: 'MaxPerformance',
dataFile: datafile,
autoUpdate: false
}).build();
// Logging of errors and other messages.
// Valid logs types are info, debug, warn, error
pipeline.on('error', console.error);
// Get list of properties for the deviceDetectionEngine
// (including metadata like category, description, type and
// which datafiles they appear in)
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}`);
}
// The following function uses "flowData.getWhere()"
// to fetch all of the data related to "supported media"
// on a device by querying the category.
const getAllSupportedMedia = async function (userAgent) {
// Create a flow data element and process the desktop User-Agent.
const flowData = pipeline.createFlowData();
// Add the User-Agent as evidence
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
// Get all supported media types (html5 video, svg...)
// and loop over them to get the support results
// The second parameter can also be a boolean function
// that checks the value of that meta type (category in this case)
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 {
// Echo out why the value isn't meaningful
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);
// The device detection engine comes with additional metadata about each match
const getMatchMetaData = async function (userAgent) {
// Create a flow data element and process the desktop User-Agent.
const flowData = pipeline.createFlowData();
// Add the User-Agent as evidence
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
// This is all stored under the "device metrics" category
if (flowData.device.deviceID && flowData.device.deviceID.hasValue) {
console.log('Device ID: ' + flowData.device.deviceID.value);
// Consists of four components separated by a hyphen symbol:
// Hardware-Platform-Browser-IsCrawler where each Component
// represents an ID of the corresponding Profile.
}
if (flowData.device.userAgents && flowData.device.userAgents.hasValue) {
console.log('Matched useragents' + flowData.device.userAgents.value);
// The matched useragents
}
if (flowData.device.difference && flowData.device.difference.hasValue) {
console.log('Difference' + flowData.device.difference.value);
// Used when detection method is not Exact or None.
// This is an integer value and the larger the value
// the less confident the detector is in this result.
}
if (flowData.device.method && flowData.device.method.hasValue) {
console.log('Method ' + flowData.device.method.value);
// Provides information about the algorithm that was used to
// perform detection for a particular User-Agent.
}
if (flowData.device.matchedNodes && flowData.device.matchedNodes.hasValue) {
console.log('Matched nodes' + flowData.device.matchedNodes.value);
// The number of hash nodes that have been matched before finding a result.
}
};
getMatchMetaData(iPhoneUA);