- Print
- DarkLight
- PDF
Generating events through the native application
WebViews embedded in a native app can generate Webtrends events using JavaScript helper methods that interact with the native app to generate and send events, or using the the Webtrends JavaScript tag in the embedded web content to generate and send events. Follow these instructions to use JavaScript helper methods.
The native app must implement the Webtrends Mobile Library for Android, and contain embedded web content in a WebView.
For more information, see the "Webtrends JavaScript Tag" documentation.
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 theWTDataCollector
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); wtdc.enableWtInWebView(wv); wv.loadUrl(URL);
If your application uses its own custom WebViewClient, you should not use the
enableWtInWebView
helper method. Instead, ensure that the WTWebViewClient also gets invoked by following these steps:- Modify your custom WebViewClient to extend WTWebViewClient instead of WebViewClient.
- If you are overriding
onLoadResource
, callsuper.onLoadResource
. - Set your custom WebViewClient on your WebView.
import android.webkit.WebView; import com.webtrends.mobile.analytics.android.webViewExtension.WTWebViewClient; 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);
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.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>
Generating events through the Webtrends JavaScript Tag
WebViews embedded in a native app can generate Webtrends events by using JavaScript helper methods that interact with the native app to generate and send events, or by using the Webtrends JavaScript tag in the embedded web content to generate and send events. Follow these instructions to use the Webtrends JavaScript tag.
The native app must implement the Webtrends Mobile Library for Android, the web content must be tagged with the Webtrends JavaScript tag, and you must be familiar with Android WebViews.
Due to restrictions enforced by some versions of Android, the Webtrends JavaScript tag is unable to read and write cookies if you are loading local content in the WebView using the file:/// protocol. For this reason, generating events through the JavaScript tag is only supported if the WebView content is hosted externally to the app and accessed over HTTP. If you are using content bundled with the app, we recommend that you use JavaScript helper methods, which interact with the native app to generate and send events. See: Generating events through the native application.
For more information, see the "Webtrends JavaScript Tag" documentation and Android WebView page on the Android Developer Network.
Enable JavaScript in the WebView.
Append the WT session parameters to the URL of the embedded web content using the provided helper method. These parameters are parsed by the JavaScript plug-in in the embedded content and used to configure its user ID and session configuration.
String URL = "file:///android_asset/WebContent/webViewContent.html"; +URL = WTDataCollector.getInstance().appendSessionParamsToURL(URL); WebView wv = (WebView)findViewById(R.id.webView1); wv.getSettings().setJavaScriptEnabled(true); wv.loadUrl(URL);
Add the
webtrends.id_receiver.js
plugin to the JavaScript tag configuration on the embedded content.window.webtrendsAsyncInit = function () { var dcs = new Webtrends.dcs().init({ dcsid: "<your_dcsid>", timezone: "<your_timezone>", plugins: { id_receiver: { src: "//s.webtrends.com/js/webtrends.id_receiver.js" } } }); dcs.track(); }
Generate events using track and multiTrack as you would on a standard web page tagged with the Webtrends JavaScript tag.