Skip to content
Open
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
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/migueldeicaza/SwiftTerm", from: "1.0.0"),
.package(url: "https://github.com/gonzalezreal/swift-markdown-ui", from: "2.4.0"),
.package(url: "https://github.com/PostHog/posthog-ios", from: "3.0.0"),
],
targets: [
.executableTarget(
name: "KanbanCode",
dependencies: ["KanbanCodeCore", "SwiftTerm", .product(name: "MarkdownUI", package: "swift-markdown-ui")],
dependencies: ["KanbanCodeCore", "SwiftTerm", .product(name: "MarkdownUI", package: "swift-markdown-ui"), .product(name: "PostHog", package: "posthog-ios")],
path: "Sources/KanbanCode",
resources: [.copy("Resources")]
),
Expand Down
88 changes: 88 additions & 0 deletions Sources/KanbanCode/Analytics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Foundation
import PostHog
import KanbanCodeCore

enum Analytics {
private static let apiKey = "phc_REPLACE_WITH_YOUR_KEY"
private static let host = "https://us.i.posthog.com"

static func setup() {
let config = PostHogConfig(apiKey: apiKey, host: host)
config.captureApplicationLifecycleEvents = true
config.captureScreenViews = false
config.sendFeatureFlagsOnNewIdentity = false
PostHogSDK.shared.setup(config)

capture("app_opened")
}

static func capture(_ event: String, properties: [String: Any] = [:]) {
var props = properties
props["platform"] = "macos"
props["app_version"] = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown"
PostHogSDK.shared.capture(event, properties: props)
}

/// Hook into BoardStore.onAction to track user-initiated actions.
static func trackAction(_ action: Action) {
switch action {
// Card lifecycle
case .createManualTask:
capture("card_created")
case .launchCard(_, _, _, let worktreeName, let runRemotely, _):
capture("card_launched", properties: [
"has_worktree": worktreeName != nil,
"run_remotely": runRemotely,
])
case .resumeCard:
capture("card_resumed")
case .moveCard(_, let to):
capture("card_moved", properties: ["to_column": to.rawValue])
case .deleteCard:
capture("card_deleted")
case .archiveCard:
capture("card_archived")
case .renameCard:
capture("card_renamed")
case .mergeCards:
capture("cards_merged")

// Terminal
case .createTerminal:
capture("terminal_created")
case .addExtraTerminal:
capture("extra_terminal_added")
case .killTerminal:
capture("terminal_killed")

// Queued prompts
case .addQueuedPrompt:
capture("queued_prompt_added")
case .sendQueuedPrompt:
capture("queued_prompt_sent")
case .removeQueuedPrompt:
capture("queued_prompt_removed")

// Linking
case .addBranchToCard:
capture("branch_linked")
case .addIssueLinkToCard:
capture("issue_linked")
case .addPRToCard:
capture("pr_linked")
case .markPRMerged:
capture("pr_marked_merged")

// Selection
case .selectCard(let cardId):
if cardId != nil { capture("card_selected") }

case .setSelectedProject(let path):
capture("project_filtered", properties: ["has_filter": path != nil])

// Skip internal/background actions
default:
break
}
}
}
2 changes: 2 additions & 0 deletions Sources/KanbanCode/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ struct KanbanCodeApp: App {

final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate, @unchecked Sendable {
func applicationDidFinishLaunching(_ notification: Notification) {
Analytics.setup()

NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
if let window = NSApp.windows.first {
Expand Down
1 change: 1 addition & 0 deletions Sources/KanbanCode/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct ContentView: View {
worktreeAdapter: GitWorktreeAdapter(),
tmuxAdapter: tmux
)
boardStore.onAction = { action in Analytics.trackAction(action) }

// Load Pushover from settings.json, wrap in CompositeNotifier with macOS fallback
let pushover = Self.loadPushoverConfig()
Expand Down
4 changes: 4 additions & 0 deletions Sources/KanbanCodeCore/UseCases/BoardStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,9 @@ public final class BoardStore: @unchecked Sendable {

public let sessionStore: SessionStore

/// Optional callback invoked on every user-initiated dispatch. Used for analytics.
public var onAction: (@MainActor (Action) -> Void)?

public init(
effectHandler: EffectHandler,
discovery: SessionDiscovery,
Expand All @@ -1178,6 +1181,7 @@ public final class BoardStore: @unchecked Sendable {

/// Dispatch an action. Reducer runs synchronously, effects run async.
public func dispatch(_ action: Action) {
onAction?(action)
let effects = Reducer.reduce(state: &state, action: action)
state.rebuildCards()
for effect in effects {
Expand Down
Loading