Analytics+SwiftUI.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if canImport(SwiftUI)
  15. import FirebaseAnalytics
  16. import SwiftUI
  17. /// Custom view modifier to allow for easily logging screen view events.
  18. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, *)
  19. @available(watchOS, unavailable)
  20. internal struct LoggedAnalyticsModifier: ViewModifier {
  21. /// The name of the view to log in the `AnalyticsParameterScreenName` parameter.
  22. let screenName: String
  23. /// The name of the view to log in the `AnalyticsParameterScreenClass` parameter.
  24. let screenClass: String
  25. /// Extra parameters to log with the screen view event.
  26. let extraParameters: [String: Any]
  27. func body(content: Content) -> some View {
  28. // Take the content and add an onAppear action to know when the view has appeared on screen.
  29. content.onAppear {
  30. // Log the event appearing, adding the appropriate keys and values needed for screen
  31. // view events.
  32. var parameters = extraParameters
  33. parameters[AnalyticsParameterScreenName] = screenName
  34. parameters[AnalyticsParameterScreenClass] = screenClass
  35. Analytics.logEvent(AnalyticsEventScreenView, parameters: parameters)
  36. }
  37. }
  38. }
  39. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, *)
  40. @available(watchOS, unavailable)
  41. public extension View {
  42. /// Logs `screen_view` events in Google Analytics for Firebase when this view appears on screen.
  43. /// - Parameters:
  44. /// - name: Current screen name logged with the `screen_view` event.
  45. /// - class: Current screen class or struct logged with the `screen_view` event.
  46. /// - extraParameters: Any additional parameters to be logged. These extra parameters must
  47. /// follow the same rules as described in the `Analytics.logEvent(_:parameters:)` docs.
  48. /// - Returns: A view with a custom `ViewModifier` used to log `screen_view` events when this
  49. /// view appears on screen.
  50. func analyticsScreen(name: String,
  51. class: String = "View",
  52. extraParameters: [String: Any] = [:]) -> some View {
  53. // `self` is the view, we're just adding an `LoggedAnalyticsModifier` modifier on it.
  54. modifier(LoggedAnalyticsModifier(screenName: name,
  55. screenClass: `class`,
  56. extraParameters: extraParameters))
  57. }
  58. }
  59. #endif