detect_useragent_device_type(useragent)
Detects the device type from a <useragent> string. It can be a tablet, mobile, PC, TV, or other.
CREATE OR REPLACE FUNCTION `justfunctions.eu.detect_useragent_device_type`(`useragent` STRING)
RETURNS STRING AS (CASE
WHEN REGEXP_CONTAINS(useragent, r'(?i)(iPad|Tablet|Kindle|Tab|GT-P)') THEN 'tablet'
WHEN REGEXP_CONTAINS(useragent, r'(?i)(Android)') AND NOT REGEXP_CONTAINS(useragent, r'(?i)(Mobile)') THEN 'tablet'
WHEN REGEXP_CONTAINS(useragent, r'(?i)(Mobile|iPhone|Android|Windows Phone)') THEN 'mobile'
WHEN REGEXP_CONTAINS(useragent, r'(?i)(SmartTV|AppleTV|GoogleTV|HbbTV|netcast|NETTV|OpenTV|Tizen|Web0S|SonyDTV)') THEN 'tv'
WHEN REGEXP_CONTAINS(useragent, r'(?i)(Windows NT|Macintosh|Linux|X11)') THEN 'pc'
ELSE 'other'
END
)
OPTIONS ( description = '''Detects the device type from a <useragent> string. It can be a tablet, mobile, PC, TV, or other.''')
Example
SELECT `justfunctions.eu.detect_useragent_device_type`("Mozilla/5.0 (iPad; CPU OS 12_5_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Mobile/15E148 Safari/604.1")
tablet