Mixpanel

Integrating OneSignal with Mixpanel

11001100

Mixpanel and OneSignal have joined forces to provide:

FeatureDetails
Personalized messagingBoost engagement with more contextualized messaging from OneSignal, triggered when users enter or exit Mixpanel cohorts
Real-time insightsGain a holistic view of customers by combining Mixpanel in-product user actions and OneSignal message engagement metrics
Data-driven campaignsLeverage product-level user insights for re-engaging campaigns through OneSignal

🚧

Requirements

A Paid OneSignal Account is required for this integration.

Moving Current Mixpanel Subscriptions to OneSignal

After following the below steps, your user data will be synced with OneSignal as follows:

User TypeDetails
Push SubscribersAutomatically and silently (not prompted again) moved into OneSignal upon updating the mobile app or returning to the website. New Users can subscribe through Prompting.
Email SubscribersEmails can be Imported in a one-time step to immediately message your userbase and synced overtime using our API. Details in Email Quickstart.
SMS Subscribers You can easily import your subscriber phone numbers and send text messages based on the user activity details from Mixpanel.
28802880

Make sure you have added the OneSignal Mobile SDK and/or Web SDK and have started collecting Subscribers.

Step 1. Turn on Integration

Navigate to your onesignal.com Dashboard > Settings > Analytics and Activate Mixpanel

25822582

Step 2. Add Mixpanel Token in OneSignal dashboard

You can find your Mixpanel Token under Project Settings > Access Keys. Copy-paste the Project Token into OneSignal. See Mixpanel's "Find Project Token" guide for further details.

Recommended: Select the checkbox to "Use external_user_id to sync events" if you want to send cross-channel messages (push, email, sms, in-app).

If you are using Mixpanel EU servers, select Send events exclusively to Mixpanel EU servers.

856856

OneSignal messaging events will automatically show up in the activity feed of a user in Mixpanel.

Step 3. Add OneSignal to Mixpanel Integrations

Get your OneSignal App ID and REST API Key

In your mixpanel.com dashboard > Data Management > Integrations. Select "OneSignal" and press Connect.

27582758

With your OneSignal App ID & REST API Keys input them into the Mixpanel fields and press Continue.

10841084

Step 4. Send Mixpanel the OneSignal User Id

Syncing user data across OneSignal and Mixpanel requires setting the Mixpanel User Property called OneSignal User Id ($onesignal_user_id).

Option 1: External User Id (Recommended)

You must use this option if you plan to use multi-channel: Push, Email, In-App Messages, and SMS.

In OneSignal, if you selected Use external_user_id to sync events (recommended) in step 2, then the Mixpanel OneSignal User Id property needs to equal the OneSignal External User Id property.

OneSignal external_user_id is a user-level identifier and can be associate across different channels (Push, Email, In-App Messages, and/or SMS). If you have another 3rd party Database/DMP User ID that you use in Mixpanel, you can set that to OneSignal's external_user_id as well.

//Get the Mixpanel Distinct Id or any User Id of choice
let distinct_id = "";
mixpanel.init('YOUR PROJECT TOKEN', {
    loaded: function(mixpanel) {
        distinct_id = mixpanel.get_distinct_id();
    }
});
//Set the OneSignal External User ID to Mixpanel Distinct Id
OneSignal.push(function() {
  OneSignal.setExternalUserId(distinct_id);
  //Set Mixpanel OneSignal User Id property to Distinct Id or any User Id
  mixpanel.people.set({
    $onesignal_user_id: distinct_id
  });
});
//Get the Mixpanel Distinct Id or any User Id of choice
MixpanelAPI mixpanel = MixpanelAPI.getInstance(context, "MIXPANEL_PROJECT_TOKEN");

String distinctId = mixpanel.getDistinctId();

// Setting External User Id with Callback Available in SDK Version 4.0.0+
OneSignal.setExternalUserId(distinctId, new OneSignal.OSExternalUserIdUpdateCompletionHandler() {
   @Override
   public void onSuccess(JSONObject results) {
      // The results will contain push and email success statuses
      OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "Set external user id done with results: " + results.toString());
     //Set Mixpanel OneSignal User Id property to Distinct Id or any User Id
     mixpanel.getPeople().set("$onesignal_user_id", distinctId);
   }
   @Override
   public void onFailure(OneSignal.ExternalIdError error) {
      // The results will contain push and email failure statuses
      // Use this to detect if external_user_id was not set and retry when a better network connection is made
      OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "Set external user id done with error: " + error.toString());
   }
});
//Get the Mixpanel Distinct Id or any User Id of choice
Mixpanel.initialize(token: "MIXPANEL_PROJECT_TOKEN")
let mixpanel = Mixpanel.mainInstance()

let distinctId = mixpanel.distinctId

// Setting External User Id with Callback Available in SDK Version 3.x.x
OneSignal.setExternalUserId(distinctId, withSuccess: { results in
  // The results will contain push and email success statuses
  print("External user id update complete with results: ", results!.description)
  //Set Mixpanel OneSignal User Id property to Distinct Id or any User Id
  mixpanel.people.set(properties: ["$onesignal_user_id":userId])
})
//Get the Mixpanel Distinct Id or any User Id of choice
const distinctId = await mixpanel.getDistinctId(); 

OneSignal.setExternalUserId(distinctId, (results) => {
  // The results will contain push and email success statuses
  console.log('Results of setting external user id');
  console.log(results);
  
  //Set Mixpanel OneSignal User Id property to Distinct Id or any User Id
  mixpanel.getPeople().set("$onesignal_user_id", distinctId);
  
  // Push can be expected in almost every situation with a success status, but
  // as a pre-caution its good to verify it exists
  if (results.push && results.push.success) {
    console.log('Results of setting external user id push status:');
    console.log(results.push.success);
  }
  
  // Verify the email is set or check that the results have an email success status
  if (results.email && results.email.success) {
    console.log('Results of setting external user id email status:');
    console.log(results.email.success);
  }
});

Option 2: Player Id

Only available for push and in-app messages. Not available for omni-channel: Email & SMS.

If you did not select Use external_user_id to sync events (recommended), then use the OneSignal Player Id as the OneSignal User Id Mixpanel property. However, you will not get access to Email capabilities.

Through the Mixpanel API, set this OneSignal User ID Property $onesignal_user_id to the people property.

//Set the OneSignal Push Player ID to Mixpanel record
OneSignal.push(function() {
  // Check if the current site visitor is subscribed (no player id is created until the user subscribes to push
  OneSignal.isPushNotificationsEnabled(function(isEnabled) {
    if (isEnabled) {
      console.log("Push notifications are enabled!");
      OneSignal.getUserId(function (userId) {
        console.log("OneSignal User ID:", userId);
        mixpanel.people.set({
          $onesignal_user_id: userId
        });
      });
    } else {
      console.log("Push notifications are not enabled yet.");   
    }
  });
});
//Set the OneSignal Push Player ID to Mixpanel record
OSDeviceState device = OneSignal.getDeviceState();
String userId = device.getUserId();//String: the OS Player Id or null if device has not registered with OS Servers
mixpanel.getPeople().set("$onesignal_user_id", userId);
//Set the OneSignal Push Player ID to Mixpanel record
let deviceState = OneSignal.getDeviceState()
if let userId = deviceState?.userId {
    mixpanel.people.set(properties: ["$onesignal_user_id":userId])
}

🚧

Highly Important

User profiles without this user property ($onesignal_user_id) will not export to OneSignal - it is a requirement for user matching.

🚧

Migrating from Player Id to External User Id

  1. In OneSignal, select Use external_user_id to sync events (recommended) in step 2, then the Mixpanel OneSignal User Id property needs to equal the OneSignal External User Id property.

  2. Send Mixpanel the External User Id set within OneSignal. See example code.

Step 5. Export Mixpanel Cohorts to OneSignal

As you start sending Mixpanel the $onesignal_user_id you will see a new User Property in Mixpanel called OneSignal User Id. You can use is set to filter all devices currently associated with the OneSignal Device Record.

11461146

In Mixpanel Users > Cohorts select the 3-dot option next to the cohort you want to send push.

Select Export to... > OneSignal

14491449

Under "Export Type" you have 2 options:

  1. "One-time export" will send OneSignal the current user data. Best if sending one message only to the current users.
  2. "Dynamic Sync" is where Mixpanel will send OneSignal the updated cohort data around every 15 minutes. Best if sending recurring messages to updated user list.

Select the option and press Begin Sync.

10761076
  1. The exported Mixpanel Cohorts will show in the onesignal.com dashboard Audience > Segments as a new segment and will become available as a segment filter for further customization.
10341034 23002300

Why am I not seeing all my cohort users inside the OneSignal segment?

There are generally 3 reasons why your Mixpanel Cohort Users will not all show up inside a OneSignal Segment:

  1. The OneSignal player_id or external_user_id must be synced. Make sure you add the $onesignal_user_id to the Mixpanel user record through our SDKs. See Step 4.
  2. The device must be subscribed. Device records with an active $onesignal_user_id in Mixpanel may not be subscribed to push notifications or emails. Any device not subscribed will not be inside the OneSignal Segment.
  3. Dynamic vs One-time export. Syncing occurs every 15 minutes for Dynamic exports. You may need to wait to see all the data sync over.

You can verify this in the Mixpanel Cohort filtering All Users where OneSignal User Id is set.

🚧

Cohort - Segment User Discrepancies

Users within Mixpanel Cohorts sent to OneSignal must satisfy 2 requirements:

  1. the $onesignal_user_id property must be set on the device. See step 4.
  2. the device must be subscribed to push. OneSignal Segments only contain subscribed users.

OneSignal will also automatically sync the $first_name and $last_name user info from Mixpanel user records to our Data Tags if they are present. This is helpful for Message Personalization.

15061506

Step 6. Tracking Message Data in Mixpanel

In Mixpanel User Activity Feed, OneSignal Message Events will appear in activity feed like:

  • Message Sent
  • App Opened from Push (clicked event)

Those have properties
time - the time the event happened
delivery_id - OneSignal notification ID
campaign_id - the heading from the notification or title from the IAM
$source - this is always onesignal
message_type - push/in-app/email/sms

15061506

Cohort Events

You can use the following events to create cohorts in Mixpanel:

OneSignal EventMixpanel EventMixpanel Message Event Property
Push Notification Clicked Which Opens the App/WebsiteApp Opened from PushMessage = Push Notification Title/Headings

Message Contents = Push Notification Message/Contents
Push Notification Sent from OneSignalMessage SentMessage = Push Notification Title/Headings

Message Contents = Push Notification Message/Contents
Push Notification Received on Device (Confirmed Deliveries)Message ReceivedMessage = Push Notification Title/Headings

Message Contents = Push Notification Message/Contents
In-App Message ClickedMessage OpenedMessage Name set within OneSignal Dashboard - does not track what was clicked. Use the Click Action ID to send event to Mixpanel.
In-App Message Impression (shown to user)Message SentMessage Name set within OneSignal Dashboard
Email sent from OneSignalMessage SentMessage = Subject of the email sent from OneSignal

Message Contents = first 255 bytes of the email body
Email opened/viewed from OneSignalMessage OpenedMessage = Subject of the email sent from OneSignal

Message Contents = first 255 bytes of the email body
SMS sent from OneSignalMessage SentMessage = Contents of the SMS sent from OneSignal

Message Contents = Contents of the SMS sent from OneSignal

Those have properties
time - the time the event happened
delivery_id - OneSignal notification ID
campaign_id - the heading from the notification or title from the IAM
$source - this is always onesignal.

Example Cohorts

Did not click a push or in-app message

With this type of Cohort you can track devices that may not have received your push or in-app message to send them an email or sms text.

Checking the users that did get a Message Sent greater than 1 time in the Last Day, where Message Type equals to push.
AND
Users that Did Not have the App Opened from Push (works on web as well) in the Last day.

13381338

Were Shown a Specific Message in the Past Day

Using Message Sent at least 1 time, in the Last 7 days where Message Type equals in-app and Message equals [name of the message]

In this cast the [name of the message] is pulled from:

  • Push Notification Title
  • In-App Message Name
26822682

FAQ

Do unsubscribed devices within my Mixpanel Cohort sync to OneSignal?

Devices not subscribed to push notification or emails will not show in the OneSignal segment's device count. However, sending In-App Messages will reach the unsubscribed devices.

Can I change the cohort name and have that reflected in OneSignal?

Changing the name of a synced Mixpanel Cohort will not show in OneSignal.

Upon syncing cohorts into OneSignal Segments, 2 things happen:

  1. a OneSignal Segment is created with that cohort name
  2. a Segment Data Filter is created for that cohort

The OneSignal Segment name can be changed, but the Segment Data Filter will always reflect the original name of the Mixpanel Cohort.

If you want to reflect this change, you will need to create a new Cohort in Mixpanel and sync it to OneSignal with the new name.


What’s Next