My original plan when building up the OpenNETCF Google Analytics library (which provides GA support to Xamarin Forms apps) was to make it 100% PCL. A fine goal, but it turns out that it was unrealistic for a single reason: the User Agent string. My original implementation had a “user agent generator” that successfully would tell GA whether you were using an Android or iOS device, but getting any finer grained was really not a simple task. I considered – very, very briefly – keeping a long list of user agent strings and trying to figure out which one to use based on the platform, but that sounded like an unmaintainable nightmare. Instead, I had to create a simple native piece for each platform for something that seems pretty basic.
In iOS, this was all that was needed:
using (var webView = new UIWebView(CGRect.Empty)) { m_userAgent = webView.EvaluateJavascript("navigator.userAgent"); }
In Android it was even less:
m_userAgent = Java.Lang.JavaSystem.GetProperty("http.agent")
But it had to be done native.
I’ve updated the library in NuGet, and now GA will show you exactly what type of device was connected and tracking, which I feel was more than worth the added complexity.