Search code examples
androidwebviewgoogle-analyticsyoutube

How can I differentiate Android Chrome Browser and In-App WebView user agents?


I am currently working on an Android App that uses WebViews to load some pages (I cannot modify the HTML, because it is being used by the iOS App and the website). First we had the problem, that the user agent was the same for the InApp WebViews and a regular Android Browser (starting with Android 4.4 which started using chromium as the webengine).

InApp Webview Example:

Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.117 Mobile Safari/537.36

So in Google Analytics we could not differentiate between users using the App and users visiting the HTML page with the normal browser. The solution was to alter the Useragent String with:

webView.getSettings().setUserAgentString("Our custom Mobile Android InApp Browser");

This worked for Google Analytics.

But now we ran into the problem that Youtube is not identifying InApp users as mobile users and is serving the desktop Youtube player.

Now I am looking for one of the following solutions:

1) Somehow make Google Analytics identify the In-App-Browser as different from the normal Android Browser.

or

2) Have some custom String for the webview-user-agent that causes Youtube to serve its mobile version.

Thanks!


Solution

  • Setting a custom User-Agent string seems like a good solution.

    Just make sure this custom string can be recognized by Youtube as a mobile Android device. For example, you could use the "field" used to store the device model:

    Dalvik/1.6.0 (Linux; U; Android 4.0.3; <Your_custom_marker> Build/IML74K)