• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

Perl API

What's Covered

This tutorial illustrates how to use the findProfiles function to return a list of matching profiles for a given property value pair. The following steps are covered:

  • How to find all the mobile device profiles.
  • Find all the mobile profiles with a screen size of 1080 pixels.
  • Do the same for non-mobile devices, and display how many have that screen size.

Code and Explanation

Find profiles example of using 51Degrees device detection. The example shows how to:
  1. Set the various settings for 51Degrees detector

    my $filename = "51Degrees-LiteV3.2.dat";
    my $propertyList = "IsMobile";
    my $cacheSize = 10000;
    my $poolSize = 20;
    

  2. Instantiate the 51Degrees device detection provider with these properties

    my $provider = new FiftyOneDegrees::PatternV3::Provider(
    	$dataFile,
    	$properties,
    	$cacheSize,
    	$poolSize);
    

  3. Retieve all the profiles fromt the data set which match the provided property value pair

    my $profiles = $provider->findProfiles("IsMobile", "True");
    

  4. Search within a list of profiles for another property value pair.

    my $profiles = $provider->findProfiles("ScreenPixelsWidth", "1080", $profiles);
    

This example assumes you are running from the original subdirectory i.e. Device-Detection/perl/examples/ and the 51Degrees Perl module is installed.
Full Source File
use FiftyOneDegrees::PatternV3;
use feature qw/say/;

my $filename = "../../data/51Degrees-LiteV3.2.dat";
my $propertyList = "IsMobile";
my $cacheSize = 10000;
my $poolSize = 20;

# Initialises the device detection provider with the settings declared above.
# This uses Lite data file. For more info
# see:
# <a href="https://51degrees.com/compare-data-options">compare data options
# </a>

my $provider = new FiftyOneDegrees::PatternV3::Provider(
    $filename,
    $propertyList,
    $cacheSize,
    $poolSize);

my $dataSetName = $provider->getDataSetName();

say "Starting Find Profiles Example.\n";

# Retrieve all the mobile profiles from the data set.
my $profiles = $provider->findProfiles("IsMobile", "True");
my $profilesCount = $profiles->getCount();
say "There are $profilesCount mobile profiles in the $dataSetName data set.";
# Find how many have a screen width of 1080 pixels.
my $profiles = $provider->findProfiles("ScreenPixelsWidth", "1080", $profiles);
my $profilesCount = $profiles->getCount();
say "$profilesCount of them have a screen width of 1080 pixels.";

# Retrieve all the non-mobile profiles from the data set.
my $profiles = $provider->findProfiles("IsMobile", "False");
my $profilesCount = $profiles->getCount();
say "There are $profilesCount non-mobile profiles in the $dataSetName data set.";
# Find how many have a screen width of 1080 pixels.
my $profiles = $provider->findProfiles("ScreenPixelsWidth", "1080", $profiles);
my $profilesCount = $profiles->getCount();
say "$profilesCount of them have a screen width of 1080 pixels.";

Full Source File

Summary

This tutorial covered how to work with the 51Degrees device Data Model to obtain a subset of device profiles that meet several conditions. Each condition in this case is a specific value for a chosen property. The result should be read as follows: this is a subset of device profiles where property1 has value1 and property2 has value2 and property3 has value3 and so on.

One real world application for this is building a set of interlinked menus where each choice will narrow down the available options for subsequent choices. This can be useful when part of the API is exposed to the end user:

  • An ad agency could benefit from allowing their clients to target specific devices based on pre-defined criteria, such as DeviceType, ScreenInchesWidth or even PriceBand.
  • A program that uses a 51Degrees API to generate/augment reports could be enhanced to allow the user to choose the report parameters. By providing a finite set of choices and avoiding arbitrary input the chance of errors occurring is reduced and user experience improved.

The Lite data file contains considerably fewer properties and values than the Premium or Enterprise files, making the usefulness of this tutorial slightly harder to appreciate. With Premium and Enterprise data files there are many more possibilities for creating subsets of device profiles. For example: using Premium device data you can generate a subset of all 'Samsung' 'Smartphone' devices. Or get all the properties of a specific HTC model.