Skip to content

Commit f1138bf

Browse files
bsneedBrandon Sneed
andauthored
Plugin Interface refactor (#35)
* Initial plugin refactor on macOS * Updated integrationSettings collection. * Fixups for other platforms. * Example apps and samples fixups. Co-authored-by: Brandon Sneed <brandon.sneed@segment.com>
1 parent 98d4f4e commit f1138bf

35 files changed

+240
-281
lines changed

Examples/apps/DestinationsExample/DestinationsExample/AppDelegate.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2424
analytics = Analytics(configuration: configuration)
2525

2626
// Add Adjust destination plugin
27-
analytics?.add(plugin: AdjustDestination(name: "Adjust"))
27+
analytics?.add(plugin: AdjustDestination())
2828

2929
// Add Amplitude session plugin
30-
analytics?.add(plugin: AmplitudeSession(name: "Amplitude"))
30+
analytics?.add(plugin: AmplitudeSession())
3131

3232
// Add Mixpanel destination plugin
33-
analytics?.add(plugin: MixpanelDestination(name: "Mixpanel"))
33+
analytics?.add(plugin: MixpanelDestination())
3434

3535
// Add Flurry destination plugin
36-
analytics?.add(plugin: FlurryDestination(name: "Flurry"))
36+
analytics?.add(plugin: FlurryDestination())
3737

3838
// Add the Firebase destination plugin
39-
analytics?.add(plugin: FirebaseDestination(name: "Firebase"))
39+
analytics?.add(plugin: FirebaseDestination())
4040

4141
//Add the AppsFlyer destination plugin
42-
analytics?.add(plugin: AppsFlyerDestination(name: "AppsFlyer"))
42+
analytics?.add(plugin: AppsFlyerDestination())
4343

4444
return true
4545
}

Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample.xcodeproj/project.pbxproj

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

99
/* Begin PBXBuildFile section */
10+
46016C1326BB5BD700BCEE80 /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 46016C1226BB5BD700BCEE80 /* Segment */; };
1011
9613E655265C2E350078A2BD /* SegmentSwiftUIExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9613E654265C2E350078A2BD /* SegmentSwiftUIExampleApp.swift */; };
1112
9613E657265C2E350078A2BD /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9613E656265C2E350078A2BD /* ContentView.swift */; };
1213
9613E659265C2E360078A2BD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9613E658265C2E360078A2BD /* Assets.xcassets */; };
1314
9613E65C265C2E360078A2BD /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9613E65B265C2E360078A2BD /* Preview Assets.xcassets */; };
14-
9613E665265C2E880078A2BD /* Segment in Frameworks */ = {isa = PBXBuildFile; productRef = 9613E664265C2E880078A2BD /* Segment */; };
1515
/* End PBXBuildFile section */
1616

1717
/* Begin PBXFileReference section */
@@ -28,18 +28,26 @@
2828
isa = PBXFrameworksBuildPhase;
2929
buildActionMask = 2147483647;
3030
files = (
31-
9613E665265C2E880078A2BD /* Segment in Frameworks */,
31+
46016C1326BB5BD700BCEE80 /* Segment in Frameworks */,
3232
);
3333
runOnlyForDeploymentPostprocessing = 0;
3434
};
3535
/* End PBXFrameworksBuildPhase section */
3636

3737
/* Begin PBXGroup section */
38+
46016C1126BB5BD700BCEE80 /* Frameworks */ = {
39+
isa = PBXGroup;
40+
children = (
41+
);
42+
name = Frameworks;
43+
sourceTree = "<group>";
44+
};
3845
9613E648265C2E350078A2BD = {
3946
isa = PBXGroup;
4047
children = (
4148
9613E653265C2E350078A2BD /* SegmentSwiftUIExample */,
4249
9613E652265C2E350078A2BD /* Products */,
50+
46016C1126BB5BD700BCEE80 /* Frameworks */,
4351
);
4452
sourceTree = "<group>";
4553
};
@@ -88,7 +96,7 @@
8896
);
8997
name = SegmentSwiftUIExample;
9098
packageProductDependencies = (
91-
9613E664265C2E880078A2BD /* Segment */,
99+
46016C1226BB5BD700BCEE80 /* Segment */,
92100
);
93101
productName = SegmentSwiftUIExample;
94102
productReference = 9613E651265C2E350078A2BD /* SegmentSwiftUIExample.app */;
@@ -118,7 +126,6 @@
118126
);
119127
mainGroup = 9613E648265C2E350078A2BD;
120128
packageReferences = (
121-
9613E663265C2E880078A2BD /* XCRemoteSwiftPackageReference "analytics-swift" */,
122129
);
123130
productRefGroup = 9613E652265C2E350078A2BD /* Products */;
124131
projectDirPath = "";
@@ -335,21 +342,9 @@
335342
};
336343
/* End XCConfigurationList section */
337344

338-
/* Begin XCRemoteSwiftPackageReference section */
339-
9613E663265C2E880078A2BD /* XCRemoteSwiftPackageReference "analytics-swift" */ = {
340-
isa = XCRemoteSwiftPackageReference;
341-
repositoryURL = "git@github.com:segmentio/analytics-swift.git";
342-
requirement = {
343-
branch = main;
344-
kind = branch;
345-
};
346-
};
347-
/* End XCRemoteSwiftPackageReference section */
348-
349345
/* Begin XCSwiftPackageProductDependency section */
350-
9613E664265C2E880078A2BD /* Segment */ = {
346+
46016C1226BB5BD700BCEE80 /* Segment */ = {
351347
isa = XCSwiftPackageProductDependency;
352-
package = 9613E663265C2E880078A2BD /* XCRemoteSwiftPackageReference "analytics-swift" */;
353348
productName = Segment;
354349
};
355350
/* End XCSwiftPackageProductDependency section */

Examples/apps/SegmentSwiftUIExample/SegmentSwiftUIExample.xcworkspace/contents.xcworkspacedata

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Examples/apps/SegmentUIKitExample/SegmentUIKitExample/AppDelegate.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1515
// add console logging plugins to our multiple instances
1616
Analytics.main.add(plugin: ConsoleLogger(name: "main"))
17-
Analytics.main.add(plugin: ConsentTracking(name: "consent"))
18-
Analytics.main.add(plugin: IDFACollection(name: "idfa"))
19-
Analytics.main.add(plugin: UIKitScreenTracking(name: "autoScreenTracking"))
17+
Analytics.main.add(plugin: ConsentTracking())
18+
Analytics.main.add(plugin: IDFACollection())
19+
Analytics.main.add(plugin: UIKitScreenTracking())
2020

2121
Analytics.support.add(plugin: ConsoleLogger(name: "support"))
22-
Analytics.support.add(plugin: ConsentTracking(name: "consent"))
22+
Analytics.support.add(plugin: ConsentTracking())
2323

2424
Analytics.support.track(name: "test event")
2525

Examples/destination_plugins/AdjustDestination.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,16 @@ internal struct AdjustSettings: Codable {
4747
}
4848

4949
@objc
50-
public class AdjustDestination: NSObject, DestinationPlugin, RemoteNotifications {
51-
public let timeline: Timeline = Timeline()
52-
public let type: PluginType = .destination
53-
public let name: String
54-
public var analytics: Analytics? = nil
50+
class AdjustDestination: NSObject, DestinationPlugin, RemoteNotifications {
51+
let timeline = Timeline()
52+
let type = PluginType.destination
53+
let key = "Adjust"
54+
var analytics: Analytics? = nil
5555

5656
internal var settings: AdjustSettings? = nil
5757

58-
required public init(name: String) {
59-
self.name = name
60-
}
61-
6258
public func update(settings: Settings) {
63-
guard let settings: AdjustSettings = settings.integrationSettings(name: "Adjust") else { return }
59+
guard let settings: AdjustSettings = settings.integrationSettings(forPlugin: self) else { return }
6460
self.settings = settings
6561

6662
var environment = ADJEnvironmentSandbox

Examples/destination_plugins/AmplitudeSession.swift

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,45 @@ import Foundation
4242
import Segment
4343

4444
class AmplitudeSession: EventPlugin, iOSLifecycle {
45-
46-
var type: PluginType
47-
var name: String
45+
var key = "Amplitude"
46+
var type = PluginType.enrichment
4847
var analytics: Analytics?
4948

49+
var active = false
50+
5051
private var sessionTimer: Timer?
5152
private var sessionID: TimeInterval?
5253
private let fireTime = TimeInterval(300)
5354

54-
required init(name: String) {
55-
self.name = name
56-
self.type = .enrichment
55+
func update(settings: Settings) {
56+
if settings.isDestinationEnabled(key: key) {
57+
active = true
58+
} else {
59+
active = false
60+
}
61+
}
62+
63+
func execute<T: RawEvent>(event: T?) -> T? {
64+
if !active {
65+
return event
66+
}
67+
68+
var result: T? = event
69+
switch result {
70+
case let r as IdentifyEvent:
71+
result = self.identify(event: r) as? T
72+
case let r as TrackEvent:
73+
result = self.track(event: r) as? T
74+
case let r as ScreenEvent:
75+
result = self.screen(event: r) as? T
76+
case let r as AliasEvent:
77+
result = self.alias(event: r) as? T
78+
case let r as GroupEvent:
79+
result = self.group(event: r) as? T
80+
default:
81+
break
82+
}
83+
return result
5784
}
5885

5986
func track(event: TrackEvent) -> TrackEvent? {
@@ -101,15 +128,15 @@ class AmplitudeSession: EventPlugin, iOSLifecycle {
101128
}
102129
}
103130

131+
104132
// MARK: - AmplitudeSession Helper Methods
105133
extension AmplitudeSession {
106-
107134
func insertSession(event: RawEvent) -> RawEvent {
108135
var returnEvent = event
109136
if var integrations = event.integrations?.dictionaryValue,
110137
let sessionID = sessionID {
111138

112-
integrations["Amplitude"] = ["session_id": (Int(sessionID) * 1000)]
139+
integrations[key] = ["session_id": (Int(sessionID) * 1000)]
113140
returnEvent.integrations = try? JSON(integrations as Any)
114141
}
115142
return returnEvent

Examples/destination_plugins/AppsFlyerDestination.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,17 @@ private struct AppsFlyerSettings: Codable {
4646

4747
@objc
4848
class AppsFlyerDestination: UIResponder, DestinationPlugin, RemoteNotifications, iOSLifecycle {
49+
let timeline = Timeline()
50+
let type = PluginType.destination
51+
let key = "AppsFlyer"
4952

50-
let timeline: Timeline = Timeline()
51-
let type: PluginType = .destination
52-
let name: String
5353
var analytics: Analytics?
5454

5555
fileprivate var settings: AppsFlyerSettings? = nil
5656

57-
required init(name: String) {
58-
self.name = name
59-
analytics?.track(name: "AppsFlyer Loaded")
60-
}
61-
6257
public func update(settings: Settings) {
6358

64-
guard let settings: AppsFlyerSettings = settings.integrationSettings(name: "AppsFlyer") else { return }
59+
guard let settings: AppsFlyerSettings = settings.integrationSettings(forPlugin: self) else { return }
6560
self.settings = settings
6661

6762
AppsFlyerLib.shared().appsFlyerDevKey = settings.appsFlyerDevKey

Examples/destination_plugins/FirebaseDestination.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ import FirebaseAnalytics
4343
An implmentation of the Firebase Analytics device mode destination as a plugin.
4444
*/
4545

46+
struct FirebaseSettings: Codable {
47+
let deepLinkURLScheme: String?
48+
}
49+
4650
class FirebaseDestination: DestinationPlugin {
47-
let timeline: Timeline = Timeline()
48-
let type: PluginType = .destination
49-
let name: String
51+
let timeline = Timeline()
52+
let type = PluginType.destination
53+
let key = "Firebase"
5054
var analytics: Segment.Analytics? = nil
5155

52-
required init(name: String) {
53-
self.name = name
54-
}
55-
5656
func update(settings: Settings) {
57-
guard let firebaseSettings = settings.integrationSettings(for: "Firebase") else { return }
58-
if let deepLinkURLScheme = firebaseSettings["deepLinkURLScheme"] as? String {
57+
guard let firebaseSettings: FirebaseSettings = settings.integrationSettings(forPlugin: self) else { return }
58+
if let deepLinkURLScheme = firebaseSettings.deepLinkURLScheme {
5959
FirebaseOptions.defaultOptions()?.deepLinkURLScheme = deepLinkURLScheme
6060
analytics?.log(message: "Added deepLinkURLScheme: \(deepLinkURLScheme)")
6161
}

Examples/destination_plugins/FlurryDestination.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,35 @@ import FlurryAnalytics
4242
An implmentation of the Flurry Analytics device mode destination as a plugin.
4343
*/
4444

45+
private struct FlurrySettings: Codable {
46+
let apiKey: String
47+
let sessionContinueSeconds: Int?
48+
let screenTracksEvents: Bool?
49+
}
50+
51+
4552
class FlurryDestination: DestinationPlugin {
46-
let timeline: Timeline = Timeline()
47-
let type: PluginType = .destination
48-
let name: String
53+
let timeline = Timeline()
54+
let type = PluginType.destination
55+
let key = "Flurry"
4956
var analytics: Analytics? = nil
5057

5158
var screenTracksEvents = false
5259

53-
required init(name: String) {
54-
self.name = name
55-
}
56-
5760
func update(settings: Settings) {
58-
guard let jsonSettings = settings.integrationSettings(for: "Flurry") else { return }
59-
guard let flurryApiKey = jsonSettings["apiKey"] as? String else { return }
61+
guard let flurrySettings: FlurrySettings = settings.integrationSettings(forPlugin: self) else { return }
6062

6163
let builder = FlurrySessionBuilder()
6264

63-
if let sessionContinueSeconds = jsonSettings["sessionContinueSeconds"] as? Int {
65+
if let sessionContinueSeconds = flurrySettings.sessionContinueSeconds {
6466
builder.withSessionContinueSeconds(sessionContinueSeconds)
6567
}
6668

67-
if let screenTracksEvents = jsonSettings["screenTracksEvents"] as? Bool {
69+
if let screenTracksEvents = flurrySettings.screenTracksEvents {
6870
self.screenTracksEvents = screenTracksEvents
6971
}
7072

71-
Flurry.startSession(flurryApiKey, with: builder)
73+
Flurry.startSession(flurrySettings.apiKey, with: builder)
7274
}
7375

7476
func identify(event: IdentifyEvent) -> IdentifyEvent? {

0 commit comments

Comments
 (0)