Cohorts are one of Mixpanel's most powerful features for understanding user behavior by segment. Instead of analyzing all users together, cohorts let you group by behavior or attributes—paying customers vs free users, onboarded vs not, early adopters vs late movers. To unlock cohort analysis, you need consistent event tracking and user properties flowing into Mixpanel. Here's how to set it up.
1. Instrument Event Tracking and User Properties
Cohorts are built on data. Before you can create a meaningful cohort, your app needs to send events and user attributes to Mixpanel consistently.
Initialize the SDK and identify users
Start by initializing Mixpanel with your token and calling mixpanel.identify() with a stable user ID. This ties all events to the right person. Without a consistent identity, Mixpanel treats each session as a new user and cohort analysis falls apart.
import mixpanel from 'mixpanel-browser';
// Initialize once on app load
mixpanel.init('YOUR_MIXPANEL_TOKEN');
// After user signs in, identify them
const userId = user.id; // from your auth system
mixpanel.identify(userId);
// Optional: set a display name
mixpanel.people.set({
'$name': user.email,
'$email': user.email
});Track events that define cohort membership
Log events for behaviors you want to segment by. Common examples: 'Signup', 'First Purchase', 'Completed Onboarding', 'Invited Team Member'. Each event becomes a potential cohort rule. Always include useful properties like plan type, source, or completion time to add context to the event.
// Track signup with context
mixpanel.track('Signup', {
plan: 'starter',
source: 'google',
country: 'US'
});
// Track purchase
mixpanel.track('Purchase', {
amount: 99.99,
currency: 'USD',
product_id: 'plan-annual'
});
// Track onboarding milestone
mixpanel.track('Completed Onboarding', {
steps_completed: 5,
time_taken_seconds: 1200
});Set persistent user properties for demographic cohorts
Use mixpanel.people.set() to store attributes that don't change often—plan tier, company size, account status. These properties persist across sessions and become filter options in the Segmentation > Cohorts builder. Unlike events, properties are singular snapshots of user state.
// Set user properties after signup or profile update
mixpanel.people.set({
'Plan': 'enterprise',
'Annual Revenue': 2000000,
'Team Size': 45,
'Account Status': 'active',
'Industry': 'SaaS',
'Signup Date': new Date().toISOString(),
'Is Trial': false
});
// Increment a counter
mixpanel.people.increment('Logins', 1);
// Append to a list
mixpanel.people.append('Features Used', 'Cohorts');Verify events and properties in Mixpanel
Log in to Mixpanel and check Events > Live View to see events flowing in real-time. Click a user in Users > User Search to verify their properties are recorded. Properties take a few seconds to appear—don't assume they failed if they're not instant.
// Debug: Log to browser console and Mixpanel simultaneously
console.log('Tracking event:', 'Purchase');
mixpanel.track('Purchase', {
amount: 99.99,
debug: true
});
// Verify the user profile
console.log('User ID:', mixpanel.get_distinct_id());
// Check if Mixpanel loaded
if (window.mixpanel) {
console.log('Mixpanel initialized');
}2. Create and Apply Cohorts
Once your events and properties are flowing, build cohorts in the Mixpanel UI, then use them to analyze behavior differences between user groups.
Build a cohort from an event
Go to Segmentation > Cohorts > Create Cohort. Choose Event Based and select an event (e.g., 'First Purchase'). Set a date range—users who triggered the event in the last 30 days, for example. Name it something descriptive like 'Paid Customers (Last 30 Days)'. Event-based cohorts answer the question: who took this action?
// Example: Query cohort membership via the Mixpanel API
// Use this in your backend to fetch cohort IDs
const getCohorts = async () => {
const response = await fetch(
'https://data.mixpanel.com/api/2.0/cohorts',
{
method: 'GET',
headers: {
'Authorization': `Basic ${Buffer.from('YOUR_SERVICE_ACCOUNT:PASSWORD').toString('base64')}`
}
}
);
const data = await response.json();
console.log(data.results); // Array of cohorts with IDs and names
};
getCohorts();Build a cohort from user properties
Choose Property Based to segment by user attributes. For example: Plan = 'enterprise' AND Team Size > 10. You can combine multiple conditions with AND/OR. This is useful for demographic or account-level cohorts that don't depend on a specific event. Property-based cohorts answer: who has this attribute right now?
// Property-based cohort logic (example structure)
const cohortRule = {
operator: 'and',
conditions: [
{
property: 'Plan',
operator: 'eq',
value: 'enterprise'
},
{
property: 'Team Size',
operator: 'gte',
value: 10
},
{
property: 'Account Status',
operator: 'eq',
value: 'active'
}
]
};
// This matches all users with enterprise plan, 10+ team members, and active statusUse cohorts in reports to compare behavior
Once a cohort is created, open Segmentation and select your cohort as a filter. Compare metrics across groups: Is the paid cohort more engaged? Do enterprise users have higher retention? You can also Group by Cohort to see differences in funnel conversion or engagement across multiple cohorts at once.
// Backend: Compare retention between cohorts
const compareRetention = async (cohortId) => {
const response = await fetch(
`https://data.mixpanel.com/api/2.0/retention?from_date=2025-01-01&to_date=2025-03-31&born_where=cohort_id%3D${cohortId}`,
{
headers: {
'Authorization': `Basic ${Buffer.from('YOUR_SERVICE_ACCOUNT:PASSWORD').toString('base64')}`
}
}
);
const retention = await response.json();
// retention.data shows: Day 0, Day 1, Day 7, Day 30 retention
console.log('Day 0:', retention.data[0].values[0]);
console.log('Day 30:', retention.data[30].values[0]);
};
compareRetention('123456');Common Pitfalls
- Skipping
mixpanel.identify()or using inconsistent user IDs. Without a stable identity, Mixpanel treats sessions as separate users and cohorts won't aggregate correctly. - Mixing up event-based and property-based cohorts. Event-based finds users who triggered an action; property-based finds users with a specific attribute. They answer different questions.
- Expecting cohorts to update in real-time. Mixpanel calculates cohorts daily, so changes to user properties or event history take up to 24 hours to reflect.
- Using inconsistent property names (e.g., 'Plan' vs 'plan' vs 'pricing_tier'). Mixpanel is case-sensitive and treats these as separate fields.
Wrapping Up
Cohort analysis in Mixpanel turns raw events into actionable segments. Track events and properties consistently, build cohorts in the UI, then use them to find behavior differences across user groups. Use cohorts to drive retention, feature adoption, and revenue insights. If you want to track this automatically across tools, Product Analyst can help.