Implementa il gestore JavaScript
Il primo passaggio per utilizzare Google Analytics in una WebView consiste nel creare funzioni JavaScript per inoltrare eventi e proprietà utente al codice nativo. L'esempio seguente mostra come eseguire questa operazione in modo compatibile con il codice nativo sia di Android sia di Apple:function logEvent(name, params) { if (!name) { return; } if (window.AnalyticsWebInterface) { // Call Android interface window.AnalyticsWebInterface.logEvent(name, JSON.stringify(params)); } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.firebase) { // Call iOS interface var message = { command: 'logEvent', name: name, parameters: params }; window.webkit.messageHandlers.firebase.postMessage(message); } else { // No Android or iOS interface found console.log("No native APIs found."); } } function setUserProperty(name, value) { if (!name || !value) { return; } if (window.AnalyticsWebInterface) { // Call Android interface window.AnalyticsWebInterface.setUserProperty(name, value); } else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.firebase) { // Call iOS interface var message = { command: 'setUserProperty', name: name, value: value }; window.webkit.messageHandlers.firebase.postMessage(message); } else { // No Android or iOS interface found console.log("No native APIs found."); } }
Chiama il gestore JavaScript da WebView
Puoi registrare correttamente gli eventi e impostare le proprietà utente da un WebView chiamando le funzioni JavaScript definite nel passaggio precedente. L'esempio seguente mostra come registrare correttamente un evento di acquisto e impostare una proprietà utente come esempio:function logEventExample() { // Log an event named "purchase" with parameters logEvent("purchase", { content_type: "product", value: 123, currency: "USD", quantity: 2, items: [{ item_id: "sample-item-id", item_variant: "232323" }], transaction_id: "1234567" }); } function logUserPropertyExample() { // Set a user property named 'favorite_genre' setUserProperty("favorite_genre", "comedy") }
Implementare l'interfaccia nativa
Per richiamare il codice Android nativo da JavaScript, implementa una classe con metodi contrassegnati da @JavaScriptInterface
:
public class AnalyticsWebInterface { public static final String TAG = "AnalyticsWebInterface"; private FirebaseAnalytics mAnalytics; public AnalyticsWebInterface(Context context) { mAnalytics = FirebaseAnalytics.getInstance(context); } @JavascriptInterface public void logEvent(String name, String jsonParams) { LOGD("logEvent:" + name); mAnalytics.logEvent(name, bundleFromJson(jsonParams)); } @JavascriptInterface public void setUserProperty(String name, String value) { LOGD("setUserProperty:" + name); mAnalytics.setUserProperty(name, value); } private void LOGD(String message) { // Only log on debug builds, for privacy if (BuildConfig.DEBUG) { Log.d(TAG, message); } } private Bundle bundleFromJson(String json) { // ... } }
Dopo aver creato l'interfaccia nativa, registrala con il tuo componente WebView in modo che sia visibile al codice JavaScript in esecuzione nel componente WebView:
// Only add the JavaScriptInterface on API version JELLY_BEAN_MR1 and above, due to // security concerns, see link below for more information: // https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object,%20java.lang.String) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { mWebView.addJavascriptInterface( new AnalyticsWebInterface(this), AnalyticsWebInterface.TAG); } else { Log.w(TAG, "Not adding JavaScriptInterface, API Version: " + Build.VERSION.SDK_INT); }
Passaggi successivi
Per un'implementazione completamente funzionale di Google Analytics in un WebView, consulta l'esempio analytics-webview.