For the complete documentation index, see llms.txt.

Track WebViews

Last updated View as MarkdownSet up with AI

WebView tracking links the CSQ SDK to the JavaScript tag running inside your WebViews to support analytics and Session Replay for web content displayed within your app.

Set up the tag for WebView tracking

Section titled Set up the tag for WebView tracking

If tag auto-injection is enabled for your project, you can skip this step, because the Contentsquare SDK adds the tag to your WebViews for you. Tag auto-injection is currently in beta. If you're not sure whether it's enabled, check with your Contentsquare contact.

If tag auto-injection is not enabled, add the tag to your pages using one of the following methods:

HTML code

To set up the tracking tag on your website, add the following lines of code on every page you want to analyze, either at the end of the <body> tag.

Make sure to add the trackPageview call: without it, your WebView pages are not collected as pageviews.

<script type="text/javascript">
(function () {
window._uxa = window._uxa || [];
var tag_id = {{YOUR_TAG_ID}};
function isWebView() {
return navigator.userAgent.indexOf('CS_WebView') !== -1 ||
typeof CSJavascriptBridge != 'undefined' ||
!!window.CS_isWebView;
}
if (isWebView()) {
window._uxa.push(["setOption", "isWebView", true]);
tag_id = {{YOUR_WEBVIEW_TAG_ID}};
}
// Required so the page is collected as a pageview
window._uxa.push([
"trackPageview",
window.location.pathname + window.location.hash.replace("#", "?__")
]);
var mt = document.createElement("script");
mt.type = "text/javascript";
mt.async = true;
mt.src = `https://t.contentsquare.net/uxa/${tag_id}.js`;
document.getElementsByTagName("head")[0].appendChild(mt);
})();
</script>

This code creates a function which adds an asynchronous call to a script and then run the function. This is a way to avoid other elements loading being blocked on the page, and reduces the impact of the tag on your website's performance.

Google Tag Manager (Custom HTML)

If your page loads in both a web and WebView environment, you should use a variable to determine whether the page is running in a WebView context before integrating the tracking tag.

  1. Add a new tag on your workspace.

  2. Select "Choose a tag type to begin setup…".

  3. In the list, pick the "Custom HTML" tag.

  4. Copy and paste the following code in the Tag Configuration HTML section. Make sure to add the trackPageview call: without it, your WebView pages are not collected as pageviews.

    <script type="text/javascript">
    (function () {
    window._uxa = window._uxa || [];
    var tag_id = {{YOUR_TAG_ID}};
    function isWebView() {
    return navigator.userAgent.indexOf('CS_WebView') !== -1 ||
    typeof CSJavascriptBridge != 'undefined' ||
    !!window.CS_isWebView;
    }
    if (isWebView()) {
    window._uxa.push(["setOption", "isWebView", true]);
    tag_id = {{YOUR_WEBVIEW_TAG_ID}};
    }
    // Required so the page is collected as a pageview
    window._uxa.push([
    "trackPageview",
    window.location.pathname + window.location.hash.replace("#", "?__")
    ]);
    var mt = document.createElement("script");
    mt.type = "text/javascript";
    mt.async = true;
    mt.src = `https://t.contentsquare.net/uxa/${tag_id}.js`;
    document.getElementsByTagName("head")[0].appendChild(mt);
    })();
    </script>

  5. Select "Choose a trigger to make this tag fire…" and pick "All Pages" from the list.

Register each WebView in onCreate() and unregister it in onDestroy(), using registerWebView() and unregisterWebView():

import android.webkit.WebView
import android.webkit.WebViewClient
import com.contentsquare.CSQ
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() {}
CSQ.registerWebView(webView)
webView.loadUrl(DEFAULT_WEB_VIEW_CONTENT_URL)
}
override fun onDestroy() {
super.onDestroy()
val webView = findViewById<WebView>(R.id.webview)
CSQ.unregisterWebView(webView)
}

Register before you load a URL: registration only takes effect on the next WebView content reload.

If you set your own WebViewClient, set it before calling CSQ.registerWebView(webView). Registering wraps your client, so setting a new one afterwards replaces the wrapper and stops auto-injection.

In a Fragment, register in onCreateView() and unregister in onDestroyView().

import android.webkit.WebView
import android.webkit.WebViewClient
import com.contentsquare.CSQ
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view = inflater.inflate(R.layout.fragment_web_view, container, false)
val webView = view.findViewById<WebView>(R.id.webview)
webView.settings.javaScriptEnabled = true
webView.webViewClient = object : WebViewClient() {}
CSQ.registerWebView(webView)
webView.loadUrl(DEFAULT_WEB_VIEW_CONTENT_URL)
return view
}
override fun onDestroyView() {
super.onDestroyView()
val webView = requireView().findViewById<WebView>(R.id.webview)
CSQ.unregisterWebView(webView)
}

On the native side

There is no way of validating the implementation on the native side only.

On the web side

Once you arrive on the screen with the tracked WebView, you should see the following logs in sequence:

  1. JS interface injected:

    [WebView] Js interface added to the webView
  2. WebView registered:

    [WebView] WebView attached
  3. Tag configuration received — confirms successful tag to SDK communication:

    [WebView JS log] (TAG_CONFIGURATION) [Config]
  4. Tracking active:

    [WebView] onWebViewTrackingReady

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.

After the initial tag setup, the Contentsquare tag in your WebViews can do more: collect user consent, track pageviews and transactions, include WebViews in Session Replay, collect errors for Error Analysis, and send Product Analytics events.

See Advanced WebView tracking for the prerequisites and the full list of topics.