using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using MessagePack;
[MessagePackObject]
public class GameplayEvent {
[Key("event_name")] public string event_name;
[Key("app_id")] public string app_id;
[Key("build_id")] public string build_id;
[Key("player_id")] public string player_id;
[Key("session_id")] public string session_id;
[Key("level_id")] public string level_id;
[Key("position")] public float[] position;
[Key("custom_data")] public object custom_data;
[Key("input_method")] public string input_method;
[Key("timestamp")] public string timestamp;
}
public class TelemetrySender : MonoBehaviour {
public IEnumerator SendEvent() {
var payload = new GameplayEvent {
event_name = "Player_Jump",
app_id = "72ab8f81-ef36-4712-a046-399a62b9afaa",
build_id = "Win64_Shipping_1.0.2",
player_id = "player_uuid",
session_id = "session_uuid",
level_id = "Level_1",
position = new float[] { 10.5f, 0.0f, -5.2f },
custom_data = new { weapon = "Sword", ammo = 10 },
input_method = "Gamepad",
timestamp = System.DateTime.UtcNow.ToString("o")
};
byte[] body = MessagePackSerializer.Serialize(payload);
using var req = new UnityWebRequest("http://localhost:4000/api/events", "POST");
req.uploadHandler = new UploadHandlerRaw(body);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("x-public-token", "YOUR_PUBLIC_TOKEN");
req.SetRequestHeader("Content-Type", "application/msgpack");
yield return req.SendWebRequest();
Debug.Log($"Status: {req.responseCode}");
}
}
Troubleshooting
- Verify x-public-token
and app_id
belong to the same application.
- Ensure payload is MessagePack encoded, not JSON bytes.
- For realtime events, send positions as flat numeric array.
- If a batch fails, validate every item because one invalid record rejects the entire batch.
- For dashboard querying, use /dashboard/analytics
(not /api/sql/select).