APP与JS数据打通

要做到数据打通,APP端和JS端都要集成sdk,并且APP需做相应配置,才能生效

在 App 内有加载一些 H5 页面时,需要将用户行为连贯起来,要做如下配置,打通两端行为数据:

Android

1. 系统自带WebView

//在WebView初始化之后设置
DatatistSDK.getDatatistWebViewInstance(this).showUpWebView(webView,false);

2. X5 WebView

//在WebView初始化之后设置
DatatistSDK.getDatatistWebViewInstance(this).showUpX5WebView(webView);

iOS

1 SDK 初始化之后,请调用如下方法,用来设置webview的userAgent

[DatatistTracker addUserAgentToWebview]

2 如果是UIWebView: 在 WebView的webViewDidStartLoad方法中加入如下代码

@property (strong,nonatomic) JSContext *context;
...
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    //从webview上获取相应的JSContext。
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context[@"callHandler"] = ^(NSString *string, NSDictionary *eventBody, id back){
        [DatatistTracker.sharedInstance trackJSEvent: eventBody];
    };
}

3 如果是WKWebView

- (void)viewWillAppear:(BOOL)animated {
    [_webView.configuration.userContentController addScriptMessageHandler:self name:@"callHandler"];
}
- (void)viewWillDisappear:(BOOL)animated {
    [_webView.configuration.userContentController removeScriptMessageHandlerForName:@"callHandler"];
}
- (void)userContentController:(WKUserContentController *)userContentController
     didReceiveScriptMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:@"callHandler"]) {
        NSDictionary * messageDict = (NSDictionary *)message.body;
        [DatatistTracker.sharedInstance trackJSEvent: messageDict];
    }
}

JS

不用做其它设置,正常接入SDK调用API。

© Datatist all right reserved,powered by Gitbook更新时间: 2019-10-28 11:51:21

results matching ""

    No results matching ""