• Stars
    star
    395
  • Rank 109,040 (Top 3 %)
  • Language
    Swift
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Official iOS (Swift) Tracking Library for Mixpanel Analytics

Mixpanel Swift Library

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

Overview

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.

Check out our Advanced iOS Swift Guide for additional advanced configurations and use cases, like setting up your project with European Union data storage.

Quick Start Guide

Our master branch and our releases are 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.

1. Install Mixpanel

You will need your project token for initializing your library. You can get your project token from project settings.

Installation Option 1: Swift Package Manager (v2.8.0+)

The easiest way to get Mixpanel into your iOS project is to use Swift Package Manager.

  1. In Xcode, select File > Add Packages...
  2. Enter the package URL for this repository Mixpanel Swift library.

Installation Option 2: CocoaPods

  1. If this is your first time using CocoaPods, Install CocoaPods using gem install cocoapods. Otherwise, continue to Step 3.
  2. Run pod setup to create a local CocoaPods spec mirror.
  3. Create a Podfile in your Xcode project directory by running pod init in your terminal, edit the Podfile generated, and add the following line: pod 'Mixpanel-swift'.
  4. Run pod install in your Xcode project directory. CocoaPods should download and install the Mixpanel library, and create a new Xcode workspace. Open up this workspace in Xcode or typing open *.xcworkspace in your terminal.

Installation Option 3: Carthage

Mixpanel 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.

2. Initialize Mixpanel

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

import Mixpanel

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

See all configuration options

3. Send Data

Let's get started by sending event data. You can send an event from anywhere in your application. Better understand user behavior by storing details that are specific to the event (properties). After initializing the library, Mixpanel will automatically collect common mobile events. You can enable/disable automatic collection through your project settings. Also, Mixpanel automatically tracks some properties by default. learn more

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

In addition to event data, you can also send user profile data. We recommend this after completing the quickstart guide.

4. Check for Success

Open up Events in Mixpanel to view incoming events.

Once data hits our API, it generally takes ~60 seconds for it to be processed, stored, and queryable in your project.

๐Ÿ‘‹ ๐Ÿ‘‹ Tell us about the Mixpanel developer experience! https://www.mixpanel.com/devnps ๐Ÿ‘ ๐Ÿ‘Ž

Complete Code Example

Here's a runnable code example that covers everything in this quickstart guide.

import Mixpanel

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    ...
    Mixpanel.initialize(token: "MIXPANEL_TOKEN", trackAutomaticEvents: false)
    Mixpanel.mainInstance().track(event: "Sign Up", properties: [
       "source": "Pat's affiliate site",
       "Opted out of email": true
    ])
    ...
}

FAQ

I want to stop tracking an event/event property in Mixpanel. Is that possible?

Yes, in Lexicon, you can intercept and drop incoming events or properties. Mixpanel wonโ€™t store any new data for the event or property you select to drop. See this article for more information.

I have a test user I would like to opt out of tracking. How do I do that?

Mixpanelโ€™s client-side tracking library contains the optOutTracking() method, which will set the userโ€™s local opt-out state to โ€œtrueโ€ and will prevent data from being sent from a userโ€™s device. More detailed instructions can be found in the section, Opting users out of tracking.

Why aren't my events showing up?

First, make sure your test device has internet access. To preserve battery life and customer bandwidth, the Mixpanel library doesn't send the events you record immediately. Instead, it sends batches to the Mixpanel servers every 60 seconds while your application is running, as well as when the application transitions to the background. You can call flush() manually if you want to force a flush at a particular moment.

Mixpanel.mainInstance().flush()

If your events are still not showing up after 60 seconds, check if you have opted out of tracking. You can also enable Mixpanel debugging and logging, it allows you to see the debug output from the Mixpanel library. To enable it, set loggingEnabled to true.

Mixpanel.mainInstance().loggingEnabled = true

Starting with iOS 14.5, do I need to request the userโ€™s permission through the AppTrackingTransparency framework to use Mixpanel?

No, Mixpanel does not use IDFA so it does not require user permission through the AppTrackingTransparency(ATT) framework.

If I use Mixpanel, how do I answer app privacy questions for the App Store?

Please refer to our Apple App Developer Privacy Guidance

I want to know more!

No worries, here are some links that you will find useful:

Have any questions? Reach out to Mixpanel Support to speak to someone smart, quickly.

More Repositories

1

mixpanel-iphone

Official iOS (Objective-C) Tracking Library for Mixpanel Analytics
Objective-C
1,027
star
2

mixpanel-android

Official Android Tracking Library for Mixpanel Analytics
Java
1,017
star
3

mixpanel-js

Official Mixpanel JavaScript Client Library
JavaScript
823
star
4

mixpanel-node

A node.js API for mixpanel
JavaScript
463
star
5

panel

Web Components + Virtual DOM: web standards for powerful UIs
JavaScript
273
star
6

mixpanel-ruby

Ruby
170
star
7

mixpanel-php

PHP
134
star
8

mixpanel-react-native

Official React Native Tracking Library for Mixpanel Analytics
JavaScript
103
star
9

mixpanel-python

Official Mixpanel Python library.
Python
101
star
10

mixpanel-utils

Python
80
star
11

mixpanel-flutter

Official Flutter Tracking Library for Mixpanel Analytics
Dart
66
star
12

sample-android-mixpanel-integration

Java
55
star
13

tracking-proxy

One-click configuration to proxy tracking requests to Mixpanel's API
Dockerfile
49
star
14

mixpanel-java

Java
48
star
15

mixpanel-unity

Official Unity Tracking Library for Mixpanel Analytics
C#
37
star
16

docs

Mixpanel's Official Documentation
MDX
35
star
17

fuzzbunny

Fast fuzzy string searching/matching/highlighting
JavaScript
19
star
18

panel-farm

Manage cute animals with Web Components and Virtual DOM very wow
JavaScript
10
star
19

snabbdom-jsx-lite

Write snabbdom templates in .jsx or .tsx (JSX for TypeScript)
TypeScript
10
star
20

flask-tracking-proxy

Example Python Flask application to proxy tracking requests to Mixpanel's API
Python
10
star
21

configmanager

A library for adding dynamic configuration to your code
Go
9
star
22

webcomponent

Lightweight utilities for constructing web components
JavaScript
8
star
23

webpack-dev-server-status-bar

Unobtrusive HTML status indicator for Webpack compilation status
JavaScript
7
star
24

mixpanel-gtm-template

A GitHub project created under the Mixpanel organization to store the Mixpanel GTM template
Smarty
6
star
25

obs

Opinionated observability package that combines logging, tracing and metrics.
Go
6
star
26

mixpanel-go

Mixpanel Official Go SK
Go
4
star
27

framesg

request/response communication to/from iframes
JavaScript
4
star
28

sheets

๐Ÿ”„ integrate mixpanel with google sheets
JavaScript
3
star
29

mixpanel-chrome-extension

This extension will inject the Mixpanel library into every page you visit
JavaScript
3
star
30

domsuite

JavaScript browser testing/automation utilities with async/await
JavaScript
2
star
31

mixpanel-js-wrapper

A GitHub project created under the Mixpanel organization to store the Mixpanel JS wrapper
JavaScript
2
star
32

django-prefetch-example

Python
1
star
33

dedupe-chunks-webpack-plugin

(make public later)
JavaScript
1
star