Go Browser Package #
I have been working on a project that required me to identify the browser, device, platform, or bot that is making a request to my application. The URL shortening service needed to generate analytics reports based on the browser, device, platform, or bot that was making a request to the application. Also, the service had to render Open Graph (OG) tags that were optimized for the browser, device, platform, or bot that was making a request to the application.
I wanted to build the service in Go, as it is a fast and efficient language well-suited for building web applications. However, I could not find a simple and easy-to-use package that allowed me to identify the browser, device, platform, or bot that was making a request to my application. I decided to build the Browser package to fill this gap.
Why use the Browser package? #
The Browser package is a simple and easy-to-use package that allows you to identify the browser, device, platform, or bot that is making a request to your application. It is a standalone package that does not require external dependencies and can be easily integrated into your application. It does not add overhead to your application and is designed to be fast and efficient.
Accurate and reliable #
The regex-based matchers provided by the Browser package are highly accurate and can identify a wide range of browsers, devices, platforms, and bots. The package is regularly updated to ensure it is up-to-date with the latest browser, device, platform, and bot information.
Tested against commercial user-agent database #
The regex are tested against our 250 Million user-agent database that I bought from a company that has been collecting user-agent data for over 10 years. This ensures the regexes are accurate and reliable and can identify various browsers, devices, platforms, and bots. This also helps me to improve the regex and add new ones as needed.
The Browser package can be used in a variety of use cases, including:
Analytics: Identify the browser, device, platform, or bot that is making a request to your application and use this information to generate analytics reports. Content delivery: Deliver content that is optimized for the browser, device, platform, or bot that is making a request to your application. SEO: Improve your search engine optimization (SEO) by delivering optimized content to search engine bots for their requirements. OG Tags: Generate Open Graph (OG) tags that are optimized for the browser, device, platform, or bot that is making a request to your application. User experience: Deliver content that is optimized for the browser, device, platform, or bot making a request to your application to provide a better user experience. Security: Identify and block malicious bots making requests to your application.
Quick Start #
To install the Browser package, you can use the below command:
go get github.com/dineshgowda24/browser
Import the package into your application and create a new instance of the Browser struct by passing the user agent string to the NewBrowser function.
package main import ( "fmt" "github.com/dineshgowda24/browser" ) func main () { b , err := browser . NewBrowser ( "Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36" ) if err != nil { // handle error } }
Matchers identify the browser making a request to your application. The package currently supports a wide range of matchers that can identify most of the popular browsers in use today.
To use the matchers, you can create a new instance of the Browser struct.
b , err := browser . NewBrowser ( "Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36" ) if err != nil { // handle error } // browser level information fmt . Println ( b . Name ()) // Chrome fmt . Println ( b . Version ()) // 90.0.4430.210 fmt . Println ( b . ShortVersion ()) // 90 fmt . Println ( b . IsBrowserKnown ()) // true fmt . Println ( b . IsChrome ()) // true
What if the browser is not supported? #
The matchers can be extended by adding a custom regex to the package. This allows you to identify browsers not supported by the package out of the box.
The custom matcher should implement the below interface:
type Matchers interface { Match () bool // Match returns true if the user agent string matches the matcher. Name () string // Name returns the name of the matcher. } type BrowserMatcher interface { Matcher Version () string // Version returns the full version of the browser. }
You can raise an issue or create a pull request to add the custom browser to the package. If it meets the requirements, I will be happy to look at it and merge it.
Supported Matchers #
The package currently supports the following matchers:
Platforms are used to identify the platform that is making a request to your application. Examples of platforms include Windows, macOS, Linux, iOS, and Android.
To use the platforms, you can create a new instance of the Browser struct and call it the Platform method. If the user agent string matches any of the platforms, the Platform method will return the name of the platform.
b , err := browser . NewBrowser ( "Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36" ) if err != nil { // handle error } // platform level information fmt . Println ( b . Platform (). Name ()) // Android fmt . Println ( b . Platform (). Version ()) // 10 fmt . Println ( b . Platform (). IsAndroidApp ()) // false
What if the platform is not supported? #
If the platform is not supported by the package out of the box, you can add custom platforms by implementing the below interface:
type Matchers interface { Match () bool // Match returns true if the user agent string matches the matcher. Name () string // Name returns the name of the matcher. } type PlatformMatcher interface { Matcher Version () string // Version returns the version of the platform. }
You can raise an issue or create a pull request to add the custom platform to the package. I will happily look at it and merge it if it meets the requirements.
Supported Platforms #
The package currently supports the following platforms:
Devices are used to identify the device that is making a request to your application. Examples of devices include desktops, laptops, tablets, and smartphones.
To use the devices, you can create a new instance of the Browser struct and call it the Device method. If the user agent string matches any of the devices, the Device method will return the name of the device.
b , err := browser . NewBrowser ( "Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36" ) if err != nil { // handle error } // device level information fmt . Println ( b . Device (). Name ()) // Samsung SM-A205U fmt . Println ( b . Device (). IsTablet ()) // false fmt . Println ( b . Device (). IsSamsung ()) // true
What if the device is not supported? #
If the device is not supported by the package out of the box, you can add custom devices by implementing the below interface:
type Matchers interface { Match () bool // Match returns true if the user agent string matches the matcher. Name () string // Name returns the name of the matcher. } type DeviceMatcher interface { Matcher }
You can raise an issue or create a pull request to add the custom browser to the package. I will happily look at it and merge it if it meets the requirements.
Supported Devices #
Bots are used to identify the bot that is making a request to your application. Examples of bots include Googlebot, Bingbot, and Yandexbot.
To use the bots, you can create a new instance of the Browser struct and call it the Bot method. If the user agent string matches any bots, the Bot method will return the bot’s name.
b , err := browser . NewBrowser ( "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" ) if err != nil { // handle error } // browser level information fmt . Println ( b . Name ()) // Unknown Browser fmt . Println ( b . Version ()) // 0.0 fmt . Println ( b . ShortVersion ()) // 0 fmt . Println ( b . IsBrowserKnown ()) // false fmt . Println ( b . IsUnknown ()) // true // bot level information fmt . Println ( b . Bot (). Name ()) // "Googlebot" fmt . Println ( b . Bot (). IsBot ()) // true fmt . Println ( b . Bot (). Why ()) // *bots.Known
Supported Bots #
The package supports a large number of bots. To see the complete list of supported bots, please refer to the documentation