Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion Examples/other_plugins/UIKitScreenTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Examples/tasks/CustomScreenTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
54 changes: 26 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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("<YOUR_WRITE_KEY>"))
```

### 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: "<YOUR_WRITE_KEY>")
.flushAt(3)
.trackApplicationLifecycleEvents(true)
.flushInterval(10)
Expand Down Expand Up @@ -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<P: Codable>(screenTitle: String, category: String? = nil, properties: P?)
func screen(title: String, category: String? = nil)
func screen<P: Codable>(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
Expand Down Expand Up @@ -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<T: Plugin>(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
Expand All @@ -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? {
Expand All @@ -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? {
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions Sources/Segment/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ extension Analytics {
process(incomingEvent: event)
}

public func screen<P: Codable>(screenTitle: String, category: String? = nil, properties: P?) {
public func screen<P: Codable>(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 {
exceptionFailure("\(error)")
}
}

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<T: Codable>(groupId: String, traits: T?) {
Expand Down Expand Up @@ -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.")
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Segment/ObjC/ObjCAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,26 @@ 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.
/// - Parameters:
/// - 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:
/// - 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.
@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.
Expand Down
6 changes: 3 additions & 3 deletions Sources/Segment/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]"))
analytics.screen(title: "screen2", category: "category2", properties: MyTraits(email: "[email protected]"))

let screen2Event: ScreenEvent? = outputReader.lastEvent as? ScreenEvent
XCTAssertTrue(screen2Event?.name == "screen2")
Expand Down