• Android Mobile Web 集成 Webtrends


    最近,需要在Sencha Touch + Phonegap的架构中在Android下集成Webtrends,记录下一些过程,

    查了下官网SDK说明,看起来是支持在混合模式下做点事情的,大概步骤如下,

       

    1. Add a custom WebViewClient to the WebView to handle communication from JavaScript in the embedded web content to the native application.
      • For a basic implementation, use the extendsWebView helper method in the WebtrendsDataCollector to add the Webtrends custom WebViewClient:
        String URL = "file:///android_asset/WebContent/webViewContent.html";
        WebView wv = (WebView)findViewById(R.id.webView1);
        wv.getSettings().setJavaScriptEnabled(true);
        WebtrendsDataCollector.getInstance().extendsWebView(wv);      
        wv.loadUrl(URL);
      • If your application uses its own custom WebViewClient, you should not use the extendsWebView helper method. Instead, ensure that the WebtrendsWebViewClient also gets invoked by following these steps:
      1. Modify your custom WebViewClient to extend WebtrendsWebViewClient instead of WebViewClient.
      2. If you are overriding onLoadResource, call super.onLoadResource.
      3. Set your custom WebViewClient on your WebView.
        import android.webkit.WebView;
        import com.webtrends.mobile.analytics.android.webViewExtension.WebtrendsWebViewClient;

        public class MyCustomWebViewClient extends WebtrendsWebViewClient{
                @Override
                public void onLoadResource(WebView view, String url){
                        super.onLoadResource(view, url);
                }
        }
        String URL = "file:///android_asset/WebContent/webViewContent.html";
        WebView wv = (WebView)findViewById(R.id.webView1);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new MyCustomWebViewClient());      
        wv.loadUrl(URL);
    2. Add WebtrendsMobileLib.js to the content that will be loaded in the WebView. WebtrendsMobileLib.js is delivered as part of the Webtrends Mobile Library for Android.
    3. Use the WebtrendsLib to generate events. In the embedded web content, use the JavaScript helper functions of the webtrendsLib object to generate events through the native app. A JavaScript helper function exists for each of the event helper functions exposed in the native Webtrends SDK. The JavaScript helper functions take the same arguments as the native helper functions.
      <html>
              <head>
                      <script type="text/javascript" src="js/WebtrendsMobileLib.js"></script>
                      <script type="text/javascript">
                              function sendButtonClickEvent() {
                                     
                                      var eventPath = "/HelloWorld/button/click";
                                      var eventDesc = "HelloWorld Button Click Event";
                                      var eventType = "click";
                                      var customData = {
                                              customKey : "CustomValue"
                                      };
                                      webtrendsLib.onButtonClick(eventPath, eventDesc, eventType,
                                   customData);
                              }                      
                      </script>        
              </head>
              <body>
                      <div>
                              <input type="button" onclick="sendButtonClickEvent()" value="Click Me">
                        </input>
                      </div>
              </body>
      </html>
    这时候问题来了,用了Phonegap后默认MainActivity要继承CordovaActivity,而如果要集成Webtrends,又得继承WebtrendsActivity,在Java下面不支持多继承,怎么搞呢。。。
    看来只能出大招了,把WebtrendsSDK反编译,

    看起来代码还不算多,把
    WebtrendsActivity.java里面的代码拷出来,粘贴到自己新建的的MyWebtrendsActivity.java,然后让MyWebtrendsActivity继承CordovaActivity,最后让MainActivity继承MyWebtrendsActivity,用组合的方法解决多重继承,继续一番折腾后,it works.

    另一个问题就是如果有了自己的WebClient,还需要去继承WebtrendsWebViewClient,不幸的是PhoneGap还真实现了自己的WebViewClient,即CordovaWebViewClient,又是同样的问题,去修改PhoneGap的代码不现实也不科学。咋搞呢??纠结了一番后,发现万幸的事情,在Webtrends代码中WebtrendsDataCollector.getInstance().extendsWebView(webView);像上面说的,webview是一个原生的webview,如果有自己的webview,必须继承WebtrendsWebViewClient。我试了下把CordovaWebView传进来,也可以,不用自己去实现,汗Σ( ° △ °|||)︴
    这两个问题后,其他都还好。注意在配置中修改timezone以及wt_dc_dcsid是必须的,wt_dc_dcsid需要跟Webtrends拿,估计价格不菲吧,我们是客户提供的,直接粘贴。

    SDK的下载也比较奇葩,需要发贴跟管理员申请才能链接。








  • 相关阅读:
    JAVA 实现json diff
    Apache httpclient拦截器对请求进行签名
    okHttp3教程:5种基本请求示例,拦截器实现自动重试和日志打印
    代码执行testng的几种方式
    封装log4j支持记录到testng
    修改ZuulHandlerMapping私有变量pathMatcher,重写match方法,使url匹配兼容正则表达式。
    修改testng源码,添加beforeMethod和afterMethod中的日志到test中(可以不改源码,废弃)
    Linux Python import jenkins 报错 oserror: /usr/lib/python2.7/site-packages/lookup3.so
    mongodb相关查询
    monkey命令参数介绍
  • 原文地址:https://www.cnblogs.com/yodateam/p/4206022.html
Copyright © 2020-2023  润新知