iOS (Swift) SDK

Overview

The MetaRouter iOS SDK allows you to capture analytics events from your Swift-based application and route them through your MetaRouter infrastructure. It uses the open-source, MIT-licensed Segment Analytics Swift SDK to handle event collection and delivery.

Rather than maintaining a separate tracking system, MetaRouter leverages Segment’s SDK to efficiently send batched events to your MetaRouter cluster. From there, events are forwarded to the downstream integrations configured within your MetaRouter pipeline.

The SDK is built for performance and reliability. It queues events locally, batches them efficiently, and sends data asynchronously to ensure tracking has minimal impact on your app’s responsiveness.

Installation Instructions

1. Install the Segment Analytics Swift SDK

Follow the official Segment installation guide here:
https://segment.com/docs/connections/sources/catalog/libraries/mobile/apple/

This SDK is based on Segment’s open-source analytics-swift library. At the time of writing, the latest supported version is 1.7.3.

2. Create AnalyticsManager

The Segment Analytics Swift SDK requires you to manage your own singleton instance of the Analytics client. We recommend creating a shared resource using a singleton to manage this client.

import Segment

final class AnalyticsManager {
    static let shared = AnalyticsManager()

    let analytics: Analytics

    private init() {
        let configuration = Configuration(writeKey: "{{METAROUTER_WRITE_KEY}}")
            .trackApplicationLifecycleEvents(true)
            .flushInterval(10)
            .apiHost("{{METAROUTER_INGESTION_ENDPOINT}}")
        
        analytics = Analytics(configuration: configuration)
    }
}

Replace the placeholders with values from your MetaRouter configuration:

  • {{METAROUTER_WRITE_KEY}} – The write key associated with the MetaRouter pipeline that should receive the events. This must be a string.
  • {{METAROUTER_INGESTION_ENDPOINT}} – The ingestion path for your MetaRouter instance (e.g., demo-prod.mr-in.com/v1). Do not include the protocol (https://), but make sure to include the /v1 path suffix.

You can find additional configuration options in Segment’s Swift documentation.

⚠️

Upon initialization, you may see a failed request to cdn-settings.segment.com. This is expected—Segment attempts to fetch remote settings by default. These settings are optional and do not affect the ability of the SDK to send events to MetaRouter.

3. Call Identify, Track, Screen or Group Events

You can now use the standard methods provided by the Segment Analytics Swift SDK to send data to your MetaRouter instance. The MetaRouter configuration supports all standard event types from the SDK, including identify, track, screen, and group.

Refer to Segment’s implementation guide for details:
https://segment.com/docs/connections/sources/catalog/libraries/mobile/apple/implementation/

let analytics = AnalyticsManager.shared.analytics

// Identify a known user
analytics.identify(userId: "[email protected]", traits: [
  "name": "John Doe",
  "plan": "Premium"
])

// Track a custom event
analytics.track(name: "Submit Button Clicked")

// Record a screen view
analytics.screen(title: "Checkout", properties: [
  "step": 2,
  "paymentMethod": "Apple Pay"
])

// Associate a user with a group (e.g., company or team)
analytics.group(groupId: "team_456", traits: [
  "name": "Acme Corp",
  "industry": "E-commerce"
])

You can find full API documentation and usage patterns at:
https://segment.com/docs/connections/sources/catalog/libraries/mobile/apple/