The WiseTrack WebBridge Plugin provides a seamless integration between the native Android environment and a web-based JavaScript SDK, enabling two-way communication between a WebView and the WiseTrack tracking system.
Add the WiseTrack SDK to your project:
-
Add the dependency to your
app/build.gradle:implementation 'io.wisetrack:sdk:webbridge:2.0.0' // Replace with the latest version
-
Sync your project with Gradle.
Just call following code to register and enable webview bridge, This registers the WiseTrack bridge as a Javascript interface in the web view
- Enable Javascript in the webview:
webView.settings.setJavaScriptEnabled(true) - Register WiseTrack Bridge Instance to web view:
WiseTrackBridge.getInstance(context, webview).register() - To dispose and unregister instance:
WiseTrackBridge.getInstance(context, webview).unregister()
class MainActivity : Activity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.webChromeClient = WebChromeClient()
webView.webViewClient = WebViewClient()
WiseTrackBridge.getInstance(applicationContext, webView).register()
try {
webView.loadUrl("file:///android_asset/wisetrack/index.html")
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onDestroy() {
// Unregister to avoid memory leaks
WiseTrackBridge.getInstance(applicationContext, webView).unregister()
super.onDestroy()
}
}These JavaScript files are provided to help you build and test web pages intended for display inside the WebView. You can use them as a reference or foundation when integrating WiseTrack functionality into your in-app HTML pages.
Located in: assets
Files include:
wisetrack.js: Main interface for invoking bridge methods.wt_config.js: Contains theWTInitConfigconstructor and configuration schema.wt_event.js: Defines theWTEventstructure for event logging.test.html: A standalone page to manually trigger SDK methods via a UI or console.