Session tracking API reference

API reference2022, updated 20264 min read

A reference for the session endpoints of a product-analytics SDK, covering how to start, extend and end sessions and change a user's device ID, with parameters, examples and errors.

About this sample Purpose: a practice API reference showing how I document SDK endpoints. It's based on my notes from working with analytics SDKs, rewritten for a fictional product ("Acme Analytics") so it contains no client code or internal names. Endpoints and values are illustrative.

The Acme Analytics SDK reports user sessions to the server so you can measure how long and how often people use your app. This reference describes the four session operations the SDK sends.

Overview

Operation When the SDK sends it
Start a session When the app comes to the foreground, or when you call beginSession()
Report session duration Every 60 seconds while the session is active (the "heartbeat")
End a session When the app goes to the background, or when you call endSession()
Change device ID When a user logs in and you switch from an anonymous ID to a known user ID

All operations are sent to the same ingestion endpoint:

POST https://api.acme-analytics.example/i
Content-Type: application/x-www-form-urlencoded

Common parameters

Every request includes these parameters.

Parameter Type Required Description
app_key string Yes Your app's key, from Settings → Apps.
device_id string Yes A unique ID for the device or user. Must not be empty.
timestamp integer Yes Time of the event, in milliseconds since the Unix epoch.
sdk_version string No Version of the SDK sending the request.

Start a session

Marks the beginning of a user session.

Parameters

Parameter Type Required Description
begin_session integer Yes Set to 1.
metrics JSON string No Device details such as _os, _os_version, _app_version, _resolution. Sent once per session.

Example

curl -X POST "https://api.acme-analytics.example/i" \
  -d "app_key=YOUR_APP_KEY" \
  -d "device_id=user-8f3a" \
  -d "timestamp=1764738000000" \
  -d "begin_session=1" \
  --data-urlencode 'metrics={"_os":"Android","_os_version":"15","_app_version":"2.4.0"}'

Response

{ "result": "Success" }

Notes

  • Automatic vs manual sessions. By default the SDK manages sessions for you. To control them yourself, turn on manual session handling, and turn off automatic heartbeats if you report duration yourself.
  • Duplicate starts. If a session is already active, the SDK ignores another start unless you pass force: true to beginSession().

Report session duration

Extends the current session. The SDK sends this heartbeat every 60 seconds by default.

Parameters

Parameter Type Required Description
session_duration integer Yes Seconds of activity since the last report. Not the total.

Example

curl -X POST "https://api.acme-analytics.example/i" \
  -d "app_key=YOUR_APP_KEY" \
  -d "device_id=user-8f3a" \
  -d "timestamp=1764738060000" \
  -d "session_duration=60"

Notes

  • If no heartbeat arrives within the session timeout (default 30 minutes), the server closes the session.
  • Offline requests are queued on the device and sent in order when the connection returns.

End a session

Closes the session and records its final duration.

Parameters

Parameter Type Required Description
end_session integer Yes Set to 1.
session_duration integer Yes Seconds since the last heartbeat.

Example

curl -X POST "https://api.acme-analytics.example/i" \
  -d "app_key=YOUR_APP_KEY" \
  -d "device_id=user-8f3a" \
  -d "timestamp=1764738095000" \
  -d "end_session=1" \
  -d "session_duration=35"

Change or merge a device ID

Use this when a user logs in, to move from an anonymous device ID to your own user ID.

Two modes

Mode What happens Use when
Merge Data recorded under the old ID moves to the new ID on the server. The same person logged in. Their history should follow them.
No merge The current session ends, and a new session starts under the new ID. History stays with the old ID. A different person is now using the device, for example on a shared device.

Parameters (merge)

Parameter Type Required Description
device_id string Yes The new ID.
old_device_id string Yes The previous ID whose data should be merged.

Example (merge)

curl -X POST "https://api.acme-analytics.example/i" \
  -d "app_key=YOUR_APP_KEY" \
  -d "device_id=customer-1042" \
  -d "old_device_id=user-8f3a" \
  -d "timestamp=1764738100000"

SDK equivalent

// merge: same person, keep their history
Acme.changeDeviceId("customer-1042", { merge: true });

// no merge: a different person on this device
Acme.changeDeviceId("customer-2077", { merge: false });

Notes

  • Queued offline requests are sent before the ID change, so earlier events keep the old ID.
  • If the new ID equals the current ID, the request is ignored.

Errors

HTTP status Body Cause and fix
400 {"result": "Missing parameter \"app_key\" or \"device_id\""} A required common parameter is missing or empty.
400 {"result": "Invalid device_id"} The new ID is empty or not a string. Pass a non-empty string.
401 {"result": "App does not exist"} Wrong app_key. Copy it again from Settings → Apps.
429 {"result": "Too many requests"} Rate limit hit. The SDK retries automatically with back-off.

Want documentation like this for your product?