자바스크립트 핸들러 구현
WebView에서 Google Analytics를 사용하려면 먼저 이벤트 및 사용자 속성을 네이티브 코드로 전달하는 자바스크립트 함수를 만들어야 합니다. 다음 예시에서는 Android 및 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."); } }
WebView에서 JavaScript 핸들러 호출
이전 단계에서 정의한 JavaScript 함수를 호출하여 WebView 내에서 이벤트를 올바르게 기록하고 사용자 속성을 설정할 수 있습니다. 다음 예시에서는 구매 이벤트를 올바르게 기록하고 사용자 속성을 설정하는 방법을 예로 보여줍니다.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") }
네이티브 인터페이스 구현
자바스크립트에서 네이티브 Android 코드를 호출하려면 @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) { // ... } }
네이티브 인터페이스를 만든 후 WebView에 등록하여 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); }
다음 단계
WebView에서 완벽하게 작동하는 Google Analytics 구현은 analytics-webview 샘플을 참고하세요.