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 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.
Inject a variable to detect when you load your page from a WebView
Section titled Inject a variable to detect when you load your page from a WebViewIf your page can be loaded both in a WebView and in a browser, you need to differentiate both in order to inject the proper CS Tag first.
If you don’t already have a system in place, you can rely on window.CS_isWebView
. This global variable is injected in your page by Contentsquare when the content is loaded from a WebView.
Inject the Bridge between the CS Tag in WebView mode and the SDK
Section titled Inject the Bridge between the CS Tag in WebView mode and the SDKUse the following API:
This step is threefold:
- It will inject our JavaScript Bridge in order to connect the CS Tag in WebView mode and our SDK.
- It will inject your CS Tag in WebView mode if it was not already injected, allowing us to gather data on all your webviews, even when the user is offline, on pages showing local HTML.
- It will inject a
window.CS_isWebView
variable for you to use in order to detect if the page is loaded from a WebView.
Inject the CS Tag in WebView mode into your HTML page
Section titled Inject the CS Tag in WebView mode into your HTML pageAfter doing all the above, in order for the implementation to be completed, you will need to make sure our CS Tag in WebView mode is injected on your pages. To do so, refer to 📚 Mobile Apps WebView Tracking Documentation.
Let’s sum it up
Section titled Let’s sum it upLet’s illustrate our implementation with a UIViewController
. In your viewWillAppear()
method, add the following:
Automatic tag injection
Section titled Automatic tag injectionAs of version 4.35.0, the SDK will automatically inject the CS Tag in WebView mode if it was not already injected. This will allow us to gather data on all your webviews, even when the user is offline, on pages showing local HTML.
Validate WebView tracking
Section titled Validate WebView trackingOn the native side
Section titled On the native sideOnce you open the screen with the tracked WebView in your app, you should see the following log:
Once the web page is loaded in the WebView, you should see the following log:
On the Web side
Section titled On the Web sideOnce the Web Tracking tag is detected in the web page, you should see the following log:
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:
- 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: