[null,null,["最后更新时间 (UTC):2025-08-13。"],[],[],null,["iOS+ Android \n\n\u003cbr /\u003e\n\nCalls to log events or set user properties fired from within a WebView must be forwarded to native code before they can be sent to Google Analytics.\n\nImplement JavaScript handler The first step in using Google Analytics in a WebView is to create JavaScript functions to forward events and user properties to native code. The following example shows how to do this in a way that is compatible with both Android and Apple native code: \n\n```gdscript\nfunction logEvent(name, params) {\n if (!name) {\n return;\n }\n\n if (window.AnalyticsWebInterface) {\n // Call Android interface\n window.AnalyticsWebInterface.logEvent(name, JSON.stringify(params));\n } else if (window.webkit\n && window.webkit.messageHandlers\n && window.webkit.messageHandlers.firebase) {\n // Call iOS interface\n var message = {\n command: 'logEvent',\n name: name,\n parameters: params\n };\n window.webkit.messageHandlers.firebase.postMessage(message);\n } else {\n // No Android or iOS interface found\n console.log(\"No native APIs found.\");\n }\n}\n\nfunction setUserProperty(name, value) {\n if (!name || !value) {\n return;\n }\n\n if (window.AnalyticsWebInterface) {\n // Call Android interface\n window.AnalyticsWebInterface.setUserProperty(name, value);\n } else if (window.webkit\n && window.webkit.messageHandlers\n && window.webkit.messageHandlers.firebase) {\n // Call iOS interface\n var message = {\n command: 'setUserProperty',\n name: name,\n value: value\n };\n window.webkit.messageHandlers.firebase.postMessage(message);\n } else {\n // No Android or iOS interface found\n console.log(\"No native APIs found.\");\n }\n}https://github.com/FirebaseExtended/analytics-webview/blob/322f8aeccbf5e2f2aac96d5c3083c8b5183f53cb/web/public/index.js#L44-L66\n```\n\nCall the JavaScript handler from your WebView You can properly log events and set user properties from within a WebView by calling the JavaScript functions that you defined in the previous step. The following example shows how to properly log a purchase event and set a user property as an example: \n\n```gdscript\nfunction logEventExample() {\n \n // Log an event named \"purchase\" with parameters\n logEvent(\"purchase\", {\n content_type: \"product\",\n value: 123,\n currency: \"USD\",\n quantity: 2,\n items: [{\n item_id: \"sample-item-id\",\n item_variant: \"232323\"\n }],\n transaction_id: \"1234567\"\n });\n}\n\nfunction logUserPropertyExample() {\n // Set a user property named 'favorite_genre'\n setUserProperty(\"favorite_genre\", \"comedy\") \n}\n```\n\nImplement native interface\n\nTo invoke native Apple code from JavaScript, create a message handler class\nconforming to the `WKScriptMessageHandler` protocol. You can make\nGoogle Analytics calls inside the\n[`userContentController:didReceiveScriptMessage:`](https://developer.apple.com/reference/webkit/wkscriptmessagehandler/1396222-usercontentcontroller?language=objc)\ncallback: \n\nSwift\n\n\n**Note:** This Firebase product is not available on the macOS target. \n\n```swift\nfunc userContentController(_ userContentController: WKUserContentController,\n didReceive message: WKScriptMessage) {\n guard let body = message.body as? [String: Any] else { return }\n guard let command = body[\"command\"] as? String else { return }\n guard let name = body[\"name\"] as? String else { return }\n\n if command == \"setUserProperty\" {\n guard let value = body[\"value\"] as? String else { return }\n Analytics.setUserProperty(value, forName: name)\n } else if command == \"logEvent\" {\n guard let params = body[\"parameters\"] as? [String: NSObject] else { return }\n Analytics.logEvent(name, parameters: params)\n }\n}https://github.com/FirebaseExtended/analytics-webview/blob/322f8aeccbf5e2f2aac96d5c3083c8b5183f53cb/ios/swift/FirebaseAnalyticsWeb/ViewController.swift#L57-L70\n```\n\nObjective-C \n\n```objective-c\n- (void)userContentController:(WKUserContentController *)userContentController\n didReceiveScriptMessage:(WKScriptMessage *)message {\n if ([message.body[@\"command\"] isEqual:@\"setUserProperty\"]) {\n [FIRAnalytics setUserPropertyString:message.body[@\"value\"] forName:message.body[@\"name\"]];\n } else if ([message.body[@\"command\"] isEqual: @\"logEvent\"]) {\n [FIRAnalytics logEventWithName:message.body[@\"name\"] parameters:message.body[@\"parameters\"]];\n }\n}https://github.com/FirebaseExtended/analytics-webview/blob/322f8aeccbf5e2f2aac96d5c3083c8b5183f53cb/ios/objc/FirebaseAnalyticsWeb/ViewController.m#L59-L66\n```\n\nFinally, add the message handler to the webview's user content controller: \n\nSwift\n\n\n**Note:** This Firebase product is not available on the macOS target. \n\n```swift\nself.webView.configuration.userContentController.add(self, name: \"firebase\")https://github.com/FirebaseExtended/analytics-webview/blob/322f8aeccbf5e2f2aac96d5c3083c8b5183f53cb/ios/swift/FirebaseAnalyticsWeb/ViewController.swift#L42-L42\n```\n\nObjective-C\n\n\n**Note:** This Firebase product is not available on the macOS target. \n\n```objective-c\n[self.webView.configuration.userContentController addScriptMessageHandler:self\n name:@\"firebase\"];https://github.com/FirebaseExtended/analytics-webview/blob/322f8aeccbf5e2f2aac96d5c3083c8b5183f53cb/ios/objc/FirebaseAnalyticsWeb/ViewController.m#L44-L45\n```\n\nNext Steps\n\nFor a fully functional implementation of Google Analytics in a WebView,\nsee the [analytics-webview](https://github.com/firebase/analytics-webview)\nsample."]]