Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

Analytics-Swift for iOS and Apple


Community x
Maintenance x
Flagship

With Analytics-Swift, you can send data from iOS, tvOS, iPadOS, WatchOS, macOS, and Linux applications to any analytics or marketing tool without having to learn, test, or implement a new API every time. Analytics-Swift is compatible with both Swift and Objective-C applications.

(warning)

Warning

If you're migrating to Analytics-Swift from Analytics iOS (Classic), you can skip to the migration guide.


Benefits of Analytics-Swift

benefits-of-analytics-swift page anchor

Analytics-Swift provides several key benefits including improvements in stability, performance, and developer experience when compared to Analytics iOS (Classic).

Performance

performance page anchor

Analytics-Swift offers improved performance when compared to Analytics iOS:

  • Faster event processing and delivery
  • Significantly lower CPU usage
  • Small memory and disk usage footprint

Analytics-Swift adds several improvements to the overall experience of using the core SDK, as well as improvements to the overall Plugin Architecture.

  • Ability to use Type Safe data structures rather than just dictionaries.
  • Simpler syntax and more developer friendly overall.
  • More customization options than ever before.

Device Mode Transformations and Filtering

device-mode-transformations-and-filtering page anchor

For the first time ever, developers can filter and transform their users' events even before the events leave the mobile device. What's more, these Filters & transformations can be applied dynamically (either through the Segment Dashboard, or JavaScript uploaded to the workspace) and do not require any app updates.

Learn more about Destination Filters(link takes you to an external page) on Mobile, and Edge Functions(link takes you to an external page) on Mobile.


(information)

Multiple Instances

Multiple Instances are supported as part of the Analytics-Swift mobile library. However, each instance must have a unique writeKey defined, or malformed JSON may be sent to our API resulting in 400 errors.

To get started with the Analytics-Swift mobile library:

  1. Create a Source in Segment.

    1. Go to Connections > Sources > Add Source.
    2. Search for Apple and click Add source.
  2. Add the Analytics dependency to your application. Add the Swift package, git@github.com:segmentio/analytics-swift.git as a dependency through either of these two options:

    1. Your package.swift file
    2. Xcode
      1. Xcode 12: File > Swift Packages > Add Package Dependency
      2. Xcode 13: File > Add Packages…

    After installing the package, you can reference Analytics-Swift by importing Segment's Analytics package with import Segment.

  3. Initialize and configure the Analytics-Swift client. For example, in a lifecycle method such as didFinishLaunchingWithOptions in iOS:

SwiftObjective-C
1
var analytics: Analytics? = nil
2
3
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
4
// Override point for customization after application launch.
5
let configuration = Configuration(writeKey: "WRITE_KEY")
6
.trackApplicationLifecycleEvents(true)
7
.flushInterval(10)
8
9
analytics = Analytics(configuration: configuration)
10
}

These are the options you can apply to configure the client:

Option nameDescription
writeKey requiredThis is your Segment write key.
apiHostThe default is set to api.segment.io/v1.
This sets a default API Host to which Segment sends events.
autoAddSegmentDestinationThe default is set to true.
This automatically adds the Segment Destination plugin. Set to false if you want to add plugins to the Segment Destination.
cdnHostThe default is set to cdn-settings.segment.com/v1.
This sets a default CDN Host from which Segment retrieves settings.
defaultSettingsThe default is set to {}.
This is the settings object used as fallback in case of network failure.
flushAtThe default is set to 20.
The count of events at which Segment flushes events.
flushIntervalThe default is set to 30 (seconds).
The interval in seconds at which Segment flushes events.
trackApplicationLifecycleEventsThe default is set to true.
This automatically tracks lifecycle events. Set to false to stop tracking lifecycle events.
(information)

AppClip Tracking

If you are tracking App Clips using iOS or Swift libraries, you may encounter zeros in your device ID. We recommend that you set your own device ID in these instances to avoid this issue.

Once you've installed the Analytics-Swift library, you can start collecting data through Segment's tracking methods:


Destinations are the business tools or apps that Segment forwards your data to. Adding Destinations allow you to act on your data and learn more about your customers in real time.

See Segment's documentation for device-mode destinations for a full list of supported device-mode plugins.

See Segment's cloud-mode destinations for a full list of available cloud-mode destinations that Swift supports.


Segment offers support for two different types of destination connection modes: Cloud-mode and Device-mode. learn more about the differences between the two in the Segment Destination docs.


Analytics-Swift is built with extensibility in mind. Use the tools list below to improve data collection.


If you proxy your events through the apiHost config option, you must forward the batched events to https://api.segment.io/v1/b. The https://api.segment.io/v1/batch endpoint is reserved for events arriving from server-side sending, and proxying to that endpoint for your mobile events may result in unexpected behavior.

(warning)

Warning

If you're using the Analytics iOS (Classic) SDK, you can find the documentation here. Many of the features available in the Analytics-Swift SDK aren't available in the Analytics iOS (Classic) SDK.


The Analytics-Swift SDK collects telemetry data on configuration and usage by default. This includes basic information on SDK setup, plugins and event types used, and basic error details. Segment downsamples the data to minimize traffic and doesn't collect any personally identifiable information (PII) or event data.

You can disable telemetry at any time by setting Telemetry.shared.enable = false.

When internal errors or errors from plugins occur, the write key may be included with error data to help Segment identify the issue(s). You can disable this by setting Telemetry.shared.sendWriteKeyOnError = false.


Due to efficiency updates made to Segment's Swift library, Segment now adds the sentAt timestamp to an event when the batch is complete and initially tried to the Segment API. This can impact the value of the timestamp field calculated by Segment if users are operating in an offline mode. More details on this change can be seen in Segment's timestamp documentation.