Telemetry - Code

[BUSI-Telemetry-02]

Writing Telemetry data is very easy using the Beamable SDK. But under the hood, it is a simple rest API call. When writing Telemetry, we use the AnalyticsTracker and the TrackEvent API.

The following example example shows how to write custom telemetry data into the Beamable service. You can query this data later.

📘

How to query your custom telemetry data

See the Analytics - Guide to learn more about how to query your custom telemetry data.

Start off by creating a custom event data structure. This holds the payload for your event which you will send to Beamable.

public class MyExampleEvent : CoreEvent 
    {
        public MyExampleEvent(string foo, string bar) : base (
            "example",  
            "my_example_event", 
            new Dictionary<string, object>
            {
                ["foo"] = foo,
                ["bar"] = bar,
                ["hello_world"] = "Hello World."
            })
        {
        }
    }

Now that you have defined what data you want to send, you can then send that event using the AnalyticsTracker.TrackEvent() API. This takes your CoreEvent and a flag for whether you want to send it immediately or queued, which we'll talk about in the next section.

var beamContext = BeamContext.Default;
await beamContext.OnReady;

//Create an instance of the data you want to send
var foo = "lorem ipsum 1";
var bar = "lorem ipsum 2";
var myExampleEvent = new MyExampleEvent(foo, bar);
            
//Send Immediately
var sendImmediately = true;
//Send the event to Beamable's Analytics Database (Athena)
beamContext.Api.AnalyticsTracker.TrackEvent(myExampleEvent, sendImmediately);

The API supports batching together multiple analytics calls (default max limit is 100) within a single client-server communication as an optimization. You will especially want to use the batching feature when you need to write high volumes of data during gameplay. The only difference here is the second parameter of TrackEvent: sendImmediately.

var beamContext = BeamContext.Default;

var eventOne = new MyExampleEvent("foo1", "bar1");
var eventTwo = new MyExampleEvent("foo2", "bar2");
var eventThree = new MyExampleEvent("foo3", "bar3");

//Tracking them batch-style, passing sendImmediately as false
beamContext.Api.AnalyticsTracker.TrackEvent(eventOne, false);
beamContext.Api.AnalyticsTracker.TrackEvent(eventTwo, false);
beamContext.Api.AnalyticsTracker.TrackEvent(eventThree, false);

Analytics event requests will be batched together into a list, then sent off as one API call either when a certain amount of requests has been reached, or after a certain amount of time passes. These values are configurable through the BatchSettings, which can be updated with UpdateBatchSettings. The default settings are a 1 second heartbeat, 60 second timeout, and 100 event batch size.

var beamContext = BeamContext.Default;

beamContext.Api.AnalyticsTracker.UpdateBatchSettings(
    new BatchSettings
    {
        HeartbeatInterval = 1,
        MaxSize = 100,
        TimeoutSeconds = 60
    });