How Tos
Effective Server-Side Tracking

This document gives tips for implementing scalable, maintainable server-side tracking. If you're just getting started, check out the quickstart.

Tracking Browser, Device, and OS

Mixpanel's Web and Mobile SDKs parse User-Agent into a set of properties about the user's browser, device, and OS. This doesn't happen automatically with server-side tracking, but it's easy to add these properties yourself.

User-Agent is a header sent on all requests to your server. Most server frameworks provide a way to access your headers:

Parse the user agent using a library for your language and send the browser, device, and OS properties to Mixpanel. Here's an example using Python and uaparser (opens in a new tab):

from mixpanel import Mixpanel
from ua_parser import user_agent_parser
mp = mixpanel.init("YOUR_TOKEN")
def track_to_mp(request, event_name, properties):
  parsed = user_agent_parser.Parse(request.headers["User-Agent"])
  # Set parsed values as properties.
  # You can also parse out the browser/device/os versions.
    "$browser": parsed["user_agent"]["family"],
    "$device": parsed["device"]["family"],
    "$os": parsed["os"]["family"],
  properties["ip"] = request.remote_addr
  mp.track(request.user_id, "Signed Up", properties)
def handle_signup(request):
  # ... logic to process the signup ...
  track_to_mp(request, "Signed Up", {"Signup Type": request.form["type"]})
  return "Signup successful!"

Tracking Geolocation

If you supply the ip property on an event, Mixpanel will enrich the event with $city, $country, and $region properties. Mixpanel's Web and Mobile SDKs automatically set ip to the IP address of the device that they're installed on.

Note: Mixpanel drops the ip address at ingestion and does not store it at rest, to protect a user's privacy.

If you're tracking from your servers, you need to set the ip property of the events to the client's IP address. Most server frameworks provide this out of the box.

We recommend creating a helper function that does this for all tracking calls:

from mixpanel import Mixpanel
mp = mixpanel.init("YOUR_TOKEN")
def track_to_mp(request, event_name, properties):
  properties["ip"] = request.remote_addr
  mp.track(request.user_id, "Signed Up", properties)
def handle_signup(request):
  # ... logic to process the signup ...
  track_to_mp(request, "Signed Up", {"Signup Type": request.form["type"]})
  return "Signup successful!"

Identifying Users

Our server libraries normally require that you specify the distinct_id value for each event. If you don't know the user's identity at the time the event is tracked, then they're an anonymous user. When using our Web or Mobile SDKs, Mixpanel will automatically generate an ID that's local to that user's device. This ID will persist on all events tracked by that user on that device, until you call identify() or reset(). More on that in our identity management guide.

If you're tracking from servers, you'll need to generate and manage that ID yourself. When you have hybrid implementations (events also come from the client-side), you could optionally send the ID generated on the client to the server and keep it as a session variable instead of generating a new one.

As an outline, you will want to follow the approach below:

Step 1: Generate an anonymous ID

The key is to have an ID that is unique to each user and persists during that user's session. We recommend generating a UUID and storing that value in a cookie. All common server frameworks provide a simple way to set and retrieve cookies per request.

Step 2: Leverage this ID for anonymous events

When tracking events from your server, set the distinct_id property of events to the anonymous ID generated.

In case your project has Simplied ID management specifically, and the user is anonymous, you should include a property named $device_id with this value. You can then optionally also include the distinct_id (requires for you to prefix the ID with the string 'device:') but do note that if you don't include it, it will be assumed. You can see more in the python code example.

Step 3: Set the authenticated ID once users log in

Once the user logs in, you know their true ID, you should leverage the new ID for the user. Depending on the ID management model on your project (see project settings), you can do the following:

If you are using the original ID merge API

Send an $identify event combining the anonymous and authenticated IDs. Events after this should use the authenticated ID. Learn more in our ID Merge guide.

If you are using the Simplified ID merge API Set the $user_id property to that ID. Continue setting $device_id to the ID generated in step 1. If Mixpanel receives an event with both $device_id and $user_id set, it will create a link between those two users. This is essential to track pre-login and post-login behavior accurately.

The distinct_id will be assumed but if you include it, it should be the same value as the $user_id.

Example python code

Here's a pseudocode example using Django's cookies (opens in a new tab) and authentication (opens in a new tab). It assumes the client is setting and sending cookies:

import uuid
def track_to_mp(request, event_name, properties):
  # This assumes you've previously set a cookie called "session_id" that is local to the user's session
  # Set `$device_id` to that cookie's value
  properties["$device_id"] = uuid.uuid4()
  # Set $user_id if the user is authenticated (logged in).
  if request.user.is_authenticated():
    properties["$user_id"] = request.user.username
  # Note: leave the first argument blank here, since we're passing $device_id and $user_id as properties.
  mp.track("", event_name, properties)
def identify_user(request):
  properties = {
    "$device_id": uuid.uuid4(),
    "$identified_id": request.user.username
  track_to_mp(request, "$identify", properties)
def handle_pageview(request):
  response = HTTPResponse("...")
  track_to_mp(request, "Pageview", {"page_url": request.page_url})