Skip to content

Commit

Permalink
feat: improve user agent detection (#1038)
Browse files Browse the repository at this point in the history
A user reports that they had significantly more matches on "desktop" as a type than "mobile" as a type after moving from another provider. See https://posthoghelp.zendesk.com/agent/tickets/10775

We default to "desktop" for user agents we can't identify so this points at their user base having a high proportion of useragents we don't match

They're on an older version of posthog-js that doesn't report raw user agent so we can't confirm that guess

Until they update we can target general improvements

This PR copies opensource test cases from other user agent matcher and uses them to improve our matching.

In this change we:

* don't try to attempt to start matching smart tvs
* improve matching for OS and device
* add matching for watchOS
* adds ~1.1k rather than the 6k of ua-parser-js
* pre-uglify the code to help roll-up minify it more by making many constants for repeated strings and using old fashioned concatenation to concat them together
  • Loading branch information
pauldambra authored Feb 27, 2024
1 parent 0dbb9d5 commit c52c53e
Show file tree
Hide file tree
Showing 7 changed files with 5,445 additions and 227 deletions.
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 0 additions & 52 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,58 +96,6 @@ describe('utils', () => {
}
})

it('osVersion', () => {
const osVersions = {
// Windows Phone
'Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 635; BOOST) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537':
{ os_name: 'Windows Phone', os_version: '' },
'Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36':
{
os_name: 'Windows',
os_version: '6.3',
},
'Mozilla/5.0 (iPhone; CPU iPhone OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) CriOS/44.0.2403.67 Mobile/12D508 Safari/600.1.4':
{
os_name: 'iOS',
os_version: '8.2.0',
},
'Mozilla/5.0 (iPad; CPU OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4':
{
os_name: 'iOS',
os_version: '8.4.0',
},
'Mozilla/5.0 (Linux; Android 4.4.2; Lenovo A7600-F Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.133 Safari/537.36':
{
os_name: 'Android',
os_version: '4.4.2',
},
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9300; es) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.480 Mobile Safari/534.8+':
{
os_name: 'BlackBerry',
os_version: '',
},
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36':
{
os_name: 'Mac OS X',
os_version: '10.9.5',
},
'Opera/9.80 (Linux armv7l; InettvBrowser/2.2 (00014A;SonyDTV140;0001;0001) KDL40W600B; CC/MEX) Presto/2.12.407 Version/12.50':
{
os_name: 'Linux',
os_version: '',
},
'Mozilla/5.0 (X11; CrOS armv7l 6680.81.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36':
{
os_name: 'Chrome OS',
os_version: '',
},
}

for (const [userAgent, osInfo] of Object.entries(osVersions)) {
expect(_info.os(userAgent)).toEqual(osInfo)
}
})

it('properties', () => {
const properties = _info.properties()

Expand Down
Loading

0 comments on commit c52c53e

Please sign in to comment.