-
Notifications
You must be signed in to change notification settings - Fork 7
Basic usage
You can use this analyzer directly in your .NET based applications installing this .nuget package.
and in your application you can use it as simple as this:
UserAgentAnalyzer uaa = UserAgentAnalyzer
.NewBuilder()
.HideMatcherLoadStats()`
.WithCache(10000)`
.Build();
UserAgent agent = uaa.Parse("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11");
foreach (String fieldName in agent.GetAvailableFieldNamesSorted())
{
System.WriteLine($"{fieldName} = + {agent.GetValue(fieldName)}");`
}
Please instantiate a new UserAgentAnalyzer as few times as possible because the initialization step for a full UserAgentAnalyzer (i.e. all fields) usually takes something in the range of 2-5 seconds. If you need multiple instances of the UserAgentAnalyzer then you MUST create a new Builder instance for each of those.
Note that not all fields are available after every parse. So be prepared to receive a 'null' if you extract a specific name.
I recommend you leave the cache to a size that is roughly the unique number of useragents your site finds in a limited timespan. Something like 15/30/60 minutes usually gives you a fine cache size. On a very busy website I see ~50K-60K distinct useragents per day and ~10K per hour. So in my opinion a cache size of 5K-10K elements is a good choice.
In some scenarios you only want a specific field and all others are unwanted. This can be achieved by creating the analyzer in C# like this:
UserAgentAnalyzer uaa;
uaa = UserAgentAnalyzer
.NewBuilder()
.WithField("DeviceClass")
.WithField("AgentNameVersionMajor")
.Build();
One important effect is that this speeds up the system because it will kick any rules that do not help in getting the desired fields. The above example showed an approximate 40% speed increase (i.e. times dropped from ~1ms to ~0.6ms).
In the OrbintSoft.Yauaa.Analyzer.UserAgent many (not all!!) of the provided variables are provided as a constant String. You can choose to use these and avoid subtle typos in the requested attribute names.