Skip to content

Commit 36c356a

Browse files
bsneedBrandon Sneed
andauthored
Refactor screen fun param from screenTitle to title. (#56)
* Refactor screen func param from screenTitle to title * Update README. Co-authored-by: Brandon Sneed <[email protected]>
1 parent 3eeccfc commit 36c356a

File tree

12 files changed

+53
-55
lines changed

12 files changed

+53
-55
lines changed

Examples/apps/BasicExample/BasicExample/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ViewController: UIViewController {
4141

4242
@IBAction func screenTapped(_ sender: Any) {
4343
let props = ScreenProperties(appUsage: usage)
44-
analytics?.screen(screenTitle: "Main Screen", category: "Best", properties: props)
44+
analytics?.screen(title: "Main Screen", category: "Best", properties: props)
4545
}
4646

4747
@IBAction func identifyTapped(_ sender: Any) {

Examples/apps/DestinationsExample/DestinationsExample/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class ViewController: UIViewController {
9797

9898
func screenEvent() {
9999
guard let eventFieldText = eventField?.text else { return }
100-
analytics?.screen(screenTitle: eventFieldText, properties: valuesEntered())
100+
analytics?.screen(title: eventFieldText, properties: valuesEntered())
101101
}
102102

103103
func groupEvent() {

Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct ContentView: View {
1818
Text("Track")
1919
}).padding(6)
2020
Button(action: {
21-
Analytics.main.screen(screenTitle: "Screen appeared")
21+
Analytics.main.screen(title: "Screen appeared")
2222
}, label: {
2323
Text("Screen")
2424
}).padding(6)

Examples/apps/watchOSExample/watchOSExample WatchKit Extension/InterfaceController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class InterfaceController: WKInterfaceController {
2121

2222
override func willActivate() {
2323
// This method is called when watch view controller is about to be visible to user
24-
analytics?.screen(screenTitle: "Main Screen")
24+
analytics?.screen(title: "Main Screen")
2525
}
2626

2727
override func didDeactivate() {

Examples/apps/watchOSExample/watchOSExample WatchKit Extension/SomeScreenController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SomeScreenController: WKInterfaceController {
1717

1818
override func willActivate() {
1919
// This method is called when watch view controller is about to be visible to user
20-
analytics?.screen(screenTitle: "Some Screen Controller")
20+
analytics?.screen(title: "Some Screen Controller")
2121
}
2222

2323
override func didDeactivate() {

Examples/other_plugins/UIKitScreenTracking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class UIKitScreenTracking: UtilityPlugin {
7777
controller.seg__trackScreen(name: name)
7878
} else if let name = name {
7979
// if we have a name, call screen
80-
self.analytics?.screen(screenTitle: name)
80+
self.analytics?.screen(title: name)
8181
}
8282
}
8383
}

Examples/tasks/CustomScreenTracking.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ import Segment
3636
class QueryAlertController: UIAlertController {
3737
// we already conform to UIKitScreenTrackable so override
3838
override func seg__trackScreen(name: String?) {
39-
Analytics.main.screen(screenTitle: "Query", category: "Action Sheet")
39+
Analytics.main.screen(title: "Query", category: "Action Sheet")
4040
}
4141
}
4242

4343
extension UIAlertController: UIKitScreenTrackable {
4444
func seg__trackScreen(name: String?) {
45-
Analytics.main.screen(screenTitle: "Basic Alert", category: "Alert")
45+
Analytics.main.screen(title: "Basic Alert", category: "Alert")
4646
}
4747
}

README.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ The Analytics client will typically be set up at application launch time, such a
4343
Typically the following call may be all that's required.
4444

4545
```swift
46-
Analytics(configuration: Configuration("SEGMENT_API_KEY"))
46+
Analytics(configuration: Configuration("<YOUR_WRITE_KEY>"))
4747
```
4848

4949
### Configuration Options
5050
When creating a new client, you can configure it in various ways. Some examples are listed below.
5151

5252
```swift
53-
let config = Configuration(writeKey: "8XpdAWa7qJVBJMK8V4FfXQOrnvCzu3Ie")
53+
let config = Configuration(writeKey: "<YOUR_WRITE_KEY>")
5454
.flushAt(3)
5555
.trackApplicationLifecycleEvents(true)
5656
.flushInterval(10)
@@ -126,20 +126,20 @@ The screen call lets you record whenever a user sees a screen in your mobile app
126126

127127
Method signatures:
128128
```swift
129-
func screen(screenTitle: String, category: String? = nil)
130-
func screen<P: Codable>(screenTitle: String, category: String? = nil, properties: P?)
129+
func screen(title: String, category: String? = nil)
130+
func screen<P: Codable>(title: String, category: String? = nil, properties: P?)
131131
```
132132

133133
Example Usage:
134134
```swift
135-
analytics.screen(screenTitle: "SomeScreen")
135+
analytics.screen(title: "SomeScreen")
136136
```
137137

138138
You can enable automatic screen tracking by using the [example plugin](https://github.com/segmentio/analytics-example-plugins/blob/main/plugins/swift/UIKitScreenTracking.swift).
139139

140140
Once the plugin has been added to your project add it to your Analytics instance:
141141
```swift
142-
analytics.add(plugin: UIKitScreenTracking(name: "ScreenTracking", analytics: analytics))
142+
analytics.add(plugin: UIKitScreenTracking()
143143
```
144144

145145
### group
@@ -167,46 +167,48 @@ analytics.group("user-123", MyTraits(
167167
```
168168

169169
### add
170-
add API allows you to add a plugin to the analytics timeline
170+
Add API allows you to add a plugin to the analytics timeline. It will return the plugin instance in
171+
case you wish to store it for access later.
171172

172173
Method signature:
173174
```swift
174-
@discardableResult func add(plugin: Plugin) -> String
175+
@discardableResult func add(plugin: Plugin) -> Plugin
175176
```
176177

177178
Example Usage:
178179
```swift
179-
analytics.add(plugin: UIKitScreenTracking(name: "ScreenTracking"))
180+
analytics.add(plugin: UIKitScreenTracking())
180181
```
181182

182183
### find
183-
find a registered plugin from the analytics timeline
184+
Find a registered plugin from the analytics timeline. It will return the first plugin
185+
of the specified type.
184186

185187
Method signature:
186188
```swift
187-
func find(pluginName: String) -> Plugin?
189+
func find<T: Plugin>(pluginType: T.Type) -> T?
188190
```
189191

190192
Example Usage:
191193
```swift
192-
let plugin = analytics.find("SomePlugin")
194+
let plugin = analytics.find(pluginType: SomePlugin.self)
193195
```
194196

195197
### remove
196-
remove a registered plugin from the analytics timeline
198+
Remove a registered plugin from the analytics timeline.
197199

198200
Method signature:
199201
```swift
200-
func remove(pluginName: String)
202+
func remove(plugin: Plugin)
201203
```
202204

203205
Example Usage:
204206
```swift
205-
analytics.remove("SomePlugin")
207+
analytics.remove(plugin: somePlugin)
206208
```
207209

208210
### flush
209-
flushes the current queue of events
211+
Flushes the current queue of events.
210212

211213
Example Usage:
212214
```swift
@@ -232,11 +234,9 @@ For example if you wanted to add something to the context object of any event pa
232234
```swift
233235
class SomePlugin: Plugin {
234236
let type: PluginType = .enrichment
235-
let name: String
236237
let analytics: Analytics
237238

238-
init(name: String) {
239-
self.name = name
239+
init() {
240240
}
241241

242242
override fun execute(event: BaseEvent): BaseEvent? {
@@ -256,11 +256,9 @@ For example if you only wanted to act on `track` & `identify` events
256256
```swift
257257
class SomePlugin: EventPlugin {
258258
let type: PluginType = .enrichment
259-
let name: String
260259
let analytics: Analytics
261260

262-
init(name: String) {
263-
self.name = name
261+
init() {
264262
}
265263

266264
func identify(event: IdentifyEvent) -> IdentifyEvent? {
@@ -291,26 +289,26 @@ class AppsFlyerDestination: UIResponder, DestinationPlugin, UserActivities, Remo
291289

292290
let timeline: Timeline = Timeline()
293291
let type: PluginType = .destination
294-
let name: String
292+
let key: String = "AppsFlyer"
295293
var analytics: Analytics?
296294

297295
internal var settings: AppsFlyerSettings? = nil
298296

299-
required init(name: String) {
300-
self.name = name
301-
analytics?.track(name: "AppsFlyer Loaded")
297+
init() {
302298
}
303299

304300
public func update(settings: Settings, type: UpdateType) {
305301
if type == .initial {
306302
// AppsFlyerLib is a singleton, we only want to set it up once.
307-
guard let settings: AppsFlyerSettings = settings.integrationSettings(name: "AppsFlyer") else {return}
303+
guard let settings: AppsFlyerSettings = settings.integrationSettings(key: self.name) else {return}
308304
self.settings = settings
309305

310306
AppsFlyerLib.shared().appsFlyerDevKey = settings.appsFlyerDevKey
311307
AppsFlyerLib.shared().appleAppID = settings.appleAppID
312308
AppsFlyerLib.shared().isDebug = true
313309
AppsFlyerLib.shared().deepLinkDelegate = self
310+
311+
analytics?.track(name: "AppsFlyer Loaded")
314312
}
315313

316314
// additional update logic
@@ -323,7 +321,7 @@ analytics.track("AppsFlyer Event")
323321
```
324322

325323
### Advanced concepts
326-
- `update(settings:)`
324+
- `update(settings:type:)`
327325
Use this function to react to any settings updates. This will be implicitly called when settings are updated.
328326
- OS Lifecycle hooks
329327
Plugins can also hook into lifecycle events by conforming to the platform appropriate protocol. These functions will get called implicitly as the lifecycle events are processed.

Sources/Segment/Events.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,23 @@ extension Analytics {
8282
process(incomingEvent: event)
8383
}
8484

85-
public func screen<P: Codable>(screenTitle: String, category: String? = nil, properties: P?) {
85+
public func screen<P: Codable>(title: String, category: String? = nil, properties: P?) {
8686
do {
8787
if let properties = properties {
8888
let jsonProperties = try JSON(with: properties)
89-
let event = ScreenEvent(screenTitle: screenTitle, category: category, properties: jsonProperties)
89+
let event = ScreenEvent(title: title, category: category, properties: jsonProperties)
9090
process(incomingEvent: event)
9191
} else {
92-
let event = ScreenEvent(screenTitle: screenTitle, category: category)
92+
let event = ScreenEvent(title: title, category: category)
9393
process(incomingEvent: event)
9494
}
9595
} catch {
9696
exceptionFailure("\(error)")
9797
}
9898
}
9999

100-
public func screen(screenTitle: String, category: String? = nil) {
101-
screen(screenTitle: screenTitle, category: category, properties: nil as ScreenEvent?)
100+
public func screen(title: String, category: String? = nil) {
101+
screen(title: title, category: category, properties: nil as ScreenEvent?)
102102
}
103103

104104
public func group<T: Codable>(groupId: String, traits: T?) {
@@ -178,12 +178,12 @@ extension Analytics {
178178
/// - screenTitle: The title of the screen being tracked.
179179
/// - category: A category to the type of screen if it applies.
180180
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
181-
public func screen(screenTitle: String, category: String? = nil, properties: [String: Any]? = nil) {
182-
var event = ScreenEvent(screenTitle: screenTitle, category: category, properties: nil)
181+
public func screen(title: String, category: String? = nil, properties: [String: Any]? = nil) {
182+
var event = ScreenEvent(title: title, category: category, properties: nil)
183183
if let properties = properties {
184184
do {
185185
let jsonProperties = try JSON(properties)
186-
event = ScreenEvent(screenTitle: screenTitle, category: category, properties: jsonProperties)
186+
event = ScreenEvent(title: title, category: category, properties: jsonProperties)
187187
} catch {
188188
exceptionFailure("Could not parse properties.")
189189
}

Sources/Segment/ObjC/ObjCAnalytics.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,26 @@ extension ObjCAnalytics {
6161
/// - Parameters:
6262
/// - screenTitle: The title of the screen being tracked.
6363
@objc(screen:)
64-
public func screen(screenTitle: String) {
65-
screen(screenTitle: screenTitle, category: nil, properties: nil)
64+
public func screen(title: String) {
65+
screen(title: title, category: nil, properties: nil)
6666
}
6767

6868
/// Track a screen change with a title, category and other properties.
6969
/// - Parameters:
7070
/// - screenTitle: The title of the screen being tracked.
7171
/// - category: A category to the type of screen if it applies.
7272
@objc(screen:category:)
73-
public func screen(screenTitle: String, category: String?) {
74-
analytics.screen(screenTitle: screenTitle, category: category, properties: nil)
73+
public func screen(title: String, category: String?) {
74+
analytics.screen(title: title, category: category, properties: nil)
7575
}
7676
/// Track a screen change with a title, category and other properties.
7777
/// - Parameters:
7878
/// - screenTitle: The title of the screen being tracked.
7979
/// - category: A category to the type of screen if it applies.
8080
/// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc.
8181
@objc(screen:category:properties:)
82-
public func screen(screenTitle: String, category: String?, properties: [String: Any]?) {
83-
analytics.screen(screenTitle: screenTitle, category: category, properties: properties)
82+
public func screen(title: String, category: String?, properties: [String: Any]?) {
83+
analytics.screen(title: title, category: category, properties: properties)
8484
}
8585

8686
/// Associate a user with a group such as a company, organization, project, etc.

0 commit comments

Comments
 (0)