Swiftpack.co - Package - mixpanel/mixpanel-swift
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

Mixpanel Swift Library

Build Status Average time to resolve an issue Percentage of issues still open CocoaPods Compatible Carthage compatible Swift Package Manager compatible Apache License Documentation

Table of Contents

Introduction

Welcome to the official Mixpanel Swift Library

The Mixpanel Swift library for iOS is an open-source project, and we'd love to see your contributions! We'd also love for you to come and work with us! Check out Jobs for details.

If you are using Objective-C, we recommend using our Objective-C Library.

Current supported features

Our master branch and our releases are now on Swift 5.

If you wish to use our Swift 4.2 implementation, please point to the v2.6.1 release. For Swift 4/4.1 implementation, please point to the v2.4.5 release. For Swift 3 implementation, please point to the v2.2.3 release. For Swift 2.3 implementation, please point to the v1.0.1 release.

Our Swift library fully supports all of the Mixpanel features and has full parity with the Objective-C Library.

Installation

Swift Package Manager

It requires Xcode 12+ and v2.8.0+ release

Important: we only have support for iOS, for now, if you need integrations for other platforms(tvOS or macOS), please use other integration methods instead

  1. In Xcode, select File > Swift Packages > Add Package Dependency.
  2. Follow the prompts using the URL for this repository and a minimum semantic version of v2.8.0

Check out our swift integration guide » for more info.

CocoaPods

Our current release only supports CocoaPods version 1.4.0+

Mixpanel supports CocoaPods for easy installation. To Install, see our swift integration guide »

For iOS, tvOS, macOS, and App Extension integrations:

pod 'Mixpanel-swift'

Carthage

Mixpanel also supports Carthage to package your dependencies as a framework. Include the following dependency in your Cartfile:

github "mixpanel/mixpanel-swift"

Check out the Carthage docs » for more info.

Manual Installation

To help users stay up to date with the latest version of our Swift SDK, we always recommend integrating our SDK via CocoaPods, which simplifies version updates and dependency management. However, there are cases where users can't use CocoaPods. Not to worry, just follow these manual installation steps and you'll be all set.

Step 1: Add as a Submodule

Add Mixpanel as a submodule to your local git repo like so:

git submodule add git@github.com:mixpanel/mixpanel-swift.git

Now the Mixpanel project and its files should be in your project folder!

Step 2: Drag Mixpanel to your project

Drag the Mixpanel.xcodeproj inside your sample project under the main sample project file:

alt text

Step 3: Embed the framework

Select your app .xcodeproj file. Under "General", add the Mixpanel framework as an embedded binary:

alt text

Initializing and Usage

Integrate

Import Mixpanel into AppDelegate.swift, and initialize Mixpanel within application:didFinishLaunchingWithOptions: alt text

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    Mixpanel.initialize(token: "MIXPANEL_TOKEN")
}

You initialize your Mixpanel instance with the token provided to you on mixpanel.com.

Start tracking

After installing the library into your iOS app, Mixpanel will automatically collect common mobile events. You can enable/ disable automatic collection through your project settings.

To interact with the instance and track additional events, you can either use the mixpanel instance given when initializing:

mixpanel.track(event: "Tracked Event!")

or you can directly fetch the instance and use it from the Mixpanel object:

Mixpanel.mainInstance().track(event: "Tracked Event!")

You're done! You've successfully integrated the Mixpanel Swift SDK into your app. To stay up to speed on important SDK releases and updates, star or watch our repository on Github.

Have any questions? Reach out to support@mixpanel.com to speak to someone smart, quickly.

Using Mixpanel Push Notifications

First enable Mixpanel push notifications in your app. Then for Rich Push Notifications, you'll need to integrate your application with the MixpanelNotificationServiceExtension.

Integrating the MixpanelNotificationServiceExtension

The Mixpanel SDK comes with a custom Notification Service Extension used to render rich media, custom action buttons, and track when push notifications are received. To enable these features, you will need to integrate it into your application.

1. Create a new Notification Service Extension Target

Screenshot 2020-02-13 14 45 23 Screenshot 2020-02-13 14 44 38 Screenshot 2020-02-13 14 46 53

2. Add Mixpanel-swift as a dependency

Add the following to the end of your Podfile:

target 'NotificationService' do
  use_frameworks!
  
  pod 'Mixpanel-swift'
end

Then run pod install

3. Extend your NotificationService from MixpanelNotificationServiceExtension

Replace the contents of your NotificationService.swift file with the following:

import Mixpanel

class NotificationService: MixpanelNotificationServiceExtension {}

4. Delegate the handling of the notification response to the Mixpanel SDK

In your AppDelegate.swift file, you need to call MixpanelPushNotifications.handleResponse when a push notification response is received:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    if MixpanelPushNotifications.isMixpanelPushNotification(response.notification.request.content) {
        debugPrint("Handling Mixpanel push notification response...")
        MixpanelPushNotifications.handleResponse(response: response, withCompletionHandler: completionHandler)
    } else {
        // not a Mixpanel push notification
        debugPrint("Not a Mixpanel push notification.")
        completionHandler()
    }
}

This will make sure the tap actions are appropriately handled (open URL, deeplink, etc) as well as track whether a notification was tapped or dismissed.

5. Run the app and send a test push notification from Mixpanel that includes an image or buttons

That's it! Your app should now be able to receive rich push notifications from Mixpanel.

Github

link
Stars: 287

Releases

v2.8.2 - Bug fixes - 2021-01-16T00:43:32

v2.8.1 - Bug fixes - 2020-11-23T21:50:56

Fix events being dropped when flushing if there are corrupted items in the event queue

v2.8.0 - Swift Package Manager support - 2020-10-15T20:23:01

Add support for Swift Package Manager

Important: v2.8.0 release only supports iOS, if you need integrations for other platforms, please use other integration methods instead, we will add support for all platforms soon

Integration steps:

  1. In Xcode, select File > Swift Packages > Add Package Dependency.
  2. Follow the prompts using the URL for this repository and a minimum semantic version of v2.8.0

v2.7.7 - Remove IFA/IDFA - 2020-08-07T23:27:56

Removing using IFA as distinct id https://github.com/mixpanel/mixpanel-swift/pull/389

Beginning with this version, Mixpanel no longer uses the IFA(ID for Advertisers) but uses a randomly generated UUID as the default distinct ID instead. After you call reset, Mixpanel generates a new distinct_id by default. This ensures that multiple users on the same device are not assigned the same alias.

If you want to use IFV(identifierForVendor) as the distinct_id, you can set MIXPANEL_UNIQUE_DISTINCT_ID in build settings Active Compilation Conditions on the Mixpanel framework target. After you call reset, the IFV will not change. However, when a user removes and then re-installs the app, the IFV will change with each installation.

v2.7.6 - Bug fixes - 2020-07-16T18:13:08

v2.7.5 - Bug fixes - 2020-07-14T20:33:42

Convert objc_sync_enter lock to GCD https://github.com/mixpanel/mixpanel-swift/pull/393

v2.7.4 - Bug fixes - 2020-07-07T00:08:02

Fix crash with CTTelephonyNetworkInfo in setCurrentRadio https://github.com/mixpanel/mixpanel-swift/pull/388

v2.7.3 - Bug fixes for flushing large event queues - 2020-05-14T18:55:47

v2.7.2 - Bug fixes - 2020-05-07T21:10:29

v2.7.1 - Several critical bug fixes - 2020-04-24T00:03:26

v2.7.0 - Rich push notifications and new push tracking events - 2020-02-13T17:13:32

v2.6.11 - Bug fixes - 2020-01-17T21:26:59

Respect checkForVariantsOnActive and checkForNotificationOnActive https://github.com/mixpanel/mixpanel-swift/pull/344

v2.6.10 - Bug fixes - 2020-01-17T19:42:02

v2.6.9 - In-app notification fixes for iOS 13 - 2020-01-10T16:06:15

v2.6.8 - Bug fixes - 2020-01-07T01:01:27

Prevent users from seeing duplicate inapps when logging out and back in https://github.com/mixpanel/mixpanel-swift/pull/337

v2.6.7 - bug fixes - 2019-10-23T01:41:03

v2.6.6 - Xcode 11 and bug fixes - 2019-10-04T01:13:22

v2.6.5 - bug fixes - 2019-08-23T07:19:24

Fix duplicate triggered inapps shown between app sessions https://github.com/mixpanel/mixpanel-swift/pull/324

v2.6.3 - Improved identity management support and bug fixes - 2019-06-07T07:15:44

mixpanel.identify() now sends a special $identify event to the API. This is not treated like a regular tracking event, but will aid in future platform improvements to support advanced identity management (linking anonymous and logged-in usage, multiple users on one system, etc.).

if more than one user can be identified in your app (ie log-out/log-in scenarios) make sure you call reset() when a user logs-out and that you add MIXPANEL_RANDOM_DISTINCT_ID Active Complilation Conditions in your build settings so anonymous users don't have the same distinct_id (IFA/IFV by default) https://github.com/mixpanel/mixpanel-swift/pull/306

fix a potential crash issue by fetching the current super properties https://github.com/mixpanel/mixpanel-swift/pull/304

fix deadlock in readwrite lock https://github.com/mixpanel/mixpanel-swift/pull/307 thanks @RamblinWreck77

Missing case in makeObjectSerializable() : NaN or infinity cannot be serialized as JSON https://github.com/mixpanel/mixpanel-swift/pull/303 thanks @tonysung

Prefix swizzled method name https://github.com/mixpanel/mixpanel-swift/pull/289 thanks @ChrisGriffiths

v2.6.2 - Xcode 10.2 and swift 5 support - 2019-04-01T19:03:03

v2.6.1 - Bug fixes - 2019-03-28T18:55:37

v2.6.0 - Apple Watch Support and Automatic Events for Apple TV - 2019-03-26T08:04:06

  • Add Apple watch support

  • Enable automatically track common mobile events($ae_session, $ae_updated, $ae_first_open) for Apple TV. to turn it off, please remove the TV_AUTO_EVENTS flag in Active Compilation Conditions in build settings. image More details about Default Mobile Events Collection: https://help.mixpanel.com/hc/en-us/articles/115004596186-Default-Mobile-Events-Collection

  • GDPR fix: Change the default behavior for setting optOutTrackingByDefault in the Mixpanel initialization method. We will not override opt out persistence with optOutTrackingByDefault the flag since opt-out default state is supposed to be used as an initial state while GDPR information is being collected https://github.com/mixpanel/mixpanel-swift/pull/290

v2.5.9 - Support for account-level analytics - 2019-03-22T08:39:14

New Features

  • Set, add, and remove groups the user belongs to.
  • Track events with specific groups
  • Support for group-level profiles—set, update, and remove properties on account/group objects in Mixpanel
  • Allow to remove All device push tokens, thanks @asiliuk for providing this PR https://github.com/mixpanel/mixpanel-swift/pull/288

Bug fix

v2.5.8 - Event triggered in-app notifications support - 2019-03-15T00:04:59

  • Add the feature: Event triggered in-app notifications support

Currently users have no control over when an in-app notification shows up. With this release users can now control when an in-app gets displayed based on an event that they track within their app. This "trigger" event is defined during message creation at www.mixpanel.com.

You can additionally filter the event based on properties that are tracked along with the event for even finer controls. https://github.com/mixpanel/mixpanel-swift/pull/287

v2.5.7 - Bug fixes - 2019-01-25T22:42:38

v2.5.6 - Bug fixes - 2019-01-11T23:55:11

v2.5.5 - Bug fixes - 2018-11-07T03:59:55

v2.5.4 - Send device_id and user_id with event/people updates - 2018-10-19T18:56:39

v2.5.3 - Bug fixes - 2018-10-14T19:12:00