在Android WebView应用程序中格式化用户代理字符串的正确方法? [英] Correct way to format user-agent string in an Android WebView App?
问题描述
在我的android网络应用中,我想修改用户代理,以便可以在服务器和Google Analytics(分析)上对其进行识别.
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.
当前用户代理如下:
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
我可能应该包括一个标签,谷歌分析可以将其用作网络应用程序,另一个标签中应提及我的Android应用程序的版本.这将是webview版本的补充.我希望Google Analytics(分析)能够继续为我提供有关移动设备信息,Android版本,操作系统等的正确数据.可能仅应更改浏览器"字段.
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.
推荐答案
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.
代码:
WebSettings webSettings = webView.getSettings();
String userAgent = String.format("%s [%s/%s]", webSettings.getUserAgentString(), "App Android", BuildConfig.VERSION_NAME);
webSettings.setUserAgentString(userAgent);
在Google Analytics(分析)中,浏览器"维度值为"App Android",浏览器版本"为构建配置中的字符串.
In Google Analytics the "Browser" dimension value is "App Android" and "Browser Version" is the string in your build config.
这篇关于在Android WebView应用程序中格式化用户代理字符串的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!