瀏覽代碼

add a readme for analytics swift sdk and add analytics view modifier in rc swift test app (#10266)

Charlotte Liang 3 年之前
父節點
當前提交
c11d3a856d

+ 49 - 0
FirebaseAnalyticsSwift/README.md

@@ -0,0 +1,49 @@
+# Firebase Analytics Swift SDK
+
+Introduce a manual screen view event logging API that enable developers to log individual views in SwiftUI lifecycle.
+
+## Code Samples
+
+### Before
+```swift
+
+struct ContentView: View {
+  var body: some View {
+    Text("Hello, world!")
+      // Logging screen name with class and a custom parameter.
+      .onAppear {
+        Analytics.logEvent(AnalyticsEventScreenView,
+                           parameters: [AnalyticsParameterScreenName: "main_content",
+                                        AnalyticsParameterScreenClass: "ContentView",
+                                        "my_custom_param": 5])
+      }
+
+       // OR Logging screen name only.
+      .onAppear {
+        Analytics.logEvent(AnalyticsEventScreenView,
+                           parameters: [AnalyticsParameterScreenName: "main_content"])
+      }
+  }
+}
+
+```
+
+### After
+```swift
+struct ContentView: View {
+  var body: some View {
+    Text("Hello, world!")
+       // Logging screen name with class and a custom parameter.
+      .analyticsScreen(name: "main_content",
+                       class: "ContentView",
+                       extraParameters: ["my_custom_param": 5])
+
+      // OR Logging screen name only, class and extra parameters are optional.
+      .analyticsScreen(name: "main_content")
+  }
+}
+```
+
+
+
+

+ 1 - 0
FirebaseRemoteConfigSwift/Apps/SwiftUISample/Podfile

@@ -9,6 +9,7 @@ target 'SwiftUISample' do
   # Comment the next line if you don't want to use dynamic frameworks
   use_frameworks!
   pod 'FirebaseAnalytics'
+  pod 'FirebaseAnalyticsSwift'
   pod 'FirebaseCore', :path => '../../../'
   pod 'FirebaseCoreInternal', :path => '../../../'
   pod 'FirebaseInstallations', :path => '../../../'

+ 4 - 4
FirebaseRemoteConfigSwift/Apps/SwiftUISample/SwiftUISample.xcodeproj/project.pbxproj

@@ -7,7 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		515BC88E28AD96E70032BF9B /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 515BC88D28AD96E70032BF9B /* GoogleService-Info.plist */; };
+		513A02E928E3E73300CDF5E0 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 513A02E828E3E73300CDF5E0 /* GoogleService-Info.plist */; };
 		51F3B180289464A5000B8772 /* SwiftUISampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51F3B17F289464A5000B8772 /* SwiftUISampleApp.swift */; };
 		51F3B182289464A5000B8772 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51F3B181289464A5000B8772 /* ContentView.swift */; };
 		51F3B184289464A7000B8772 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 51F3B183289464A7000B8772 /* Assets.xcassets */; };
@@ -15,7 +15,7 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
-		515BC88D28AD96E70032BF9B /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../../../../Downloads/GoogleService-Info.plist"; sourceTree = "<group>"; };
+		513A02E828E3E73300CDF5E0 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../../../../Desktop/GoogleService-Info.plist"; sourceTree = "<group>"; };
 		51F3B17C289464A5000B8772 /* SwiftUISample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUISample.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		51F3B17F289464A5000B8772 /* SwiftUISampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUISampleApp.swift; sourceTree = "<group>"; };
 		51F3B181289464A5000B8772 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -61,7 +61,7 @@
 		51F3B17E289464A5000B8772 /* SwiftUISample */ = {
 			isa = PBXGroup;
 			children = (
-				515BC88D28AD96E70032BF9B /* GoogleService-Info.plist */,
+				513A02E828E3E73300CDF5E0 /* GoogleService-Info.plist */,
 				51F3B17F289464A5000B8772 /* SwiftUISampleApp.swift */,
 				51F3B181289464A5000B8772 /* ContentView.swift */,
 				51F3B183289464A7000B8772 /* Assets.xcassets */,
@@ -137,7 +137,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				51F3B187289464A7000B8772 /* Preview Assets.xcassets in Resources */,
-				515BC88E28AD96E70032BF9B /* GoogleService-Info.plist in Resources */,
+				513A02E928E3E73300CDF5E0 /* GoogleService-Info.plist in Resources */,
 				51F3B184289464A7000B8772 /* Assets.xcassets in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 26 - 16
FirebaseRemoteConfigSwift/Apps/SwiftUISample/SwiftUISample/ContentView.swift

@@ -16,6 +16,8 @@
 
 import FirebaseRemoteConfig
 import FirebaseRemoteConfigSwift
+import FirebaseAnalyticsSwift
+import FirebaseAnalytics
 import SwiftUI
 
 struct Recipe: Decodable {
@@ -25,9 +27,8 @@ struct Recipe: Decodable {
 }
 
 struct ContentView: View {
-  @RemoteConfigProperty(key: "Color", fallback: "blue") var colorValue: String
-  @RemoteConfigProperty(key: "Food", fallback: nil) var foodValue: String?
-  @RemoteConfigProperty(key: "Toggle", fallback: false) var toggleValue: Bool
+  @RemoteConfigProperty(key: "Color", fallback: nil) var colorValue: String?
+  @RemoteConfigProperty(key: "toggleStyleSquare", fallback: false) var toggleStyleSquare: Bool
   @RemoteConfigProperty(key: "fruits", fallback: []) var fruits: [String]
   @RemoteConfigProperty(key: "counter", fallback: 1) var counter: Int
   @RemoteConfigProperty(key: "mobileweek", fallback: ["section 0": "breakfast"]) var sessions:
@@ -35,10 +36,9 @@ struct ContentView: View {
   @RemoteConfigProperty(
     key: "recipe", fallback: Recipe(recipeName: "banana bread", cookTime: 40, notes: "yum!")
   )
-  var recipe: Recipe
 
-  @State private var realtimeSwitch = false
-  var realtimeToggle: Bool
+  var recipe: Recipe
+  @State var isChecked = false
 
   var body: some View {
     VStack {
@@ -47,7 +47,16 @@ struct ContentView: View {
       }
 
       List(fruits, id: \.self) { fruit in
-        Text(fruit)
+        HStack {
+          Button(action: toggle) {
+            if toggleStyleSquare {
+              Image(systemName: isChecked ? "checkmark.square.fill" : "square")
+            } else {
+              Image(systemName: isChecked ? "checkmark.circle.fill" : "circle")
+            }
+          }
+          Text(fruit)
+        }
       }
       List {
         ForEach(sessions.sorted(by: >), id: \.key) { key, value in
@@ -60,27 +69,28 @@ struct ContentView: View {
         Text(recipe.recipeName)
         Text(recipe.notes)
         Text("cook time: \(recipe.cookTime)")
+          .analyticsScreen(name: "recipe")
       }
-      ForEach(0 ... counter, id: \.self) { i in
-        Text(colorValue)
+      // Test non exist key
+      if colorValue != nil {
+        Text(colorValue!)
           .padding()
-          .foregroundStyle(toggleValue ? .primary : .secondary)
-        if foodValue != nil {
-          Text(foodValue!)
-            .padding()
-            .foregroundStyle(toggleValue ? .primary : .secondary)
-        }
       }
     }
   }
 
+  func toggle() {
+    isChecked.toggle()
+  }
+
   func fetchAndActivate() {
     RemoteConfig.remoteConfig().fetchAndActivate()
+    Analytics.logEvent("activate", parameters: [:])
   }
 }
 
 struct ContentView_Previews: PreviewProvider {
   static var previews: some View {
-    ContentView(realtimeToggle: false)
+    ContentView()
   }
 }

+ 1 - 1
FirebaseRemoteConfigSwift/Apps/SwiftUISample/SwiftUISample/SwiftUISampleApp.swift

@@ -41,7 +41,7 @@ struct SwiftUISampleApp: App {
 
   var body: some Scene {
     WindowGroup {
-      ContentView(realtimeToggle: false)
+      ContentView().analyticsScreen(name: "ContentView")
     }
   }
 }