Installation
gem install mixpanel-ruby
Initialize
require 'mixpanel-ruby'
tracker = Mixpanel::Tracker.new("YOUR_PROJECT_TOKEN")
Track Events
You must provide a distinct_id for all events.
tracker.track('12345', 'Purchase', {
'product' => 'Premium Subscription',
'amount' => 49.99,
'currency' => 'USD'
})
Import Historical Events
For events older than 5 days:
tracker.import(
"API_KEY",
"12345",
"Old Purchase",
{
'amount' => 100,
'time' => 1369353600
}
)
User Profiles
tracker.people.set('12345', {
'name' => 'John Doe',
'$email' => '[email protected]',
'plan' => 'Premium'
}, ip = 0) # Disable geolocation
set_once()
increment()
append()
union()
tracker.people.set_once('12345', {
'First Purchase' => '2024-01-01'
}, ip = 0)
tracker.people.increment('12345', {
'login_count' => 1,
'age' => 1
}, ip = 0)
tracker.people.append('12345', {
'transactions' => 'purchase_123'
}, ip = 0)
tracker.people.union('12345', {
'skills' => ['Ruby', 'Rails']
}, ip = 0)
Group Analytics
Send Events
tracker.track('12345', 'Feature Used', {
'company' => 'Acme Inc',
'feature' => 'Reports'
})
Set Group Properties
tracker.groups.set('company', 'Acme Inc', {
'name' => 'Acme Inc',
'industry' => 'Technology',
'employees' => 500
})
set_once()
unset()
union()
remove()
tracker.groups.set_once('company', 'Acme Inc', {
'founded' => '2010-01-01'
})
tracker.groups.unset('company', 'Acme Inc', 'temp_property')
tracker.groups.union('company', 'Acme Inc', {
'features' => ['Reports', 'Analytics']
})
tracker.groups.remove('company', 'Acme Inc', {
'features' => ['Beta Feature']
})
Privacy Controls
EU Data Residency
require 'mixpanel-ruby'
eu_consumer = Mixpanel::Consumer.new(
'https://api-eu.mixpanel.com/track',
'https://api-eu.mixpanel.com/engage',
'https://api-eu.mixpanel.com/groups'
)
tracker = Mixpanel::Tracker.new(YOUR_PROJECT_TOKEN) do |type, message|
eu_consumer.send!(type, message)
end
India Data Residency
in_consumer = Mixpanel::Consumer.new(
'https://api-in.mixpanel.com/track',
'https://api-in.mixpanel.com/engage',
'https://api-in.mixpanel.com/groups'
)
tracker = Mixpanel::Tracker.new(YOUR_PROJECT_TOKEN) do |type, message|
in_consumer.send!(type, message)
end
Disable Geolocation
# Set ip to 0 for events
tracker.track('12345', 'event', {
'property' => 'value'
}, ip = 0)
# Set ip to 0 for profiles
tracker.people.set('12345', {
'name' => 'John'
}, ip = 0)
All server-side calls originate from your server’s IP. Set ip = 0 to disable geolocation.
- You manage
distinct_id yourself
- No automatic identity management
- Use
.import() for historical events
- Custom consumers for data routing
- Designed for scripting and server environments
Resources