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.
WebView registration
Section titled WebView registrationUse the following API:
// Start tracking the given WKWebViewContentsquare.register(webView: WKWebView)// Start tracking the given WKWebViewContentsquare registerWithWebView:(WKWebView * _Nonnull)];Tag setup
Section titled Tag setupTag setup
Section titled Tag setup- 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.
- 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 sum it up
Section titled Let’s sum it upLet’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)}- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
// Inject the Bridge between the CS Tag in WebView mode and the SDK [Contentsquare registerWithWebView:self.webView];}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:
CSLIB ℹ️ Info: WebView tracking enabled on native side for page: [URL]. Waiting for Web Tracking Tag messages…Once the web page is loaded in the WebView, you should see the following log:
CSLIB ℹ️ Info: WebView navigated to new page: [URL]. Waiting for Web Tracking Tag messages…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:
CSLIB ℹ️ Info: Web Tracking Tag is detected on page: [URL]Validating Pageview and Gestures tracking
Section titled Validating Pageview and Gestures trackingPage 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: 11Taps 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