Correct way to format user-agent string in an Android WebView App?

倖福魔咒の 提交于 2019-12-23 20:06:03

问题


In my android web app I want to modify my user agent so that I can identify it on my server and on google analytics.

Currently the user-agent looks like this:

Mozilla/5.0 (Linux; Android 7.0; Moto G (4) Build/NPJ25.93-14; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/56.0.2924.87 Mobile Safari/537.36

I should probably include a tag that google analytics can pick up as a web-app and another tag that mentions the version of my android app. This would be in addition to the webview version. I want google analytics to keep providing me the correct data for Mobile Device info, Android version, OS, etc. Probably only the Browser field should be changed.

I know how I can change the user agent string. I want to know what are the things I should keep in mind while creating a user-agent string.


回答1:


Thanks to this blog, I found the answer. You should append the "Browser" and "Browser version" to the WebView's existing user-agent string and google analytics will pick it up.

Code:

WebSettings webSettings = webView.getSettings();
String userAgent = String.format("%s [%s/%s]", webSettings.getUserAgentString(), "App Android", BuildConfig.VERSION_NAME);
webSettings.setUserAgentString(userAgent);

In Google Analytics the "Browser" dimension value is "App Android" and "Browser Version" is the string in your build config.



来源:https://stackoverflow.com/questions/43177023/correct-way-to-format-user-agent-string-in-an-android-webview-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!