This example demonstrates using both device detection and IP intelligence from the 51Degrees cloud service.
This example demonstrates using both device detection and IP intelligence from the 51Degrees cloud service.You will learn:
To run this example, you will eventually need to create a resource key. A resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties, but it is not yet available for IP intelligence. For now, you can run the onpremise/mixed/gettingstarted-api example to simulate a custom hosted cloud service, and point this example to it by setting the '_51DEGREES_CLOUD_ENDPOINT' environment variable to 'http://localhost:5225/'. Any value can then be used for the resource key.
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../../node_modules/', requestedPackage));
} catch (e) {
try {
return require(requestedPackage);
} catch (e2) {
return require(requestedPackage);
}
}
};
const fs = require('fs');
const PipelineBuilder = require51(
'fiftyone.pipeline.core').PipelineBuilder;
const ExampleUtils = require(path.join(__dirname,
'/../../exampleUtils'));
const OptionsExtension = require51(
'fiftyone.ipintelligence.shared').optionsExtension;
const DataExtension = require51(
'fiftyone.ipintelligence.shared').dataExtension;
const combinedEvidence = [
new Map([
['header.user-agent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1'],
['query.client-ip', '62.61.32.31']
]),
new Map([
['header.user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'],
['query.client-ip', '45.236.48.61']
]),
new Map([
['header.user-agent', 'Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/91.0.4472.80 Mobile/15E148 Safari/604.1'],
['query.client-ip', '2001:0db8:085a:0000:0000:8a2e:0370:7334']
]),
new Map([
['header.user-agent', 'Mozilla/5.0 (Linux; Android 11; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36'],
['query.client-ip', '8.8.8.8']
])
];
const outputValue = function (name, elementData, propertyName) {
return `\n\t${name}: ` +
};
const analyse = async function (evidence, pipeline, output) {
const data = pipeline.createFlowData();
let message = '='.repeat(79);
message += '\nInput values:';
for (const [key, value] of evidence) {
message += `\n\t${key}: ${value}`;
}
output.write(message + '\n');
evidence.forEach((value, key, map) => {
data.evidence.add(key, value);
});
await data.process();
message = '\nDevice Detection Results:';
message += '\n' + '-'.repeat(25);
const device = data.device;
message += outputValue('Mobile Device', device, 'ismobile');
message += outputValue('Platform Name', device, 'platformname');
message += outputValue('Platform Version', device, 'platformversion');
message += outputValue('Browser Name', device, 'browsername');
message += outputValue('Browser Version', device, 'browserversion');
message += outputValue('Hardware Name', device, 'hardwarename');
message += outputValue('Hardware Vendor', device, 'hardwarevendor');
message += outputValue('Device Type', device, 'devicetype');
message += outputValue('Screen Width', device, 'screenpixelswidth');
message += outputValue('Screen Height', device, 'screenpixelsheight');
output.write(message + '\n');
message = '\nIP Intelligence Results:';
message += '\n' + '-'.repeat(24);
const ip = data.ip;
message += outputValue('Country', ip, 'country');
message += outputValue('Country Code', ip, 'countrycode');
message += outputValue('Region', ip, 'region');
message += outputValue('State', ip, 'state');
message += outputValue('Town', ip, 'town');
message += outputValue('Latitude', ip, 'latitude');
message += outputValue('Longitude', ip, 'longitude');
message += outputValue('Registered Name', ip, 'registeredname');
message += outputValue('Registered Owner', ip, 'registeredowner');
message += outputValue('Registered Country', ip, 'registeredcountry');
message += outputValue('IP Range Start', ip, 'iprangestart');
message += outputValue('IP Range End', ip, 'iprangeend');
message += outputValue('Accuracy Radius', ip, 'accuracyradiusmin');
message += outputValue('Time Zone Offset', ip, 'timezoneoffset');
output.write(message + '\n\n');
};
const run = async function (options, output) {
if (!resourceKey) {
console.log(
'No resource key specified in the configuration file ' +
'\'51d.json\' or the environment variable ' +
`'${ExampleUtils.RESOURCE_KEY_VARIABLE}'. The 51Degrees cloud ` +
'service is accessed using a \'ResourceKey\'. For more information ' +
'see ' +
'/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required. ' +
'A resource key with everything this example displays can be ' +
'created at https://configure.51degrees.com/hYzn3TV3?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required with a paid ' +
'subscription. A free key created at ' +
'https://configure.51degrees.com/Wkqxf3Bs?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required populates only the ' +
'free properties, which for IP intelligence are Country, ' +
'LocationConfidence, Ip and IpV6. See ' +
'/pricing?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required to get a paid subscription with ' +
'more properties. Once complete, populate the config file or ' +
'environment variable mentioned at the start of this message ' +
'with the key.'
);
return;
}
pipeline.on('error', console.error);
for (const values of combinedEvidence) {
await analyse(values, pipeline, output);
}
};
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
const resourceKey = args.length > 0 ? args[0] :
ExampleUtils.getResourceKeyFromEnv();
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')), 'utf8');
if (!resourceKeyFromConfig || resourceKeyFromConfig.startsWith('!!')) {
}
run(options, process.stdout);
}
module.exports = {
run
};
Definition dataExtension.js:23
static getValueHelper(elementData, propertyName)
Helper to get a human readable value of an aspect property value.
Definition dataExtension.js:55
Definition fiftyone.ipintelligence.onpremise/examples/onpremise/exampleUtils.js:203
Definition optionsExtension.js:27
static setResourceKey(options, resourceKey)
Set the resource key in the pipeline options.
Definition optionsExtension.js:123
static getResourceKey(options)
Get the resource key from the pipeline options.
Definition optionsExtension.js:107
buildFromConfiguration(config)