We would like to add a device_type context field indicating whether the device related to the event being logged is a phone, tablet, or desktop computer. For web requests, the typical way of doing this seems to be to apply a complex regular expression to the user-agent header. Is there a cleaner way? What affordances do Android and iOS provide for gathering the device type?
|Open||jlinehan||T276378 Release Metrics Platform v1|
|Open||DAbad||T281999 Metrics Platform Schema: Define & Model Event Level Fields|
|Duplicate||jlinehan||T277092 [Metrics Platform] Standard base schema|
|Resolved||jlinehan||T276379 [Metrics Platform] Create Metrics Platform Schema|
|Resolved||DAbad||T275420 [Metrics Platform] Define first set of standard fields for metrics platform|
|Duplicate||kzimmerman||T278595 [Metrics Platform] Evaluate preliminary list of standard fields for feasibility|
|Resolved||• Mholloway||T280195 Create a device detection strategy for the device_type context field|
|Open||jlinehan||T277090 [Metrics Platform] Standard base schema should include a 'test' or 'debug' field for QA and other purposes|
I did some looking and found that there is existing device detection code in MobileFrontend, and there is demand for moving it into MW core: T263501: Move DeviceDetection.php from MobileFrontend to core. So, I wrote patches to core and MF doing just that.
Update: Based on some comments from Krinkle I decided to abandon the patch moving the old device detection code from MobileFrontend into MediaWiki core. Actually, at Wikimedia this code has only ever run on Wikitech. All other wikis rely on Varnish to redirect mobile users to the mobile site.
Krinkle suggests exploring the use of libraries in the ua-parser ecosystem. I see from codesearch that the ua-parser Java library is being used by Refinery. I can't see them being used currently in any other components running in Wikimedia production (though ua-parser-js is used incidentally by the VisualEditor extension as a dev dependency of the Karma testing library). Also the ua-parser libraries don't currently appear to provide straightforward isMobile-type methods as part of their API. That falls to other libraries, like device-detector (PHP) or bowser (JS). There is also the User Agent Client Hints API (see blog post), which is probably preferable to UA parsing but only supported in Chrome(-based browsers).
Our first decision point, I think, is whether we want to do UA-based device detection in the application layer at all, which would probably mean shepherding one or more UA parsing libraries through security review. I don't know whether or not this would mean we should propose them for use in the MediaWiki ecosystem generally. We'd probably be getting into RFC territory or equivalent at that point. Alternatively, we could try and deduce whether Varnish thought the UA was mobile from the properties of the HTTP request (e.g., Varnish probably thought it was mobile if it's an m-dot domain and there's no other header or query parameter explicitly requesting the mobile view); or we could ask for Varnish to add a header like X-UA-Is-Mobile to reflect this explicitly.
For UA-based device detection in PHP, https://github.com/matomo-org/device-detector / https://packagist.org/packages/matomo/device-detector appears to be the library we would want. (isMobile-type functionality was dropped from the ua-parser PHP library and moved to a standalone ua-classifier project, which is defunct and succeeded by device-detector.)
Doing device detection on our own using these libraries would have the advantage of being decoupled from the device detection happening in Wikimedia's Varnish caching layer and therefore being useful to third parties; the UA parsing logic in these libraries is also almost certainly more up-to-date.
https://github.com/serbanghita/Mobile-Detect / https://packagist.org/packages/mobiledetect/mobiledetectlib is another, apparently more popular library option for detecting mobile devices in PHP.
There is even an existing MobileDetect extension that wraps it, but it's not deployed at Wikimedia.
EDIT: The MobileDetect extension does NOT in fact use the mobiledetect library, but it looks like it may start doing so.
@jlinehan @mpopov OK, I think the outcome for this task is that our strategy should be to create utility method(s) for this in the PHP and JS Metrics Platform client libraries (or wherever we ultimately end up doing context-based event enrichment; see my recent comment in Slack) that lean on third-party libraries. Once we've selected our library or libraries, we'll need to get them teed up for security review. Thoughts on https://packagist.org/packages/mobiledetect/mobiledetectlib vs. https://packagist.org/packages/matomo/device-detector for PHP?