Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bsneed/options #26

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion Example/BasicExample/BasicExample/BasicExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ extension Analytics {
instance = Analytics(configuration: Configuration(writeKey: "<WRITE_KEY>")
.flushAt(3)
.trackApplicationLifecycleEvents(true))
instance?.add(plugin: AmplitudeSession())
instance?.add(plugin: AmplitudeSession(
enableSessionEvents: true,
enableLifecycleEvents: true,
enableScreenEvents: true,
enableDeepLinkEvents: true
))
}

return instance!
Expand Down
2 changes: 1 addition & 1 deletion Example/BasicExample/BasicExample/ContentView.swift
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(title: "Screen appeared")
Analytics.main.screen(title: "MyScreen")
}, label: {
Text("Screen")
}).padding(6)
Expand Down
100 changes: 67 additions & 33 deletions Sources/SegmentAmplitude/AmplitudeSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,13 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
public var type = PluginType.enrichment
public weak var analytics: Analytics?

internal struct Constants {
static let ampPrefix = "[Amplitude] "

static let ampSessionEndEvent = "session_end"
static let ampSessionStartEvent = "session_start"
static let ampAppInstalledEvent = "\(ampPrefix)Application Installed"
static let ampAppUpdatedEvent = "\(ampPrefix)Application Updated"
static let ampAppOpenedEvent = "\(ampPrefix)Application Opened"
static let ampAppBackgroundedEvent = "\(ampPrefix)Application Backgrounded"
static let ampDeepLinkOpenedEvent = "\(ampPrefix)Deep Link Opened"
static let ampScreenViewedEvent = "\(ampPrefix)Screen Viewed"
static let ampScreenNameProperty = "\(ampPrefix)Screen Name"
}

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 @@ -75,9 +66,20 @@ public class AmplitudeSession: EventPlugin, iOSLifecycle {
}
}

public init() {
public init(
enableSessionEvents: Bool = true,
enableLifecycleEvents: Bool = false,
enableScreenEvents: Bool = false,
enableDeepLinkEvents: Bool = false
) {
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 @@ -112,6 +114,8 @@ 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
// amp needs the `name` in the properties
Expand All @@ -121,8 +125,22 @@ 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
}


// handle track
if var trackEvent = workingEvent as? TrackEvent {
Expand All @@ -133,10 +151,18 @@ 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 @@ -146,26 +172,34 @@ 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.
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
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
22 changes: 22 additions & 0 deletions Sources/SegmentAmplitude/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Constants.swift
//
//
// Created by Brandon Sneed on 7/9/24.
//

import Foundation

internal struct Constants {
static let ampPrefix = "[Amplitude] "

static let ampSessionEndEvent = "session_end"
static let ampSessionStartEvent = "session_start"
static let ampAppInstalledEvent = "\(ampPrefix)Application Installed"
static let ampAppUpdatedEvent = "\(ampPrefix)Application Updated"
static let ampAppOpenedEvent = "\(ampPrefix)Application Opened"
static let ampAppBackgroundedEvent = "\(ampPrefix)Application Backgrounded"
static let ampDeepLinkOpenedEvent = "\(ampPrefix)Deep Link Opened"
static let ampScreenViewedEvent = "\(ampPrefix)Screen Viewed"
static let ampScreenNameProperty = "\(ampPrefix)Screen Name"
}
14 changes: 0 additions & 14 deletions Sources/SegmentAmplitude/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@ import Segment
// MARK: - Amplitude Session Management

internal class Session {
internal struct Constants {
static let ampPrefix = "[Amplitude] "

static let ampSessionEndEvent = "session_end"
static let ampSessionStartEvent = "session_start"
static let ampAppInstalledEvent = "\(ampPrefix)Application Installed"
static let ampAppUpdatedEvent = "\(ampPrefix)Application Updated"
static let ampAppOpenedEvent = "\(ampPrefix)Application Opened"
static let ampAppBackgroundedEvent = "\(ampPrefix)Application Backgrounded"
static let ampDeepLinkOpenedEvent = "\(ampPrefix)Deep Link Opened"
static let ampScreenViewedEvent = "\(ampPrefix)Screen Viewed"
static let ampRevenueEvent = "revenue_amount"
}

@Atomic var sessionID: Int64 {
didSet {
storage.write(key: Storage.Constants.previousSessionID, value: sessionID)
Expand Down
Loading