Track WebViews

The latest CSQ SDK is here! Learn how to upgrade your app.

Where to inject your WebViews?

Section titled Where to inject your WebViews?

It is essential that the following steps are done before an URL is loaded in your WebView. With that in mind, you should inject your webviews in onCreate() in Activity and onCreateView() in Fragment.

No matter where you decide to add the following injection code, keep in mind that it will not take effect until the next WebView content reload.

Use the following API:

CsWebViewManager.injectEventTrackingInterface(@NonNull WebView webView) // to start tracking the specific WebView
  1. Automatic tag injection - The SDK automatically injects the CS Tag from the project configuration in WebView mode if it is not already present. This gathers data on all your webviews, even when the user is offline or on pages showing local HTML.
  2. Manual tag injection - To complete the implementation, inject the CS Tag in WebView mode on your pages. See 📚 Mobile Apps WebView Tracking Documentation.

Let’s illustrate our implementation with an Activity. In your onCreate() method, you will add the following:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
val webView = findViewById<WebView>(R.id.webview)
webView.settings.javaScriptEnabled = true
webView.webViewClient = object : WebViewClient() {}
// Inject the Bridge between the CS Tag in WebView mode and the SDK
CsWebViewManager.injectEventTrackingInterface(webview)
webView.loadUrl(DEFAULT_WEB_VIEW_CONTENT_URL)
webView.reload()
}

If you are using Fragments, the approach is similar, but should be done in the onCreateView() method, as stated previously.

Validate your implementation of WebView tracking

Section titled Validate your implementation of WebView tracking

Currently, there is no way of validating the implementation on the native side only.

Validating the implementation on the web side

Section titled Validating the implementation on the web side

Once you arrive on the screen with the tracked WebView, you should see an initial confirmation log message:

WebView detected and WebView tracking enabled on native side

Specific logs are then emitted for:

  • Page views fired by the WebView Tracking Tag in the same format as for screen views: Screenview - Screen name: {{page name given}} - Screen number: 11
  • Taps and swipes detected by the WebView Tracking Tag in the same format as for defaults taps and swipe but with the target matching an HTML DOM element value: Tap - Target: {Last view info: div:eq(0)} - Unresponsive: false

Going further: If you still have doubt, you can look for logs by filtering on WebViewEventProcessor. Their presence will confirm that it is well implemented on both web and native sides.