1

I've created a relatively simply library for pushing events to Google Analytics from a Xamarin Forms app using their Measurement Protocol API. The library works and events get logged to the GA account just fine.

One area that I need to improve is the "device type" reporting. GA uses the User Agent from the API requests to determine loads of stuff about the device - OS, platform, resolution, manufacturer, etc. If I was using a browser to make the calls, then obviously I'd get all of that, but since I'm using a basic HttpClient there's no default user agent string.

I need to either store a large dictionary of possible strings (not a great idea), build up a string (an OK idea, except you have to do a load of introspection to get detailed), or have some way to ask the device itself what it uses for a user agent string.

If I have to build my own, what I'd like to do to start with is to be able to identify the OS (Android, iOS, Windows) that I'm on as well as the device type (tablet, phone). Sure, device manufacturer, model, etc would all be great, but building that lookup myself would get insane quickly.

I can find no clear documentation on how GA parses the user agent string, so I'm not completely clear on what the general form would look like to identify myself.

I've already found the long lists of user agent string online. What I'm hoping for is some sort of algorithm or documentation that might assist in generating at least a rudimentary user agent string that would allow a library consumer to report some basic device info that would in turn show up in their GA dashboards

EDIT

I suppose I should mention that ideally I'd have a PCL way of doing this. Right now the library is fully PCL and if I can avoid generating platform-specific implementations that would be great. Not a hard requirement, but a real "nice to have".

ctacke
  • 66,480
  • 18
  • 94
  • 155

1 Answers1

1

iOS UIWebView (non-visual to end-user):

var webView = new UIWebView(CGRect.Empty);
var userAgentString = webView.EvaluateJavascript("navigator.userAgent");
webView.Dispose();
Console.WriteLine(userAgentString);

Note: Do this once and store the results for future use as creating a UIWebView and running JavaScript is not cheap (perf wise)

Sample Output:

Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E230

Android Java System:

var userAgentString = Java.Lang.JavaSystem.GetProperty("http.agent");
Log.Debug("UA", userAgentString);

Sample Output:

Dalvik/2.1.0 (Linux; U; Android 5.1.1; Z716BL Build/LMY47V)

Note: If you are interested in creating your own user-agent, you should look at this answer as he pulled the code from the ASOP source.

I would look at the Wikipedia link for formatting and links to RFC 1945

Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • This looks good. It's not exactly what I'd like to do in the ideal case where I'd maintain PCL compliance, but this is likely the direction I'll head if I get no other suggestions. – ctacke Jul 21 '16 at 16:01
  • @ctacke A `user-agent` by design is platform-dependent.... You could use a `Xamarin.Forms` dependency service but that would still be calling into platform code. Otherwise you would need to just make up some string that is not dependent on any platform related results and post that to GA but I'm not sure what that would buy you in terms of segmentation analytics as GA breaks the user-agent down to give you device (browser), version, etc.. as part of your end-user demo. The only other thing we do it tag our app version into the string.... – SushiHangover Jul 21 '16 at 16:16
  • Yes, I understand they are, but I can do simple things in PCL Xamarin Forms like detect the OS and Idiom, so without a platform-specific library I could reasonably generate something that says "I'm on an Android Tablet" or "I'm on an iPhone" – ctacke Jul 21 '16 at 16:21