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.

MethodRequiredReturns
getSession()Yesthe session parameter object below
getResources()Noa 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.

VariableMeaning
__sophonz_session_idSession id. Its presence is what identifies the legacy path
__sophonz_app_nameApp name; replaces appName from init()
__sophonz_app_versionNative app version; replaces appVersion, while the web-configured value is kept as web.version
__sophonz_app_keyApp key
__sophonz_projectProject (service.namespace)
__sophonz_collector_urlCollector URL
__sophonz_service_typeApp type; web when empty
__sophonz_deployment_environment_nameDeployment environment name
__sophonz_shared_sessionWhether session and app parameters are shared. Only when this is set is the page treated as a WebView
__sophonz_deactivatedtrue stops the SDK from running
__sophonz_resourcesAdditional 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

  1. webkit.messageHandlers.SophonzNative present → iOS bridge
  2. Otherwise window.__sophonz_session_id present → legacy iOS (window globals)
  3. Otherwise window.SophonzNative present → 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 attributeValue
service.versionThe native app's version passed by the bridge (__sophonz_app_version)
web.versionThe 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.