detect_useragent_device_type - BigQuery

Function

detect_useragent_device_type(useragent)

Description

Detect Useragent Device Type - Detects the device type from a <useragent> string. It can be a tablet, mobile, PC, TV, or other.

Example Query


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")
                                            
/*--Output--
tablet
*/

Statement

CREATE OR REPLACE FUNCTION `your_project_id.your_dataset_id.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.''')

Regions

justfunctions.eu.detect_useragent_device_type(useragent),
justfunctions.us.detect_useragent_device_type(useragent)

Type

SQL User Defined Function (SQL UDF)

How to Use

Frequently Asked Questions

User-Defined Functions (UDFs) in Google BigQuery are custom functions that you can create to perform operations that aren't available through the standard SQL functions. These UDFs allow you to extend BigQuery's SQL capabilities to suit your specific data processing needs. JustFunctions is a collection of open-source user-defined functions (UDFs).

JustFunctions is a collection of open-source User-Defined Functions (UDFs) designed to extend the capabilities of Google BigQuery. These functions cover a wide range of applications, including text manipulation, URL processing, date processing, email handling, similarity measures, and more. Moreover, JustFunctions is frequently updated to include more use cases.

We welcome any feedback or questions you may have. You can Contact us or report an issue on Github.

Functions and procedures from JustFunctions can be used directly in any of your projects.
To start, simply click 👆 on any function,  Copy  the 'Example Query' and run it in your BigQuery console.
You can also  Copy  the 'Statement' to create your own private user-defined function.

Yes, JustFunctions is completely free to use.

Yes, currently JustFunctions is only available for Google BigQuery. In the future, we will also support PostgreSQL.

See something wrong? Contact us or report an issue on Github.