WebView Integration
How to integrate the Sophonz Browser SDK with a mobile native app's WebView so web screens continue the session the native SDK already started.
Sophonz Browser SDK can integrate with a mobile native app's WebView to automatically share native parameters. This enables unified instrumentation across native app and web application contexts.
WebView integration
When the Browser SDK initialises inside a native app's WebView, it adopts the session and app identity the native SDK already created, so a user's native screens and web screens land in one session.
There are two integration methods, both detected automatically at init. In a
plain browser neither is present, so the values passed to init() are used as-is.
Method 1: bridge
The native side injects a JavaScript interface and the SDK calls it. The interface differs per platform.
iOS — the native app registers webkit.messageHandlers.SophonzNative. The SDK
detects it and builds the window.SophonzNativeIOS bridge itself. The bridge posts
messages through invoke(action, data), and the native side answers on
window.SophonzNativeIOS.onNativeCallback(callbackId, result). Calls time out after
5 seconds.
Android — the native app injects a window.SophonzNative object. The SDK calls
two synchronous methods, each of which must return a JSON string.
| Method | Required | Returns |
|---|---|---|
getSession() | Yes | the session parameter object below |
getResources() | No | a resource attribute object |
Method 2: window globals
The native side writes window.__sophonz_* values directly before the page loads.
This path exists for older iOS WebViews that predate the bridge. The SDK identifies
it by the presence of window.__sophonz_session_id.
The bridge path writes what it receives into the same globals, so both methods read the same parameters.
| Variable | Meaning |
|---|---|
__sophonz_session_id | Session id. Its presence is what identifies the legacy path |
__sophonz_app_name | App name; replaces appName from init() |
__sophonz_app_version | Native app version; replaces appVersion, while the web-configured value is kept as web.version |
__sophonz_app_key | App key |
__sophonz_project | Project (service.namespace) |
__sophonz_collector_url | Collector URL |
__sophonz_service_type | App type; web when empty |
__sophonz_deployment_environment_name | Deployment environment name |
__sophonz_shared_session | Whether session and app parameters are shared. Only when this is set is the page treated as a WebView |
__sophonz_deactivated | true stops the SDK from running |
__sophonz_resources | Additional resource attributes |
These values override what was passed to init(). Without
__sophonz_shared_session the page is not treated as a WebView and the init()
values stand.
Detection order
webkit.messageHandlers.SophonzNativepresent → iOS bridge- Otherwise
window.__sophonz_session_idpresent → legacy iOS (window globals) - Otherwise
window.SophonzNativepresent → Android bridge
Resource attributes
Separately from the session parameters, resource attributes supplied by the native
side are merged into the resource on web spans. The iOS bridge fetches them through
its resource action, Android through getResources(), and either platform can also
set window.__sophonz_resources. All three are merged, with later reads overwriting
earlier ones.
Web version and source maps
In a WebView the app version and the web version move separately. The native binary ships through store review every few weeks; the web a WebView opens changes whenever the web team deploys.
The Browser SDK (2.1.0 and later) sends the two apart:
| Resource attribute | Value |
|---|---|
service.version | The native app's version passed by the bridge (__sophonz_app_version) |
web.version | The appVersion the page passed to init(). The bridge does not overwrite it |
The SDK keeps the page's value before the bridge replaces appVersion with the app's, so the page's value wins even if the native resource attributes carry a web.version of their own.
JavaScript errors in a WebView are resolved with the source maps of their web.version. Upload maps for each web build under that web version:
npx @sophonz/cli upload-sourcemaps --path dist --web-version "$WEB_VERSION"Use the token of the app the telemetry arrives under. With a shared bridge session the web telemetry uses the native app's app key, so upload with the native app's access token.
Settings > project > app > Mapping files shows a native app's app versions and web versions together, and flags web versions without source maps. The full upload guide is in Source map upload.