|
| 1 | +// |
| 2 | +// Builtins.swift |
| 3 | +// Segment |
| 4 | +// |
| 5 | +// Created by Brandon Sneed on 10/31/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension Analytics { |
| 11 | + internal static let versionKey = "SEGVersionKey" |
| 12 | + internal static let buildKey = "SEGBuildKeyV2" |
| 13 | + |
| 14 | + internal static var appCurrentVersion: String { |
| 15 | + Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "" |
| 16 | + } |
| 17 | + |
| 18 | + internal static var appCurrentBuild: String { |
| 19 | + Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "" |
| 20 | + } |
| 21 | + |
| 22 | + public func checkAndTrackInstallOrUpdate() { |
| 23 | + let previousVersion = UserDefaults.standard.string(forKey: Self.versionKey) |
| 24 | + let previousBuild = UserDefaults.standard.string(forKey: Self.buildKey) |
| 25 | + |
| 26 | + if previousBuild == nil { |
| 27 | + // Fresh install |
| 28 | + if configuration.values.trackedApplicationLifecycleEvents.contains(.applicationInstalled) { |
| 29 | + trackApplicationInstalled(version: Self.appCurrentVersion, build: Self.appCurrentBuild) |
| 30 | + } |
| 31 | + } else if let previousBuild, Self.appCurrentBuild != previousBuild { |
| 32 | + // App was updated |
| 33 | + if configuration.values.trackedApplicationLifecycleEvents.contains(.applicationUpdated) { |
| 34 | + trackApplicationUpdated( |
| 35 | + previousVersion: previousVersion ?? "", |
| 36 | + previousBuild: previousBuild, |
| 37 | + version: Self.appCurrentVersion, |
| 38 | + build: Self.appCurrentBuild |
| 39 | + ) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Always update UserDefaults |
| 44 | + UserDefaults.standard.setValue(Self.appCurrentVersion, forKey: Self.versionKey) |
| 45 | + UserDefaults.standard.setValue(Self.appCurrentBuild, forKey: Self.buildKey) |
| 46 | + } |
| 47 | + |
| 48 | + /// Tracks an Application Installed event. |
| 49 | + /// - Parameters: |
| 50 | + /// - version: The app version (e.g., "1.0.0") |
| 51 | + /// - build: The app build number (e.g., "42") |
| 52 | + public func trackApplicationInstalled(version: String, build: String) { |
| 53 | + track(name: "Application Installed", properties: [ |
| 54 | + "version": version, |
| 55 | + "build": build |
| 56 | + ]) |
| 57 | + } |
| 58 | + |
| 59 | + /// Tracks an Application Updated event. |
| 60 | + /// - Parameters: |
| 61 | + /// - previousVersion: The previous app version |
| 62 | + /// - previousBuild: The previous build number |
| 63 | + /// - version: The current app version |
| 64 | + /// - build: The current build number |
| 65 | + public func trackApplicationUpdated(previousVersion: String, previousBuild: String, version: String, build: String) { |
| 66 | + track(name: "Application Updated", properties: [ |
| 67 | + "previous_version": previousVersion, |
| 68 | + "previous_build": previousBuild, |
| 69 | + "version": version, |
| 70 | + "build": build |
| 71 | + ]) |
| 72 | + } |
| 73 | + |
| 74 | + /// Tracks an Application Opened event. |
| 75 | + /// - Parameters: |
| 76 | + /// - fromBackground: Whether the app was opened from background (true) or cold start (false) |
| 77 | + /// - url: The URL that opened the app, if any |
| 78 | + /// - referringApp: The bundle ID of the app that referred this open, if any |
| 79 | + public func trackApplicationOpened(fromBackground: Bool, url: String? = nil, referringApp: String? = nil) { |
| 80 | + var properties: [String: Any] = [ |
| 81 | + "from_background": fromBackground, |
| 82 | + "version": Self.appCurrentVersion, |
| 83 | + "build": Self.appCurrentBuild |
| 84 | + ] |
| 85 | + |
| 86 | + if let url = url { |
| 87 | + properties["url"] = url |
| 88 | + } |
| 89 | + |
| 90 | + if let referringApp = referringApp { |
| 91 | + properties["referring_application"] = referringApp |
| 92 | + } |
| 93 | + |
| 94 | + track(name: "Application Opened", properties: properties) |
| 95 | + } |
| 96 | + |
| 97 | + /// Tracks an Application Backgrounded event. |
| 98 | + public func trackApplicationBackgrounded() { |
| 99 | + track(name: "Application Backgrounded") |
| 100 | + } |
| 101 | + |
| 102 | + /// Tracks an Application Foregrounded event. |
| 103 | + public func trackApplicationForegrounded() { |
| 104 | + track(name: "Application Foregrounded") |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#if os(macOS) |
| 109 | + |
| 110 | +extension Analytics { |
| 111 | + /// Tracks an Application Hidden event (macOS only). |
| 112 | + public func trackApplicationHidden() { |
| 113 | + track(name: "Application Hidden") |
| 114 | + } |
| 115 | + |
| 116 | + /// Tracks an Application Unhidden event (macOS only). |
| 117 | + /// - Parameters: |
| 118 | + /// - version: The app version (defaults to current version) |
| 119 | + /// - build: The app build (defaults to current build) |
| 120 | + public func trackApplicationUnhidden(version: String? = nil, build: String? = nil) { |
| 121 | + track(name: "Application Unhidden", properties: [ |
| 122 | + "from_background": true, |
| 123 | + "version": version ?? Self.appCurrentVersion, |
| 124 | + "build": build ?? Self.appCurrentBuild |
| 125 | + ]) |
| 126 | + } |
| 127 | + |
| 128 | + /// Tracks an Application Terminated event (macOS only). |
| 129 | + public func trackApplicationTerminated() { |
| 130 | + track(name: "Application Terminated") |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +#endif |
0 commit comments