For the complete documentation index, see llms.txt.

Track Webviews

Dernière mise à jour Voir en Markdown

This SDK mainly receives critical fixes. New capabilities are shipping in the CSQ SDK first.

Migrate to the CSQ SDK using our AI wizard.

WebView tracking links the Contentsquare 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.

Where to inject your WebViews?

Section titled Where to inject your WebViews?

It is essential that you inject your WebViews in viewWillAppear().

Additionally, when setting a delegate on your WebView, ensure that it is implemented and set before calling Contentsquare.register(webView: WKWebView). If this order is not respected, your delegate will not receive any callbacks.

Use the following API:

// Start tracking the given WKWebView
Contentsquare.register(webView: WKWebView)

Let's illustrate our implementation with a UIViewController. In your viewWillAppear() method, add the following:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Inject the Bridge between the CS Tag in WebView mode and the SDK
Contentsquare.register(webView: webView)
}

Once you open the screen with the tracked WebView in your app, you should see the following logs in sequence:

  1. WebView registered:

    CSLIB ℹ️ Info: [WebView] Registering WebView on native side for page: [URL]. Waiting for Web Tracking Tag messages…
  2. Web page loaded:

    CSLIB ℹ️ Info: [WebView] Navigated to new page: [URL]. Waiting for Web Tracking Tag messages…

Once the Web Tracking tag runs in the web page, you should see the following logs in sequence:

  1. JavaScript bridge detected:

    CSLIB ℹ️ Info: [WebView] CSJavascriptBridge is detected on page: [URL]
  2. Tag configuration received — confirms successful tag to SDK communication:

    CSLIB ℹ️ Info: [WebView JS log] (TAG_CONFIGURATION) [Config]

Validating Pageview and Gestures tracking

Section titled Validating Pageview and Gestures tracking

Page views sent through the Web Tracking Tag are displayed the same way as native screen views:

CSLIB ℹ️ Info: Screenview - Screen name: "{{page name given}}" - Screen number: 11

Taps and swipes detected by the Web Tracking Tag are also displayed the same way as taps and swipe on native elements, but the end of the target contains HTML DOM elements:

CSLIB ℹ️ Info: Tap - Target: ...>UIViewControllerWrapperView:eq(0)>UIView:eq(0)>WKWebView:eq(0)|webview|img#picture-holder

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.