Check out the latest documentation.

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. Configure the 51Degrees data set to give optimal performance when calling FindProfiles.

    										
    string fileName = args[0];
    DataSet dataSet = DataSetBuilder.File()
                    .ConfigureDefaultCaches()
                    .SetCacheSize(CacheType.ValuesCache, 200000)
                    .SetTempFile(false)
                    .Build(fileName)
    
    										
  2. Retrive all the profiles for a specified property value pair.

    										
    profiles = dataSet.FindProfiles("IsMobile", "True");
    
    										
This tutorial assumes you are building this from within the 51Degrees Visual Studio solution. Running the executable produced inside Visual Studio will ensure all the command line arguments are preset correctly. If you are running outside of Visual Studio, make sure to add the path to a 51Degrees data file as an argument.
Full Source File
									
        public static void Run(string fileName)
        {
            // DataSet is the object used to interact with the data file.
            // DataSetBuilder creates Dataset with pool of binary readers to 
            // perform device lookup using file on disk. The type is 
            // disposable and is therefore contained in using block to 
            // ensure file handles and resources are freed.
            using (DataSet dataSet = DataSetBuilder.File()
                .ConfigureDefaultCaches()
                // Set the cache size for the Values cache to 200,000
                // This is done because FindProfiles performs significantly
                // faster when all Value objects can be held in memory.
                // The number of Value objects varies by data file type:
                // Lite < 5000
                // Premium < 180,000
                // Enterprise < 200,000
                .SetCacheSize(CacheType.ValuesCache, 200000)
                .SetTempFile(false)
                .Build(fileName))
            {
                Console.WriteLine("Staring Find Profiles Example.");

                // Retrieve all mobile profiles from the data set.
                Profile[] profiles = dataSet.FindProfiles("IsMobile", "True");
                Console.WriteLine("There are " + profiles.Count()
                    + " mobile profiles in the " + dataSet.Name
                    + " data set.");

                // Retrieve all non-mobile profiles from the data set.
                profiles = dataSet.FindProfiles("IsMobile", "False");
                Console.WriteLine("There are " + profiles.Count()
                    + " non-mobile profiles in the " + dataSet.Name
                    + " data set.");
            }
        }


									
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 p roviding 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.