Skip to main content

Installation

go get github.com/mixpanel/mixpanel-go

Initialize

import "github.com/mixpanel/mixpanel-go"

mp := mixpanel.NewApiClient("YOUR_PROJECT_TOKEN")

Configuration

mp := mixpanel.NewApiClient(
  "YOUR_PROJECT_TOKEN",
  mixpanel.ServiceAccount(7282615, "service_account_name", "service_account_secret")
)

Track Events

You must provide a distinct_id for all events.
ctx := context.Background()

err := mp.Track(ctx, []*mixpanel.Event{
  mp.NewEvent("Purchase", "12345", map[string]any{
    "product": "Premium Subscription",
    "amount": 49.99,
    "currency": "USD",
  }),
})

if err != nil {
  log.Fatal(err)
}

Import Historical Events

For events older than 5 days:
ctx := context.Background()

event := mp.NewEvent("Old Purchase", mixpanel.EmptyDistinctID, nil)
event.AddTime(time.Now().AddDate(0, 0, -10))
event.AddInsertID("unique_insert_id")

importEvents := []*mixpanel.Event{event}

err := mp.Import(ctx, importEvents, mixpanel.ImportOptionsRecommend)
if err != nil {
  log.Fatal(err)
}

User Profiles

ctx := context.Background()

user := mixpanel.NewPeopleProperties("12345", map[string]any{
  "name": "John Doe",
  "email": "[email protected]",
  "plan": "Premium",
  "$ip": 0, // Disable geolocation
})

err := mp.PeopleSet(ctx, []*mixpanel.PeopleProperties{user})
user := mixpanel.NewPeopleProperties("12345", map[string]any{
  "First Purchase": "2024-01-01",
  "$ip": 0,
})

err := mp.PeopleSetOnce(ctx, []*mixpanel.PeopleProperties{user})

Group Analytics

Send Events

ctx := context.Background()

err := mp.Track(ctx, []*mixpanel.Event{
  mp.NewEvent("Feature Used", "12345", map[string]any{
    "company": "Acme Inc",
    "feature": "Reports",
  }),
})

Set Group Properties

err := mp.GroupSet(ctx, "company", "Acme Inc", map[string]any{
  "name": "Acme Inc",
  "industry": "Technology",
  "employees": 500,
})
err := mp.GroupSetOnce(ctx, "company", "Acme Inc", map[string]any{
  "founded": "2010-01-01",
})

Privacy Controls

EU Data Residency

ctx := context.Background()

mp := mixpanel.NewApiClient(
  "YOUR_PROJECT_TOKEN",
  mixpanel.EuResidency()
)

Disable Geolocation

// For events
err := mp.Track(ctx, []*mixpanel.Event{
  mp.NewEvent("event", "12345", map[string]any{
    "ip": "0",
  }),
})

// For profiles
user := mixpanel.NewPeopleProperties("12345", map[string]any{
  "name": "John",
  "$ip": 0,
})
err := mp.PeopleSet(ctx, []*mixpanel.PeopleProperties{user})

Platform Considerations

All server-side calls originate from your server’s IP. Set ip to 0 to disable geolocation.
  • Context-based API (use context.Background())
  • You manage distinct_id yourself
  • No automatic identity management
  • Use .Import() for historical events
  • Type-safe API with Go generics
  • Designed for high-performance Go services

Resources

Build docs developers (and LLMs) love