Sử dụng Analytics trong WebView

Các lệnh gọi để ghi nhật ký sự kiện hoặc đặt thuộc tính người dùng được kích hoạt từ bên trong WebView phải được chuyển tiếp tới mã gốc trước khi chúng có thể được gửi tới Google Analytics.

Triển khai trình xử lý JavaScript

Bước đầu tiên khi sử dụng Google Analytics trong WebView là tạo các hàm JavaScript để chuyển tiếp các sự kiện và thuộc tính người dùng sang mã gốc. Ví dụ sau đây cho thấy cách thực hiện việc này theo cách tương thích với cả mã gốc của Android và 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.");
  }
}

Triển khai giao diện gốc

Để gọi mã gốc của Apple từ JavaScript, hãy tạo một lớp xử lý tin nhắn tuân theo giao thức WKScriptMessageHandler . Bạn có thể thực hiện lệnh gọi Google Analytics bên trong lệnh gọi lại userContentController:didReceiveScriptMessage: ::

Nhanh

Lưu ý: Sản phẩm Firebase này không có sẵn trên mục tiêu macOS.
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)
  }
}

Mục tiêu-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"]];
  }
}

Cuối cùng, thêm trình xử lý tin nhắn vào bộ điều khiển nội dung người dùng của chế độ xem web:

Nhanh

Lưu ý: Sản phẩm Firebase này không có sẵn trên mục tiêu macOS.
self.webView.configuration.userContentController.add(self, name: "firebase")

Mục tiêu-C

Lưu ý: Sản phẩm Firebase này không có sẵn trên mục tiêu macOS.
[self.webView.configuration.userContentController addScriptMessageHandler:self
                                                                     name:@"firebase"];

Bước tiếp theo

Để triển khai đầy đủ chức năng của Google Analytics trong WebView, hãy xem mẫu lượt xem web phân tích .