Skip to content

Commit c11d3a8

Browse files
add a readme for analytics swift sdk and add analytics view modifier in rc swift test app (#10266)
1 parent 55440b6 commit c11d3a8

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
lines changed

FirebaseAnalyticsSwift/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Firebase Analytics Swift SDK
2+
3+
Introduce a manual screen view event logging API that enable developers to log individual views in SwiftUI lifecycle.
4+
5+
## Code Samples
6+
7+
### Before
8+
```swift
9+
10+
struct ContentView: View {
11+
var body: some View {
12+
Text("Hello, world!")
13+
// Logging screen name with class and a custom parameter.
14+
.onAppear {
15+
Analytics.logEvent(AnalyticsEventScreenView,
16+
parameters: [AnalyticsParameterScreenName: "main_content",
17+
AnalyticsParameterScreenClass: "ContentView",
18+
"my_custom_param": 5])
19+
}
20+
21+
// OR Logging screen name only.
22+
.onAppear {
23+
Analytics.logEvent(AnalyticsEventScreenView,
24+
parameters: [AnalyticsParameterScreenName: "main_content"])
25+
}
26+
}
27+
}
28+
29+
```
30+
31+
### After
32+
```swift
33+
struct ContentView: View {
34+
var body: some View {
35+
Text("Hello, world!")
36+
// Logging screen name with class and a custom parameter.
37+
.analyticsScreen(name: "main_content",
38+
class: "ContentView",
39+
extraParameters: ["my_custom_param": 5])
40+
41+
// OR Logging screen name only, class and extra parameters are optional.
42+
.analyticsScreen(name: "main_content")
43+
}
44+
}
45+
```
46+
47+
48+
49+

FirebaseRemoteConfigSwift/Apps/SwiftUISample/Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ target 'SwiftUISample' do
99
# Comment the next line if you don't want to use dynamic frameworks
1010
use_frameworks!
1111
pod 'FirebaseAnalytics'
12+
pod 'FirebaseAnalyticsSwift'
1213
pod 'FirebaseCore', :path => '../../../'
1314
pod 'FirebaseCoreInternal', :path => '../../../'
1415
pod 'FirebaseInstallations', :path => '../../../'

FirebaseRemoteConfigSwift/Apps/SwiftUISample/SwiftUISample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
515BC88E28AD96E70032BF9B /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 515BC88D28AD96E70032BF9B /* GoogleService-Info.plist */; };
10+
513A02E928E3E73300CDF5E0 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 513A02E828E3E73300CDF5E0 /* GoogleService-Info.plist */; };
1111
51F3B180289464A5000B8772 /* SwiftUISampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51F3B17F289464A5000B8772 /* SwiftUISampleApp.swift */; };
1212
51F3B182289464A5000B8772 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51F3B181289464A5000B8772 /* ContentView.swift */; };
1313
51F3B184289464A7000B8772 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 51F3B183289464A7000B8772 /* Assets.xcassets */; };
1414
51F3B187289464A7000B8772 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 51F3B186289464A7000B8772 /* Preview Assets.xcassets */; };
1515
/* End PBXBuildFile section */
1616

1717
/* Begin PBXFileReference section */
18-
515BC88D28AD96E70032BF9B /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../../../../Downloads/GoogleService-Info.plist"; sourceTree = "<group>"; };
18+
513A02E828E3E73300CDF5E0 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../../../../Desktop/GoogleService-Info.plist"; sourceTree = "<group>"; };
1919
51F3B17C289464A5000B8772 /* SwiftUISample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUISample.app; sourceTree = BUILT_PRODUCTS_DIR; };
2020
51F3B17F289464A5000B8772 /* SwiftUISampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUISampleApp.swift; sourceTree = "<group>"; };
2121
51F3B181289464A5000B8772 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -61,7 +61,7 @@
6161
51F3B17E289464A5000B8772 /* SwiftUISample */ = {
6262
isa = PBXGroup;
6363
children = (
64-
515BC88D28AD96E70032BF9B /* GoogleService-Info.plist */,
64+
513A02E828E3E73300CDF5E0 /* GoogleService-Info.plist */,
6565
51F3B17F289464A5000B8772 /* SwiftUISampleApp.swift */,
6666
51F3B181289464A5000B8772 /* ContentView.swift */,
6767
51F3B183289464A7000B8772 /* Assets.xcassets */,
@@ -137,7 +137,7 @@
137137
buildActionMask = 2147483647;
138138
files = (
139139
51F3B187289464A7000B8772 /* Preview Assets.xcassets in Resources */,
140-
515BC88E28AD96E70032BF9B /* GoogleService-Info.plist in Resources */,
140+
513A02E928E3E73300CDF5E0 /* GoogleService-Info.plist in Resources */,
141141
51F3B184289464A7000B8772 /* Assets.xcassets in Resources */,
142142
);
143143
runOnlyForDeploymentPostprocessing = 0;

FirebaseRemoteConfigSwift/Apps/SwiftUISample/SwiftUISample/ContentView.swift

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import FirebaseRemoteConfig
1818
import FirebaseRemoteConfigSwift
19+
import FirebaseAnalyticsSwift
20+
import FirebaseAnalytics
1921
import SwiftUI
2022

2123
struct Recipe: Decodable {
@@ -25,20 +27,18 @@ struct Recipe: Decodable {
2527
}
2628

2729
struct ContentView: View {
28-
@RemoteConfigProperty(key: "Color", fallback: "blue") var colorValue: String
29-
@RemoteConfigProperty(key: "Food", fallback: nil) var foodValue: String?
30-
@RemoteConfigProperty(key: "Toggle", fallback: false) var toggleValue: Bool
30+
@RemoteConfigProperty(key: "Color", fallback: nil) var colorValue: String?
31+
@RemoteConfigProperty(key: "toggleStyleSquare", fallback: false) var toggleStyleSquare: Bool
3132
@RemoteConfigProperty(key: "fruits", fallback: []) var fruits: [String]
3233
@RemoteConfigProperty(key: "counter", fallback: 1) var counter: Int
3334
@RemoteConfigProperty(key: "mobileweek", fallback: ["section 0": "breakfast"]) var sessions:
3435
[String: String]
3536
@RemoteConfigProperty(
3637
key: "recipe", fallback: Recipe(recipeName: "banana bread", cookTime: 40, notes: "yum!")
3738
)
38-
var recipe: Recipe
3939

40-
@State private var realtimeSwitch = false
41-
var realtimeToggle: Bool
40+
var recipe: Recipe
41+
@State var isChecked = false
4242

4343
var body: some View {
4444
VStack {
@@ -47,7 +47,16 @@ struct ContentView: View {
4747
}
4848

4949
List(fruits, id: \.self) { fruit in
50-
Text(fruit)
50+
HStack {
51+
Button(action: toggle) {
52+
if toggleStyleSquare {
53+
Image(systemName: isChecked ? "checkmark.square.fill" : "square")
54+
} else {
55+
Image(systemName: isChecked ? "checkmark.circle.fill" : "circle")
56+
}
57+
}
58+
Text(fruit)
59+
}
5160
}
5261
List {
5362
ForEach(sessions.sorted(by: >), id: \.key) { key, value in
@@ -60,27 +69,28 @@ struct ContentView: View {
6069
Text(recipe.recipeName)
6170
Text(recipe.notes)
6271
Text("cook time: \(recipe.cookTime)")
72+
.analyticsScreen(name: "recipe")
6373
}
64-
ForEach(0 ... counter, id: \.self) { i in
65-
Text(colorValue)
74+
// Test non exist key
75+
if colorValue != nil {
76+
Text(colorValue!)
6677
.padding()
67-
.foregroundStyle(toggleValue ? .primary : .secondary)
68-
if foodValue != nil {
69-
Text(foodValue!)
70-
.padding()
71-
.foregroundStyle(toggleValue ? .primary : .secondary)
72-
}
7378
}
7479
}
7580
}
7681

82+
func toggle() {
83+
isChecked.toggle()
84+
}
85+
7786
func fetchAndActivate() {
7887
RemoteConfig.remoteConfig().fetchAndActivate()
88+
Analytics.logEvent("activate", parameters: [:])
7989
}
8090
}
8191

8292
struct ContentView_Previews: PreviewProvider {
8393
static var previews: some View {
84-
ContentView(realtimeToggle: false)
94+
ContentView()
8595
}
8696
}

FirebaseRemoteConfigSwift/Apps/SwiftUISample/SwiftUISample/SwiftUISampleApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct SwiftUISampleApp: App {
4141

4242
var body: some Scene {
4343
WindowGroup {
44-
ContentView(realtimeToggle: false)
44+
ContentView().analyticsScreen(name: "ContentView")
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)