Analytics+SwiftUI.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, *)
  19. @available(tvOS, unavailable)
  20. @available(macOS, unavailable)
  21. @available(macCatalyst, unavailable)
  22. @available(watchOS, unavailable)
  23. internal struct LoggedAnalyticsModifier: ViewModifier {
  24. /// The name of the view to log in the `AnalyticsParameterScreenName` parameter.
  25. let screenName: String
  26. /// The name of the view to log in the `AnalyticsParameterScreenClass` parameter.
  27. let screenClass: String
  28. /// Extra parameters to log with the screen view event.
  29. let extraParameters: [String: Any]
  30. func body(content: Content) -> some View {
  31. // Take the content and add an onAppear action to know when the view has appeared on screen.
  32. content.onAppear {
  33. // Log the event appearing, adding the appropriate keys and values needed for screen
  34. // view events.
  35. var parameters = extraParameters
  36. parameters[AnalyticsParameterScreenName] = screenName
  37. parameters[AnalyticsParameterScreenClass] = screenClass
  38. Analytics.logEvent(AnalyticsEventScreenView, parameters: parameters)
  39. }
  40. }
  41. }
  42. @available(iOS 13, *)
  43. @available(tvOS, unavailable)
  44. @available(macOS, unavailable)
  45. @available(macCatalyst, unavailable)
  46. @available(watchOS, unavailable)
  47. public extension View {
  48. /// Logs `screen_view` events in Google Analytics for Firebase when this view appears on screen.
  49. /// - Parameters:
  50. /// - name: Current screen name logged with the `screen_view` event.
  51. /// - class: Current screen class or struct logged with the `screen_view` event.
  52. /// - extraParameters: Any additional parameters to be logged. These extra parameters must
  53. /// follow the same rules as described in the `Analytics.logEvent(_:parameters:)` docs.
  54. /// - Returns: A view with a custom `ViewModifier` used to log `screen_view` events when this
  55. /// view appears on screen.
  56. func analyticsScreen(name: String,
  57. class: String = "View",
  58. extraParameters: [String: Any] = [:]) -> some View {
  59. // `self` is the view, we're just adding an `LoggedAnalyticsModifier` modifier on it.
  60. modifier(LoggedAnalyticsModifier(screenName: name,
  61. screenClass: `class`,
  62. extraParameters: extraParameters))
  63. }
  64. }
  65. #endif