Track WebViews
Enable tracking in WebViews
Section titled Enable tracking in WebViewsWhere to inject your WebViews?
Section titled Where to inject your WebViews?It is essential that the registration is done before a URL is loaded in your WebView. With that in mind, you should register your WebViews in onCreate() for Activities and onCreateView() for Fragments. Unregister them in the corresponding onDestroy() or onDestroyView() methods.
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.
WebView tracking lifecycle
Section titled WebView tracking lifecycleUse the CSQ.registerWebView() API to start tracking a specific WebView and CSQ.unregisterWebView() to stop tracking it.
Tag setup
Section titled Tag setupTo complete the implementation, inject the CS Tag in WebView mode on your pages.
See 📚 Mobile Apps WebView Tracking Documentation.
Full example
Section titled Full exampleHere is an Activity implementation. Register the WebView in onCreate() and unregister it in onDestroy():
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 CSQ.registerWebView(webView)
webView.loadUrl(DEFAULT_WEB_VIEW_CONTENT_URL) webView.reload()}
override fun onDestroy() { super.onDestroy()
val webView = findViewById<WebView>(R.id.webview) CSQ.unregisterWebView(webView)}@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view);
WebView webView = findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() {});
// Inject the Bridge between the CS Tag in WebView mode and the SDK CSQ.registerWebView(webView);
webView.loadUrl(DEFAULT_WEB_VIEW_CONTENT_URL); webView.reload();}
@Overrideprotected void onDestroy() { super.onDestroy();
WebView webView = findViewById(R.id.webview); CSQ.unregisterWebView(webView);}If you are using Fragments, the approach is similar, but should be done in the onCreateView() method for registration and onDestroyView() method for unregistration.
Validate your implementation of WebView tracking
Section titled Validate your implementation of WebView trackingCurrently, 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 sideOnce 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 sideSpecific 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.