Note
Cette page est affichée en anglais car elle n'est pas disponible dans votre langue.
A newer version of this documentation is available. Switch to the
latest version
docs.
Note
To enable WebView tracking, it is required to build a JavaScript Bridge between the content of the WebView and the native SDK. To do so, you will have to implement the Contentsquare WebView JavaScript Tracking Tag in the web pages called in your app WebViews.
📚 WebView JavaScript Tracking Tag Documentation
Once the WebView Tracking Tag is implemented in the web pages, you need to wrap any tracked WebView in the ContentsquareWebViewTrackerBuilder
Widget. See:
Note
If you are using a different package to use WebViews, see the Generic API .
If you are using the official WebView plugin ↗ , the following code can be used to track a webview:
ContentsquareWebViewTrackerBuilder (
builder : (context , tracker) {
initialUrl : 'https://your_tracked_webview.com' ,
javascriptMode : JavascriptMode . unrestricted ,
javascriptChannels : tracker . javascriptChannels ,
onWebViewCreated : tracker . onWebViewCreated ,
onPageFinished : tracker . onPageFinished ,
To use a custom WebView package, you can use the ContentsquareWebViewTrackerBuilder.custom
widget constructor.
The builder gives you a tracker of which you need to consider the following elements:
tracker.registerWebViewController
tracker.javascriptChannels
: Register these channels which are used to communicate between Contentsquare and your WebView
tracker.initializeWebViewTracking
: This must be called after initializeWebViewTracking and when the javascriptChannels
are registered.
Attention
You also need JavaScript injection to be allowed in your WebView.
See the following section for a concrete example of implementation with the flutter_inappwebview ↗ package.
Note
If you need help implementing webview tracking for a package not mentioned here, contact us.
If you are using the flutter_inappwebview ↗ , the following code can be used to track a webview:
ContentsquareWebViewTrackerBuilder (
builder : (context , tracker) {
initialUrlRequest : URLRequest (
url : Uri . parse ( 'https://your_tracked_webview.com' ) ,
onWebViewCreated : (controller) {
tracker . registerWebViewController (
ContentsquareWebViewController (
evaluateJavascript : (source) async {
return ' ${ await controller . evaluateJavascript ( source : source )} ' ;
currentUrl : () async => ' ${ await controller . getUrl ()} ' ,
javascriptChannelCall : (channelName , data) {
return 'window.flutter_inappwebview.callHandler(" $ channelName ", $ data )' ;
for ( final javascriptChannel in tracker . javascriptChannels) {
controller . addJavaScriptHandler (
handlerName : javascriptChannel . name ,
return javascriptChannel . onMessageReceived (args . first) ;
onLoadStop : (controller , uri) async {
// This MUST be called AFTER the controller is registered and the JavaScript
// channels are available
await tracker . startPageTracking () ;
Note
JavaScript injection is enabled by default, therefore there is no mention of it in the code above.
If you are using the webview_flutter ↗ , the following code can be used to track a webview:
Start by defining a WebViewTrackerInitializer
widget:
class WebViewTrackerInitializer extends StatefulWidget {
const WebViewTrackerInitializer ({
required this . initialUrl ,
required this . webviewController ,
final ContentsquareWebViewTracker tracker ;
final WebViewController webviewController ;
State < WebViewTrackerInitializer > createState () =>
_WebViewTrackerInitializerState () ;
class _WebViewTrackerInitializerState extends State < WebViewTrackerInitializer > {
Future < void > _initTracking () async {
final tracker = widget . tracker ;
final controller = widget . webviewController ;
await controller . setJavaScriptMode ( JavaScriptMode . unrestricted) ;
tracker . registerWebViewController (
ContentsquareWebViewController (
evaluateJavascript : (source) async {
return ' ${ await controller . runJavaScriptReturningResult ( source )} ' ;
currentUrl : controller . currentUrl ,
javascriptChannelCall : (channelName , data) {
return 'window. $ channelName .postMessage( $ data )' ;
for ( final javascriptChannel in tracker . javascriptChannels) {
await controller . addJavaScriptChannel (
onMessageReceived : (javascriptMessage) {
javascriptChannel . onMessageReceived (javascriptMessage . message) ;
await controller . setNavigationDelegate (
onPageFinished : (_) async {
// This will be called AFTER the controller is registered and the
// JavaScript channels are available since we register the controller
// and the JavaScript channels BEFORE loading the initial url.
await tracker . startPageTracking () ;
await controller . loadRequest ( Uri . parse (widget . initialUrl)) ;
Widget build ( BuildContext context) => widget . child ;
Which you can then use to track your webview:
ContentsquareWebViewTrackerBuilder (
builder : (context , tracker) {
return WebViewTrackerInitializer (
initialUrl : 'https://your_tracked_webview.com' ,
webviewController : webviewController ,
child : WebViewWidget (controller : webviewController) ,
Note
JavaScript injection is enabled by default, therefore there is no mention of it in the code above.
If you are using the webview_flutter ↗ v2 or v3, the following code can be used to track a webview:
ContentsquareWebViewTrackerBuilder (
builder : (context , tracker) {
initialUrl : 'https://your_tracked_webview.com' ,
javascriptMode : JavascriptMode . unrestricted ,
onWebViewCreated : (controller) {
tracker . registerWebViewController (
ContentsquareWebViewController (
evaluateJavascript : controller . runJavascriptReturningResult ,
javascriptChannelCall : (channelName , data) {
return 'window. $ channelName .postMessage( $ data )' ;
for ( final channel in tracker . javascriptChannels)
onMessageReceived : (message) => channel . onMessageReceived (
onPageFinished : (_) async {
await tracker . startPageTracking () ;
Note
JavaScript injection is enabled by default, therefore there is no mention of it in the code above.
Once you arrive on the screen with the tracked WebView, you should see the following log:
┌───────────────────────────────────────────────────────────────────────────────
│ ℹ️ INFO ℹ️ (CSLIB) [ Tracked WebView ]
├───────────────────────────────────────────────────────────────────────────────
│ WebView tracking enabled on WebView with initialUrl:
│ https://your.tracked.website
└───────────────────────────────────────────────────────────────────────────────
Once you arrive on the screen with the tracked WebView, you should see the following log:
┌───────────────────────────────────────────────────────────────────────────────
│ ℹ️ INFO ℹ️ (CSLIB) [ Tracked WebView ] ⟶ Event "sendMessage"
├───────────────────────────────────────────────────────────────────────────────
│ {"message":"ready, v:06c8d9bbfdf0e4bb9041b784e95c751873654cb0"}
└───────────────────────────────────────────────────────────────────────────────