Track WebViews

In order to enable WebView tracking, two steps are required:

  1. Inject the Contentsquare Tracking Tag in WebView mode in the web pages loaded in your app’s WebViews. For more information on this step, refer to 📚 Mobile Apps WebView Tracking Documentation..
  2. Make sure JavaScript is enabled in your WebViews.
  3. Wrap your WebView widget with ContentsquareWebViewWrapper widget and implement it as explained below.

The implementation can be done in two different ways depending on how your WebView allows the injection of scripts. These two different ways relies of two subtypes of WebViewWrapperDelegate:

  • UserScriptWebViewWrapperDelegate to inject the required Contentsquare script through User Scripts. Choose this if you use the flutter_inappwebview package.
  • JSChannelWebViewWrapperDelegate to inject the required Contentsquare script through JavaScript channel. Choose this if you use packages like webview_flutter or flutter_webview_plugin.

This example uses the flutter_inappwebview InAppWebView widget.

ContentsquareWebViewWrapper(
delegate: UserScriptWebViewWrapperDelegate(
builder: (BuildContext context, String userScript) {
return InAppWebView(
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
javaScriptEnabled: true,
),
),
initialUrlRequest: URLRequest(
url: Uri.parse(url),
),
initialUserScripts: UnmodifiableListView([
UserScript(
// Inject the script properly
// by setting the `userScript` as source
source: userScript,
// Set the injectionTime as AT_DOCUMENT_START
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START,
),
]),
);
},
),
)

To validate the tracking of WebViews, refer to the platform-specific instructions.