You can add Google Analytics to a WebView app in two ways. The simple one is to keep the GA4 tag on your website, so the pages report to your web data stream whenever the app loads them. The thorough one is to add the Firebase SDK to the app and forward web events to it through a JavaScript bridge, so web and native events share one app stream.
Which route fits depends on whether you can change the app’s Android code.
Two ways to get Google Analytics data from a WebView app
A WebView app gets analytics either from the website’s own tag or from a native SDK fed by the web page.
| GA4 tag on the website | Firebase bridge in the app | |
|---|---|---|
| Native code needed | No | Yes: SDK, Java class, interface |
| Where data lands | Web data stream | Android app data stream |
| Sees installs and app opens | No | Yes |
| Works with no-code wrappers | Yes | Only if the builder adds it |
| Main risk | App and browser traffic mixed together | Exposing a JavaScript interface |
In GA4 you add a data stream by picking iOS app, Android app or Web. Picking an app stream creates a matching Firebase project and links it to your property. The bridge route needs a Firebase project; the tag route doesn’t.
Option 1: keep the GA4 tag on your website
The GA4 tag on your site keeps working inside a WebView app, because the WebView runs the page’s own JavaScript. You don’t touch the app at all. Page views, clicks and form events go to the same web stream your browser visitors use.
Two app settings decide whether it works:
- JavaScript must be on. Android’s WebView guide notes that JavaScript is disabled in a WebView by default and is enabled with
setJavaScriptEnabled(true). - Cookies must stick. Per the CookieManager reference, a WebView accepts cookies by default. Apps targeting Lollipop or later block third-party cookies by default, and
flush()writes cookies to persistent storage. If cookies don’t persist between launches, expect returning users to be counted as new.
If sign-ins also drop between launches, the fix is the same cookie work covered in keeping users logged in.
How to separate app visits from browser visits
App visits and browser visits mix together on the web stream unless you mark them. Use both of these.
Tag the start URL. Set the app’s start address to something like https://example.com/?utm_source=android_app&utm_medium=app&utm_campaign=launcher. Google’s campaign URL article says you should always use utm_source, utm_medium and utm_campaign, and that missing ones show as “(not set)”. The values then appear under Acquisition > Traffic acquisition as Session source/medium. Values are case sensitive, so pick one spelling and keep it.
Read the user agent. Android’s user-agent reduction post says sites can detect WebView through the wv token, unless the app overrides the user agent. You can read navigator.userAgent in your page and send a custom dimension such as app_shell: true. If you control the app, appending your own token is cleaner; see setting a custom user agent.
Say a bakery wraps its order site and tags the start URL with utm_source=android_app. A week later, app sessions sit on their own row in Traffic acquisition, and the owner can compare checkout rates for app users against mobile browser users.
Option 2: forward web events to Firebase Analytics
The Firebase bridge sends events from the web page to the Firebase Analytics SDK inside the app. Firebase’s WebView guide says calls fired in a WebView must be forwarded to native code before they reach Google Analytics. Here’s the Android setup it describes.
- Add Firebase. Your app needs a Firebase project with the Analytics SDK, plus the INTERNET permission. The Firebase codelab lists both as prerequisites.
- Write the web wrapper. In your page, define
logEvent(name, params)andsetUserProperty(name, value). Each checks forwindow.AnalyticsWebInterfaceand callswindow.AnalyticsWebInterface.logEvent(name, JSON.stringify(params)). In a normal browser the interface is missing, so the wrapper does nothing native. - Write the Java class. Create
AnalyticsWebInterfacewith public methods marked@JavascriptInterface. ItslogEventturns the JSON string into a Bundle and calls the FirebaselogEvent; itssetUserPropertypasses straight through. - Register it. Call
addJavascriptInterface(new AnalyticsWebInterface(this), "AnalyticsWebInterface")on the WebView. The guide only adds it on JELLY_BEAN_MR1 and above, citing security concerns. - Turn on JavaScript. Same
setJavaScriptEnabled(true)call as before.
The codelab’s JSON-to-Bundle helper maps String, Integer and Double values and logs a warning for anything else. Nested objects and arrays won’t arrive as you sent them, so keep params flat.
Security rules for the JavaScript bridge
The JavaScript bridge is safe only when every page in the WebView is yours. Android’s WebView guide puts it bluntly: addJavascriptInterface lets JavaScript control your app, so don’t use it unless you wrote all the HTML and JavaScript in that WebView. The same guide says not to let users browse to pages that aren’t yours inside it.
So send outside links to the browser; the post on opening external links in the browser shows how. The bridge object runs on a different thread than the one that built it, so don’t touch UI from those methods.
How to check that events arrive
Firebase’s debug mode shows bridged events quickly. The codelab gives this command:
adb shell setprop debug.firebase.analytics.app package_name
Replace package_name with your app’s ID. For the web-tag route, open the page in the app and inspect it with remote debugging, as covered in debugging a WebView with Chrome DevTools. If GA requests never fire, check the JavaScript setting first.
Declare analytics in the Play Data safety form
Analytics data counts in the Google Play Data safety form, on both routes. Google’s Data safety help page says you must reflect data collected by third-party libraries or SDKs. It also covers data collected from a WebView opened from your app, when your app controls the code delivered through it.
The form lists “Analytics” as a purpose, and its “Device or other IDs” type includes the Firebase installation ID. Our Data safety walkthrough for WebView apps goes through each answer.
Which route should you pick?
Start with the GA4 tag and a tagged start URL. It costs nothing, needs no app rebuild, and answers what most small sites want to know: which pages app users open and whether they convert. Add the Firebase bridge only when you need install, retention or app-open numbers.
The web-only route is blind to anything outside your pages: installs, launches with no network, crashes. If those numbers matter, you need native code.
That limit applies to apps built with WebToAPK Free. The builder packages a generic Android template with options for app name, package name, icon, colours, navigation and sections, so there’s no place to add the Firebase SDK. Your website’s own tag is the analytics route for those apps.
FAQ
Does my website’s GA4 tag work inside a WebView app?
Yes, as long as JavaScript is enabled in the WebView. The page loads its tag the same way it does in a browser, so page views and web events reach your web data stream. It can’t see app-level events such as installs.
Why does Google say to forward WebView events to native code?
Firebase’s guide says events and user properties fired inside a WebView must be forwarded to native code before they reach Google Analytics. That way they land in the app’s data stream next to the app’s own events, under one app identity.
Is addJavascriptInterface safe for an analytics bridge?
Only for pages you wrote. Android’s documentation warns that the interface lets JavaScript control your app and says not to use it unless all the HTML and JavaScript in the WebView is yours.
Do I need to declare analytics in the Play Data safety form?
Yes. Google Play says data collected by third-party SDKs counts, and so does data collected from a WebView whose code your app controls. Analytics is one of the purposes the form lists.
Sources
- Use Analytics in a WebView, Firebase
- Implementing Google Analytics for Firebase in Android Webview, Firebase codelab
- Set up Analytics for a website and/or app, Analytics Help
- Build web apps in WebView, Android Developers
- CookieManager reference, Android Developers
- URL builders: Collect campaign data with custom URLs, Analytics Help
- User-Agent reduction on Android WebView, Android Developers Blog
- Provide information for Google Play’s Data safety section, Play Console Help