Skip to content

Commit

Permalink
Removing configurations and conditional code
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGHSeg committed Jul 10, 2024
1 parent bbab249 commit 83824cf
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 75 deletions.
7 changes: 1 addition & 6 deletions Example/BasicExample/BasicExample/BasicExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ extension Analytics {
instance = Analytics(configuration: Configuration(writeKey: "<WRITE_KEY>")
.flushAt(3)
.trackApplicationLifecycleEvents(true))
instance?.add(plugin: AmplitudeSession(
enableSessionEvents: true,
enableLifecycleEvents: true,
enableScreenEvents: true,
enableDeepLinkEvents: true
))
instance?.add(plugin: AmplitudeSession())
}

return instance!
Expand Down
70 changes: 1 addition & 69 deletions Sources/SegmentAmplitude/AmplitudeSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {

public var logging: Bool = false

private let enableSessionEvents: Bool
private let enableLifecycleEvents: Bool
private let enableScreenEvents: Bool
private let enableDeepLinkEvents: Bool

@Atomic private var active = false
@Atomic private var inForeground: Bool = false
@Atomic private var resetPending: Bool = false
Expand All @@ -66,20 +61,9 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
}
}

public init(
enableSessionEvents: Bool = true,
enableLifecycleEvents: Bool = false,
enableScreenEvents: Bool = false,
enableDeepLinkEvents: Bool = false
) {
public init() {
self.sessionID = storage.read(key: Storage.Constants.previousSessionID) ?? -1
self.lastEventTime = storage.read(key: Storage.Constants.lastEventTime) ?? -1

self.enableSessionEvents = enableSessionEvents
self.enableScreenEvents = enableScreenEvents
self.enableLifecycleEvents = enableLifecycleEvents
self.enableDeepLinkEvents = enableDeepLinkEvents

debugLog("startup sessionID = \(sessionID)")
}

Expand Down Expand Up @@ -114,7 +98,6 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
startNewSessionIfNecessary()

// handle screen

// this code works off the destination action logic.
if var screenEvent = workingEvent as? ScreenEvent, let screenName = screenEvent.name {
var adjustedProps = screenEvent.properties
Expand All @@ -125,19 +108,6 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
adjustedProps?.setValue(screenName, forKeyPath: KeyPath("name"))
}
screenEvent.properties = adjustedProps

// if we're sending Amplitude's definition of a screen event ...
if enableScreenEvents {
// set the keyname amplitude wants
adjustedProps?.setValue(screenName, forKeyPath: KeyPath(Constants.ampScreenNameProperty))
// remove the unnecessary `name` property.
adjustedProps = try? adjustedProps?.remove(key: "name")
// send a new track call for amplitude to pick up the screen.
analytics?.track(name: Constants.ampScreenViewedEvent, properties: adjustedProps)
// keep our current screen event from going to amplitude.
screenEvent.integrations?.setValue(false, forKeyPath: KeyPath(key))
}

workingEvent = screenEvent as? T
}

Expand All @@ -151,18 +121,10 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
resetPending = false
eventSessionID = sessionID
debugLog("NewSession = \(eventSessionID)")
if !enableSessionEvents {
// don't send these events, so return nil to drop it.
return nil
}
}

if eventName == Constants.ampSessionEndEvent {
debugLog("EndSession = \(eventSessionID)")
if !enableSessionEvents {
// don't send these events, so return nil to drop it.
return nil
}
}

// if it's amp specific stuff, disable all the integrations except for amp.
Expand All @@ -172,36 +134,6 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
trackEvent.integrations = integrations
}

if enableDeepLinkEvents {
if eventName == "Deep Link Opened" {
analytics?.track(name: Constants.ampDeepLinkOpenedEvent, properties: trackEvent.properties)
}
}

// handle events that need to be re-generated back to amplitude.
// block the originals from going to amplitude as well.
if enableLifecycleEvents {
switch trackEvent.event {
case "Application Opened":
analytics?.track(name: Constants.ampAppOpenedEvent, properties: trackEvent.properties)
trackEvent.integrations?.setValue(false, forKeyPath: KeyPath(key))
case "Application Installed":
analytics?.track(name: Constants.ampAppInstalledEvent, properties: trackEvent.properties)
trackEvent.integrations?.setValue(false, forKeyPath: KeyPath(key))
case "Application Updated":
analytics?.track(name: Constants.ampAppUpdatedEvent, properties: trackEvent.properties)
trackEvent.integrations?.setValue(false, forKeyPath: KeyPath(key))
case "Application Backgrounded":
analytics?.track(name: Constants.ampAppBackgroundedEvent, properties: trackEvent.properties)
trackEvent.integrations?.setValue(false, forKeyPath: KeyPath(key))
case "Application Foregrounded":
// amplitude doesn't need this one, it's redundant.
trackEvent.integrations?.setValue(false, forKeyPath: KeyPath(key))
default:
break
}
}

workingEvent = trackEvent as? T
}

Expand Down

0 comments on commit 83824cf

Please sign in to comment.