ใช้ตัวแฮนเดิล JavaScript
ขั้นตอนแรกในการใช้ Google Analytics ใน WebView คือการสร้าง ฟังก์ชัน JavaScript เพื่อส่งต่อเหตุการณ์และพร็อพเพอร์ตี้ผู้ใช้ไปยังโค้ดเนทีฟ ตัวอย่างต่อไปนี้แสดงวิธีดำเนินการนี้ในลักษณะที่เข้ากันได้ กับโค้ดเนทีฟของทั้ง Android และ Applefunction 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."); } }
เรียกตัวแฮนเดิล JavaScript จาก WebView
คุณสามารถบันทึกเหตุการณ์อย่างถูกต้องและตั้งค่าพร็อพเพอร์ตี้ผู้ใช้จากภายใน WebView ได้โดยการเรียกใช้ฟังก์ชัน JavaScript ที่คุณกำหนดไว้ในขั้นตอนก่อนหน้า ตัวอย่างต่อไปนี้แสดงวิธีบันทึกเหตุการณ์การซื้ออย่างถูกต้องและตั้งค่าพร็อพเพอร์ตี้ผู้ใช้เป็นตัวอย่าง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") }
ติดตั้งใช้งานอินเทอร์เฟซเนทีฟ
คุณสามารถใช้ส่วนติดต่อเนทีฟสำหรับ iOS หรือ Android ได้
iOS
หากต้องการเรียกใช้โค้ดของ Apple ดั้งเดิมจาก JavaScript ให้สร้างคลาสตัวแฮนเดิลข้อความ
ที่สอดคล้องกับโปรโตคอล WKScriptMessageHandler คุณสามารถโทรหา
Google Analytics ภายใน
userContentController:didReceiveScriptMessage:
การเรียกกลับได้
Swift
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { guard let body = message.body as? [String: Any] else { return } guard let command = body["command"] as? String else { return } guard let name = body["name"] as? String else { return } if command == "setUserProperty" { guard let value = body["value"] as? String else { return } Analytics.setUserProperty(value, forName: name) } else if command == "logEvent" { guard let params = body["parameters"] as? [String: NSObject] else { return } Analytics.logEvent(name, parameters: params) } }
Objective-C
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { if ([message.body[@"command"] isEqual:@"setUserProperty"]) { [FIRAnalytics setUserPropertyString:message.body[@"value"] forName:message.body[@"name"]]; } else if ([message.body[@"command"] isEqual: @"logEvent"]) { [FIRAnalytics logEventWithName:message.body[@"name"] parameters:message.body[@"parameters"]]; } }
สุดท้าย ให้เพิ่มตัวแฮนเดิลข้อความลงในเครื่องควบคุมเนื้อหาของผู้ใช้ของ WebView ดังนี้
Swift
self.webView.configuration.userContentController.add(self, name: "firebase")
Objective-C
[self.webView.configuration.userContentController addScriptMessageHandler:self name:@"firebase"];
Android
หากต้องการเรียกใช้โค้ด Android ดั้งเดิมจาก JavaScript ให้ใช้คลาสที่มี
เมธอดที่ทำเครื่องหมาย @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 เพื่อให้โค้ด JavaScript ที่ทำงานใน 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 บน iOS
คุณบันทึกเหตุการณ์ IAP ใน WebView ด้วยตนเองได้โดยใช้ Analytics SDK v12.5.0 ขึ้นไป
function logManualPurchaseEvent() {
// For manually tracking in-app purchases within a WebView, log the in-app purchase event:
logEvent("in_app_purchase", {
currency: "USD",
price: 0.99,
product_id: "prod_123",
product_name: "Product 123",
quantity: 1,
value: 0.99,
});
}
โปรดทราบว่า SDK จะยังคงบันทึกการซื้อในแอปโดยอัตโนมัติต่อไปเมื่อเป็นไปได้ และจะไม่ขจัดเหตุการณ์ in_app_purchase ที่บันทึกด้วยตนเองที่ซ้ำกัน
ขั้นตอนถัดไป
ดูการติดตั้งใช้งาน Google Analytics ใน WebView ที่ใช้งานได้อย่างสมบูรณ์ได้ในตัวอย่าง analytics-webview