Mixpanel Swift SDK

repository·master·Indexed 19 days ago

https://github.com/mixpanel/mixpanel-swift

An open-source SDK for iOS applications to track user behavior, send event data, and manage user profiles. Supports installation via Swift Package Manager, CocoaPods, and Carthage.

Tokens
706
Snippets
6
Records
8
Agent score
17%

What's inside mixpanel-swift

  1. Install Mixpanel Swift via CocoaPods

    master

    To install using CocoaPods:

    1. Install CocoaPods if not already installed: gem install cocoapods.
    2. Run pod setup to create a local spec mirror.
    3. Initialize a Podfile in your project directory with pod init.
    4. Add pod 'Mixpanel-swift' to your Podfile.
    5. Run pod install in your project directory.
    6. Open the resulting .xcworkspace file.
    pod 'Mixpanel-swift'
  2. Initialize Mixpanel

    master

    Import Mixpanel and initialize it within your AppDelegate.swift inside the application(_:didFinishLaunchingWithOptions:) method. You will need your project token from your Mixpanel project settings.

    Note: Setting trackAutomaticEvents to false prevents the SDK from automatically collecting common mobile events.

    import Mixpanel
    
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        ...
        Mixpanel.initialize(token: "MIXPANEL_TOKEN", trackAutomaticEvents: false)
        ...
    }
  3. Track events with Mixpanel

    master

    Use Mixpanel.mainInstance().track(event:properties:) to send event data from anywhere in your application. You can include a dictionary of properties to provide additional context for the event.

    Mixpanel.mainInstance().track(event: "Sign Up", properties: [
       "source": "Pat's affiliate site",
       "Opted out of email": true
    ])
  4. Enable Mixpanel debugging and logging

    master

    If events are not appearing in your Mixpanel dashboard, you can enable debug logging to see the library's output by setting loggingEnabled to true on the main instance.

    Mixpanel.mainInstance().loggingEnabled = true
  5. Force data upload with flush()

    master

    The Mixpanel library batches events and sends them every 60 seconds or when the app moves to the background to preserve battery and bandwidth. If you need to ensure data is sent immediately, call flush() manually.

    Mixpanel.mainInstance().flush()