Auto-Instrumentation Setup
This guide outlines the steps required to set up the Signals SDK in your Apple OS applications using Swift.
Learn how to connect an existing source, integrate dependencies, turn on Auto-Instrumentation, and verify that your setup captures and processes data as intended.
Auto-Instrumentation in public beta
Auto-Instrumentation is in public beta, and Segment is actively working on this feature. Some functionality may change before it becomes generally available.
Regional availability
Auto-Instrumentation isn't supported in EU workspaces.
You need the writeKey from an existing Segment source. To find it:
- In your Segment workspace, go to Connections > Sources.
- Select your source.
- From the source's overview tab, go to Settings > API Keys.
- Copy the
writeKeyshown in the code block.
Segment recommends testing in a development environment before deploying Signals in production. For more information, see Debug mode.
Auto-Instrumentation (also known as Signals) works on top of Analytics. Make sure to add the following dependency to your project if you don't have analytics-swift already.
1dependencies: [2.package(url: "https://github.com/segmentio/analytics-swift.git", from: "1.9.1")3]
- Add AnalyticsLive to your Swift Package dependencies:
1dependencies: [2.package(url: "https://github.com/segmentio/analytics-live-swift.git", from: "3.2.1")3]
- Import and initialize with your Analytics instance. For a complete list, see configuration options.
1import Segment2import AnalyticsLive34let analytics = Analytics(configuration: Configuration(writeKey: "YOUR_WRITE_KEY"))56// Add LivePlugins first7analytics.add(plugin: LivePlugins())89// Add Signals10analytics.add(plugin: Signals.shared)1112// Configure Signals13Signals.shared.useConfiguration(SignalsConfiguration(14writeKey: "YOUR_WRITE_KEY", // Same writeKey as Analytics15useUIKitAutoSignal: true,16useSwiftUIAutoSignal: true,17useNetworkAutoSignal: true,18#if DEBUG19// NOTE: See section below on using these flags appropriately.20sendDebugSignalsToSegment: true, // Only true for development21obfuscateDebugSignals: false // Only false for development22#endif23// ... other options24))
- Set up capture for the UI framework(s) you're using:
SwiftUI automatic signal capture requires adding typealiases to your code. This is necessary because SwiftUI doesn't provide hooks for automatic instrumentation.
- Enable SwiftUI auto-signals in your configuration:
1Signals.shared.useConfiguration(SignalsConfiguration(2writeKey: "YOUR_WRITE_KEY",3useSwiftUIAutoSignal: true4// ... other options5))
- Add the following typealiases to your SwiftUI views or in a shared file:
1import SwiftUI2import AnalyticsLive34// Navigation5typealias NavigationLink = SignalNavigationLink6typealias NavigationStack = SignalNavigationStack // iOS 16+78// Selection & Input Controls9typealias Button = SignalButton10typealias TextField = SignalTextField11typealias SecureField = SignalSecureField12typealias Picker = SignalPicker13typealias Toggle = SignalToggle14typealias Slider = SignalSlider // Not available on tvOS15typealias Stepper = SignalStepper // Not available on tvOS1617// List & Collection Views18typealias List = SignalList
- Use the controls in your SwiftUI code:
1struct ContentView: View {2var body: some View {3NavigationStack {4VStack {5Button("Click Me") {6// Button tap automatically generates a signal7}89TextField("Enter text", text: $text)10// Text changes automatically generates signals11}12}13}14}
The typealiases replace SwiftUI's native controls with signal-generating versions. Your code remains unchanged, but interactions are now automatically captured.
UIKit automatic signal capture uses method swizzling and requires no code changes.
-
Enable UIKit auto-signals in your configuration:
1Signals.shared.useConfiguration(SignalsConfiguration(2writeKey: "YOUR_WRITE_KEY",3useUIKitAutoSignal: true4// ... other options5)) -
The following UIKit interactions and navigation events are automatically captured via method swizzling:
Interactions:
UIButtontapsUISlidervalue changesUISteppervalue changesUISwitchtoggle eventsUITextFieldtext changesUITableViewCellselections
Navigation:
UINavigationControllerpush/pop operationsUIViewControllermodal presentations and dismissalsUITabBarControllertab switches
Network capture automatically tracks URLSession requests and responses.
- Enable network auto-signals in your configuration:
1Signals.shared.useConfiguration(SignalsConfiguration(2writeKey: "YOUR_WRITE_KEY",3useNetworkAutoSignal: true,4allowedNetworkHosts: ["*"], // Allow all hosts (default)5blockedNetworkHosts: [] // Block specific hosts (optional)6// ... other options7))
- Network requests made via URLSession are automatically captured, including:
- Request URL, method, headers, and body.
- Response status, headers, and body.
- Request or response correlation via request ID.
Third-party networking libraries that use URLSession underneath (like Alamofire) should work automatically. Segment API endpoints are automatically blocked to prevent recursive tracking.
You can control which network requests are tracked:
1SignalsConfiguration(2writeKey: "YOUR_WRITE_KEY",3useNetworkAutoSignal: true,4allowedNetworkHosts: ["api.myapp.com", "*.example.com"], // Only track these hosts5blockedNetworkHosts: ["analytics.google.com"] // Exclude these hosts6)
allowedNetworkHosts: Array of host patterns to track. Use"*"to allow all hosts (default).blockedNetworkHosts: Array of host patterns to exclude from tracking.
The following hosts are automatically blocked to prevent recursive tracking:
api.segment.comcdn-settings.segment.comsignals.segment.comapi.segment.buildcdn.segment.buildsignals.segment.build
By default, Signals stores captured data on the device and doesn't forward it to Segment. This process prevents unnecessary bandwidth use and helps support privacy compliance requirements.
To view captured signals in the Event Builder and create event generation rules, enable sendDebugSignalsToSegment. This setting temporarily lets the SDK send signal data to Segment while you're testing.
In addition, the SDK obfuscates signals sent to Segment by default. To view the completed data, you need to turn off obfuscateDebugSignals.
Warning
Only enable sendDebugSignalsToSegment in development environments. Avoid using sendDebugSignalsToSegment in production apps.
You can enable sendDebugSignalsToSegment and turn off obfuscateDebugSignals in one of three ways.
- Define different configurations in your project settings (for example, Debug or Release).
- Use compiler flags to control the setting:
1Signals.shared.useConfiguration(SignalsConfiguration(2writeKey: "YOUR_WRITE_KEY",3// ... other config options4#if DEBUG5sendDebugSignalsToSegment: true,6obfuscateDebugSignals: false7#else8sendDebugSignalsToSegment: false,9obfuscateDebugSignals: true10#endif11))
If you're using Firebase Remote Config or a similar feature flag system, you can dynamically control sendDebugSignalsToSegment and obfuscateDebugSignals without requiring a new app build:
1let remoteConfig = RemoteConfig.remoteConfig()23Signals.shared.useConfiguration(SignalsConfiguration(4writeKey: "YOUR_WRITE_KEY",5// ... other config options6sendDebugSignalsToSegment: remoteConfig["sendDebugSignalsToSegment"].boolValue,7obfuscateDebugSignals: remoteConfig["obfuscateDebugSignals"].boolValue8))
You can check for environment variables or launch arguments during development:
1let isDebugEnabled = ProcessInfo.processInfo.environment["SIGNALS_DEBUG"] != nil23Signals.shared.useConfiguration(SignalsConfiguration(4writeKey: "YOUR_WRITE_KEY",5// ... other config options6sendDebugSignalsToSegment: isDebugEnabled,7obfuscateDebugSignals: !isDebugEnabled8))
Next, return to the source settings to turn on Auto-Instrumentation:
- Go to Connections > Sources.
- Select the source you used in Step 1.
- From the source's overview tab, go to Settings > Advanced.
- Toggle Auto-Instrumention on.
After integrating the SDK and running your app, verify that Segment is collecting signals:
- In your Segment workspace, go to Connections > Sources and select the source you used for Auto-Instrumentation.
- In the source overview, look for the Event Builder tab. If the tab doesn't appear:
- Make sure you've installed the SDK correctly.
- Reach out to your Segment CSM to confirm that your workspace has the necessary feature flags enabled.
- If
sendDebugSignalsToSegmentis enabled, Signals appear in real time in the Event Builder as you interact with the app. - Use the app as a user would: navigate between screens, tap buttons, trigger network requests. Signals appear in real time as you interact with the app.
- In the Event Builder, find a signal and click Configure event to define a new event. After configuring the event, click Publish event rules.
Using the Signals Configuration object, you can control the destination, frequency, and types of signals that Segment automatically tracks within your application. The following table details the configuration options for Signals-Swift:
| Option | Required | Value | Description |
|---|---|---|---|
| writeKey | Yes | String | Your Segment write key. Should match your Analytics instance writeKey. |
| maximumBufferSize | No | Int | The number of signals to be kept for JavaScript inspection. This buffer is first-in, first-out. Default is 1000. |
| relayCount | No | Int | Relays every X signals to Segment. Default is 20. |
| relayInterval | No | TimeInterval | Relays signals to Segment every X seconds. Default is 60. |
| broadcasters | No | SignalBroadcaster | An array of broadcasters. These objects forward signal data to their destinations, like WebhookBroadcaster, or you could write your own DebugBroadcaster that writes logs to the developer console. SegmentBroadcaster is always added by the SDK when sendDebugSignalsToSegment is true. |
| sendDebugSignalsToSegment | No | Bool | Turns on debug mode and allows the SDK to relay Signals to Segment server. Default is false. It should only be set to true for development purposes. |
| obfuscateDebugSignals | No | Bool | Obfuscates signals being relayed to Segment. Default is true. |
| apiHost | No | String | API host for signal relay. Default is "signals.segment.io/v1". |
| useUIKitAutoSignal | No | Bool | Enables automatic UIKit signal capture via method swizzling. Default is false. |
| useSwiftUIAutoSignal | No | Bool | Enables automatic SwiftUI signal capture (requires typealiases). Default is false. |
| useNetworkAutoSignal | No | Bool | Enables automatic network signal capture for URLSession. Default is false. |
| allowedNetworkHosts | No | String | Array of host patterns to track. Use ["*"] for all hosts. Default is ["*"]. |
| blockedNetworkHosts | No | String | Array of host patterns to exclude from tracking. Default is []. |
This guide walked you through initial Signals SDK/Auto-Instrumentation setup. Next, read the Auto-Instrumentation Signals Implementation Guide, which dives deeper into Signals and offers example rules.