Web View Event Tracking
This section is applicable to you if you have webview in your app and you want to track the user behaviour in the webview as App events.
A quick summary of what you can do via our native-to-webview bridge is given below:
- You can get values such as GUID, OS, Token, Platform, etc. from the SDK
- You can send additional parameters with GUID via a javascript method.
- A call from javascript to SDK can be made where you can pass events tracked in the webview to the SDK.
- You can auto-track events by either passing true/false value. If the value is set as false, then a callback will be given to the App where you can handle the events and in case of true, the SDK will handle events by itself.
Passing parameters to WKWebview
This method creates javascript which includes developer provided app info and CEE SDK info to WKWebview.
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
WKUserContentController *controller = [WKUserContentController new];
controller = self.webView.configuration.userContentController;
[controller addUserScript:[[Smartech sharedInstance] getSmartechAppWebScript:dict]];
[controller addScriptMessageHandler:self name:[[Smartech sharedInstance] getSmartechAppWebMessageHandler]];
let dict = [
"key1" : "value1",
"key2" : "value2"
]
var controller = WKUserContentController()
if let userContentController = webView?.configuration.userContentController {
controller = userContentController
}
controller.addUserScript(Smartech.sharedInstance().getSmartechAppWebScript(dict))
controller.addScriptMessageHandler(self, name: Smartech.sharedInstance().getSmartechAppWebMessageHandler())
This method gets a messageHandler string used in app webview callback.
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
[[Smartech sharedInstance] appWebDidReceiveScriptMessage:message];
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
Smartech.sharedInstance().appWebDidReceiveScriptMessage(message)
}
This delegate method will be triggered when the auto-track is not enabled from the web app page.
//Set delegate
[Smartech sharedInstance].smtAppWebViewDelegate = self;
- (void)handleAppWebViewEvent:(nonnull WKScriptMessage *)message {
NSLog(@"Handle webView event = %@", message.body);
}
Updated 3 months ago