diff --git a/Examples/apps/BasicExample/BasicExample/ViewController.swift b/Examples/apps/BasicExample/BasicExample/ViewController.swift index 31acb07d..11a54ea7 100644 --- a/Examples/apps/BasicExample/BasicExample/ViewController.swift +++ b/Examples/apps/BasicExample/BasicExample/ViewController.swift @@ -41,7 +41,7 @@ class ViewController: UIViewController { @IBAction func screenTapped(_ sender: Any) { let props = ScreenProperties(appUsage: usage) - analytics?.screen(screenTitle: "Main Screen", category: "Best", properties: props) + analytics?.screen(title: "Main Screen", category: "Best", properties: props) } @IBAction func identifyTapped(_ sender: Any) { diff --git a/Examples/apps/DestinationsExample/DestinationsExample/ViewController.swift b/Examples/apps/DestinationsExample/DestinationsExample/ViewController.swift index 65f3ca84..610ad4e1 100644 --- a/Examples/apps/DestinationsExample/DestinationsExample/ViewController.swift +++ b/Examples/apps/DestinationsExample/DestinationsExample/ViewController.swift @@ -97,7 +97,7 @@ class ViewController: UIViewController { func screenEvent() { guard let eventFieldText = eventField?.text else { return } - analytics?.screen(screenTitle: eventFieldText, properties: valuesEntered()) + analytics?.screen(title: eventFieldText, properties: valuesEntered()) } func groupEvent() { diff --git a/Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample/ContentView.swift b/Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample/ContentView.swift index e9791776..771b9e0f 100644 --- a/Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample/ContentView.swift +++ b/Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample/ContentView.swift @@ -18,7 +18,7 @@ struct ContentView: View { Text("Track") }).padding(6) Button(action: { - Analytics.main.screen(screenTitle: "Screen appeared") + Analytics.main.screen(title: "Screen appeared") }, label: { Text("Screen") }).padding(6) diff --git a/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/InterfaceController.swift b/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/InterfaceController.swift index c151707f..a794313d 100644 --- a/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/InterfaceController.swift +++ b/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/InterfaceController.swift @@ -21,7 +21,7 @@ class InterfaceController: WKInterfaceController { override func willActivate() { // This method is called when watch view controller is about to be visible to user - analytics?.screen(screenTitle: "Main Screen") + analytics?.screen(title: "Main Screen") } override func didDeactivate() { diff --git a/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/SomeScreenController.swift b/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/SomeScreenController.swift index f230a27d..51d66de5 100644 --- a/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/SomeScreenController.swift +++ b/Examples/apps/watchOSExample/watchOSExample WatchKit Extension/SomeScreenController.swift @@ -17,7 +17,7 @@ class SomeScreenController: WKInterfaceController { override func willActivate() { // This method is called when watch view controller is about to be visible to user - analytics?.screen(screenTitle: "Some Screen Controller") + analytics?.screen(title: "Some Screen Controller") } override func didDeactivate() { diff --git a/Examples/other_plugins/UIKitScreenTracking.swift b/Examples/other_plugins/UIKitScreenTracking.swift index 185c5dd5..670c07c9 100644 --- a/Examples/other_plugins/UIKitScreenTracking.swift +++ b/Examples/other_plugins/UIKitScreenTracking.swift @@ -77,7 +77,7 @@ class UIKitScreenTracking: UtilityPlugin { controller.seg__trackScreen(name: name) } else if let name = name { // if we have a name, call screen - self.analytics?.screen(screenTitle: name) + self.analytics?.screen(title: name) } } } diff --git a/Examples/tasks/CustomScreenTracking.swift b/Examples/tasks/CustomScreenTracking.swift index 6eea9189..cd80d162 100644 --- a/Examples/tasks/CustomScreenTracking.swift +++ b/Examples/tasks/CustomScreenTracking.swift @@ -36,12 +36,12 @@ import Segment class QueryAlertController: UIAlertController { // we already conform to UIKitScreenTrackable so override override func seg__trackScreen(name: String?) { - Analytics.main.screen(screenTitle: "Query", category: "Action Sheet") + Analytics.main.screen(title: "Query", category: "Action Sheet") } } extension UIAlertController: UIKitScreenTrackable { func seg__trackScreen(name: String?) { - Analytics.main.screen(screenTitle: "Basic Alert", category: "Alert") + Analytics.main.screen(title: "Basic Alert", category: "Alert") } } diff --git a/README.md b/README.md index 31d9e980..cee3e7a7 100644 --- a/README.md +++ b/README.md @@ -43,14 +43,14 @@ The Analytics client will typically be set up at application launch time, such a Typically the following call may be all that's required. ```swift -Analytics(configuration: Configuration("SEGMENT_API_KEY")) +Analytics(configuration: Configuration("")) ``` ### Configuration Options When creating a new client, you can configure it in various ways. Some examples are listed below. ```swift -let config = Configuration(writeKey: "8XpdAWa7qJVBJMK8V4FfXQOrnvCzu3Ie") +let config = Configuration(writeKey: "") .flushAt(3) .trackApplicationLifecycleEvents(true) .flushInterval(10) @@ -126,20 +126,20 @@ The screen call lets you record whenever a user sees a screen in your mobile app Method signatures: ```swift -func screen(screenTitle: String, category: String? = nil) -func screen(screenTitle: String, category: String? = nil, properties: P?) +func screen(title: String, category: String? = nil) +func screen(title: String, category: String? = nil, properties: P?) ``` Example Usage: ```swift -analytics.screen(screenTitle: "SomeScreen") +analytics.screen(title: "SomeScreen") ``` You can enable automatic screen tracking by using the [example plugin](https://github.com/segmentio/analytics-example-plugins/blob/main/plugins/swift/UIKitScreenTracking.swift). Once the plugin has been added to your project add it to your Analytics instance: ```swift -analytics.add(plugin: UIKitScreenTracking(name: "ScreenTracking", analytics: analytics)) +analytics.add(plugin: UIKitScreenTracking() ``` ### group @@ -167,46 +167,48 @@ analytics.group("user-123", MyTraits( ``` ### add -add API allows you to add a plugin to the analytics timeline +Add API allows you to add a plugin to the analytics timeline. It will return the plugin instance in +case you wish to store it for access later. Method signature: ```swift -@discardableResult func add(plugin: Plugin) -> String +@discardableResult func add(plugin: Plugin) -> Plugin ``` Example Usage: ```swift -analytics.add(plugin: UIKitScreenTracking(name: "ScreenTracking")) +analytics.add(plugin: UIKitScreenTracking()) ``` ### find -find a registered plugin from the analytics timeline +Find a registered plugin from the analytics timeline. It will return the first plugin +of the specified type. Method signature: ```swift -func find(pluginName: String) -> Plugin? +func find(pluginType: T.Type) -> T? ``` Example Usage: ```swift -let plugin = analytics.find("SomePlugin") +let plugin = analytics.find(pluginType: SomePlugin.self) ``` ### remove -remove a registered plugin from the analytics timeline +Remove a registered plugin from the analytics timeline. Method signature: ```swift -func remove(pluginName: String) +func remove(plugin: Plugin) ``` Example Usage: ```swift -analytics.remove("SomePlugin") +analytics.remove(plugin: somePlugin) ``` ### flush -flushes the current queue of events +Flushes the current queue of events. Example Usage: ```swift @@ -232,11 +234,9 @@ For example if you wanted to add something to the context object of any event pa ```swift class SomePlugin: Plugin { let type: PluginType = .enrichment - let name: String let analytics: Analytics - init(name: String) { - self.name = name + init() { } override fun execute(event: BaseEvent): BaseEvent? { @@ -256,11 +256,9 @@ For example if you only wanted to act on `track` & `identify` events ```swift class SomePlugin: EventPlugin { let type: PluginType = .enrichment - let name: String let analytics: Analytics - init(name: String) { - self.name = name + init() { } func identify(event: IdentifyEvent) -> IdentifyEvent? { @@ -291,26 +289,26 @@ class AppsFlyerDestination: UIResponder, DestinationPlugin, UserActivities, Remo let timeline: Timeline = Timeline() let type: PluginType = .destination - let name: String + let key: String = "AppsFlyer" var analytics: Analytics? internal var settings: AppsFlyerSettings? = nil - required init(name: String) { - self.name = name - analytics?.track(name: "AppsFlyer Loaded") + init() { } public func update(settings: Settings, type: UpdateType) { if type == .initial { // AppsFlyerLib is a singleton, we only want to set it up once. - guard let settings: AppsFlyerSettings = settings.integrationSettings(name: "AppsFlyer") else {return} + guard let settings: AppsFlyerSettings = settings.integrationSettings(key: self.name) else {return} self.settings = settings AppsFlyerLib.shared().appsFlyerDevKey = settings.appsFlyerDevKey AppsFlyerLib.shared().appleAppID = settings.appleAppID AppsFlyerLib.shared().isDebug = true AppsFlyerLib.shared().deepLinkDelegate = self + + analytics?.track(name: "AppsFlyer Loaded") } // additional update logic @@ -323,7 +321,7 @@ analytics.track("AppsFlyer Event") ``` ### Advanced concepts -- `update(settings:)` +- `update(settings:type:)` Use this function to react to any settings updates. This will be implicitly called when settings are updated. - OS Lifecycle hooks 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. diff --git a/Sources/Segment/Events.swift b/Sources/Segment/Events.swift index b28bbdf5..70c83749 100644 --- a/Sources/Segment/Events.swift +++ b/Sources/Segment/Events.swift @@ -82,14 +82,14 @@ extension Analytics { process(incomingEvent: event) } - public func screen(screenTitle: String, category: String? = nil, properties: P?) { + public func screen(title: String, category: String? = nil, properties: P?) { do { if let properties = properties { let jsonProperties = try JSON(with: properties) - let event = ScreenEvent(screenTitle: screenTitle, category: category, properties: jsonProperties) + let event = ScreenEvent(title: title, category: category, properties: jsonProperties) process(incomingEvent: event) } else { - let event = ScreenEvent(screenTitle: screenTitle, category: category) + let event = ScreenEvent(title: title, category: category) process(incomingEvent: event) } } catch { @@ -97,8 +97,8 @@ extension Analytics { } } - public func screen(screenTitle: String, category: String? = nil) { - screen(screenTitle: screenTitle, category: category, properties: nil as ScreenEvent?) + public func screen(title: String, category: String? = nil) { + screen(title: title, category: category, properties: nil as ScreenEvent?) } public func group(groupId: String, traits: T?) { @@ -178,12 +178,12 @@ extension Analytics { /// - screenTitle: The title of the screen being tracked. /// - category: A category to the type of screen if it applies. /// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc. - public func screen(screenTitle: String, category: String? = nil, properties: [String: Any]? = nil) { - var event = ScreenEvent(screenTitle: screenTitle, category: category, properties: nil) + public func screen(title: String, category: String? = nil, properties: [String: Any]? = nil) { + var event = ScreenEvent(title: title, category: category, properties: nil) if let properties = properties { do { let jsonProperties = try JSON(properties) - event = ScreenEvent(screenTitle: screenTitle, category: category, properties: jsonProperties) + event = ScreenEvent(title: title, category: category, properties: jsonProperties) } catch { exceptionFailure("Could not parse properties.") } diff --git a/Sources/Segment/ObjC/ObjCAnalytics.swift b/Sources/Segment/ObjC/ObjCAnalytics.swift index ff9ba44f..27a9bcaf 100644 --- a/Sources/Segment/ObjC/ObjCAnalytics.swift +++ b/Sources/Segment/ObjC/ObjCAnalytics.swift @@ -61,8 +61,8 @@ extension ObjCAnalytics { /// - Parameters: /// - screenTitle: The title of the screen being tracked. @objc(screen:) - public func screen(screenTitle: String) { - screen(screenTitle: screenTitle, category: nil, properties: nil) + public func screen(title: String) { + screen(title: title, category: nil, properties: nil) } /// Track a screen change with a title, category and other properties. @@ -70,8 +70,8 @@ extension ObjCAnalytics { /// - screenTitle: The title of the screen being tracked. /// - category: A category to the type of screen if it applies. @objc(screen:category:) - public func screen(screenTitle: String, category: String?) { - analytics.screen(screenTitle: screenTitle, category: category, properties: nil) + public func screen(title: String, category: String?) { + analytics.screen(title: title, category: category, properties: nil) } /// Track a screen change with a title, category and other properties. /// - Parameters: @@ -79,8 +79,8 @@ extension ObjCAnalytics { /// - category: A category to the type of screen if it applies. /// - properties: Any extra metadata associated with the screen. e.g. method of access, size, etc. @objc(screen:category:properties:) - public func screen(screenTitle: String, category: String?, properties: [String: Any]?) { - analytics.screen(screenTitle: screenTitle, category: category, properties: properties) + public func screen(title: String, category: String?, properties: [String: Any]?) { + analytics.screen(title: title, category: category, properties: properties) } /// Associate a user with a group such as a company, organization, project, etc. diff --git a/Sources/Segment/Types.swift b/Sources/Segment/Types.swift index e30d0964..98520815 100644 --- a/Sources/Segment/Types.swift +++ b/Sources/Segment/Types.swift @@ -91,14 +91,14 @@ public struct ScreenEvent: RawEvent { public var category: String? public var properties: JSON? - public init(screenTitle: String? = nil, category: String?, properties: JSON? = nil) { - self.name = screenTitle + public init(title: String? = nil, category: String?, properties: JSON? = nil) { + self.name = title self.category = category self.properties = properties } public init(existing: ScreenEvent) { - self.init(screenTitle: existing.name, category: existing.category, properties: existing.properties) + self.init(title: existing.name, category: existing.category, properties: existing.properties) applyRawEventData(event: existing) } } diff --git a/Tests/Segment-Tests/Analytics_Tests.swift b/Tests/Segment-Tests/Analytics_Tests.swift index 3d1bfc02..ec943bc0 100644 --- a/Tests/Segment-Tests/Analytics_Tests.swift +++ b/Tests/Segment-Tests/Analytics_Tests.swift @@ -241,13 +241,13 @@ final class Analytics_Tests: XCTestCase { waitUntilStarted(analytics: analytics) - analytics.screen(screenTitle: "screen1", category: "category1") + analytics.screen(title: "screen1", category: "category1") let screen1Event: ScreenEvent? = outputReader.lastEvent as? ScreenEvent XCTAssertTrue(screen1Event?.name == "screen1") XCTAssertTrue(screen1Event?.category == "category1") - analytics.screen(screenTitle: "screen2", category: "category2", properties: MyTraits(email: "blah@blah.com")) + analytics.screen(title: "screen2", category: "category2", properties: MyTraits(email: "blah@blah.com")) let screen2Event: ScreenEvent? = outputReader.lastEvent as? ScreenEvent XCTAssertTrue(screen2Event?.name == "screen2")