Skip to content

Commit 5bcc4fe

Browse files
Added more examples to Firebase Analytics Swift SDK readme (#11583)
1 parent acf4b82 commit 5bcc4fe

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

FirebaseAnalyticsSwift/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,69 @@ struct ContentView: View {
4343
}
4444
}
4545
```
46+
An example that demonstrates how the custom event logging API and manual screen view event logging API can make the code more efficient and reduce the number of lines required for event logging.
4647

48+
### Before (Without APIs)
4749

50+
```swift
51+
struct ContentView: View {
52+
var body: some View {
53+
VStack {
54+
Text("Welcome to our App!")
55+
.padding()
56+
Button("Click Me!") {
57+
// Logging a custom event when the button is clicked.
58+
Analytics.logEvent("button_clicked", parameters: nil)
59+
}
60+
}
61+
.onAppear {
62+
// Logging the screen view event when the ContentView appears.
63+
Analytics.logEvent(AnalyticsEventScreenView, parameters: [AnalyticsParameterScreenName: "main_content"])
64+
}
65+
}
66+
}
67+
```
68+
69+
### After (With APIs)
70+
71+
```swift
72+
struct ContentView: View {
73+
var body: some View {
74+
VStack {
75+
Text("Welcome to our App!")
76+
.padding()
77+
Button("Click Me!") {
78+
// Directly using Firebase's logEvent method to log the button click.
79+
Analytics.logEvent("button_clicked", parameters: nil)
80+
}
81+
}
82+
// Using the new manual screen view event logging API to log the screen view.
83+
.analyticsScreen(name: "main_content")
84+
}
85+
}
86+
87+
88+
// Introducing a manual screen view event logging API.
89+
extension View {
90+
func analyticsScreen(name: String, class screenClass: String? = nil, extraParameters: [String: Any]? = nil) -> some View {
91+
onAppear {
92+
var params: [String: Any] = [AnalyticsParameterScreenName: name]
93+
if let screenClass = screenClass {
94+
params[AnalyticsParameterScreenClass] = screenClass
95+
}
96+
if let extraParams = extraParameters {
97+
params.merge(extraParams) { _, new in new }
98+
}
99+
Analytics.logEvent(AnalyticsEventScreenView, parameters: params)
100+
}
101+
}
102+
}
103+
```
104+
105+
In this example, by leveraging the custom event logging API and manual screen view event logging API, we achieve a significant reduction in code complexity for event tracking:
48106

107+
1. **Before:** In the previous implementation, event logging for button clicks and screen views required separate blocks of code, leading to redundant lines of code throughout the
108+
app. This redundancy made the codebase less efficient and harder to maintain.
49109

110+
2. **After:** By adopting the event logging API and manual screen view event logging API, we now condense the event tracking logic into just a few lines of code. This streamlined
111+
approach improves the overall code efficiency and enhances code readability.

0 commit comments

Comments
 (0)