Page MenuHomePhabricator

Create a device detection strategy for the device_type context field
Closed, ResolvedPublic


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?

Event Timeline

Mholloway created this task.

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.

Mholloway moved this task from Inbox to Doing on the Product-Data-Infrastructure board.
Mholloway moved this task from Inbox to Doing on the Better Use Of Data board.

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, / 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.)

There is a JavaScript port of device-detector, but the README cautions that it is not sufficiently performant for use in the browser. bowser looks like a better choice.

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.

Actually, probably better than using different libraries for MediaWiki PHP and JS would be to do device detection in PHP and provide the relevant result data to JavaScript in the virtual data.json file for ext.eventLogging. / 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 vs. for PHP?

Strategy: We'll use existing libraries.