☰
All featuresAttributionRetentionAudiencesAnalyticsIntegrationsGrow Steam wishlistsSteam Next FestCreator marketingLaunch on SteamAsk with AIFor game developersFor game studiosFor game publishersComparisonsPricingSecurityDocumentationFree toolsBlogGlossary
FREE GAME ANALYTICS TOOL

Engine event snippet generator

Pick your engine and the events you want, and get copy-paste code that sends them to an HTTP analytics API. It covers 9 engines without an official SDK. The code keeps events in an offline queue, retries with backoff, sends nothing before the player consents, and uses the engine's own HTTP and save functions.

Engine event snippet generator

Events to add

game_open, session_start and session_end are in the setup code already.

GML (Create event)
// obj_analytics, Create event (mark the object Persistent)
#macro MANALENS_URL "https://events.manalens.dev/v0/events"
#macro MANALENS_KEY "YOUR_PUBLISHABLE_KEY"
randomise();
date_set_timezone(timezone_utc);
headers = ds_map_create();
headers[? "Content-Type"] = "application/json";
headers[? "x-publishable-key"] = MANALENS_KEY;
queue = []; inflight = -1; inflight_n = 0; backoff = 2;
anon_id = ""; session_id = ""; consent = false; session_start_ms = current_time;

uuid4 = function() {
    var hex = "0123456789abcdef", s = "";
    for (var i = 1; i <= 36; i++) {
        if (i == 9 || i == 14 || i == 19 || i == 24) s += "-";
        else if (i == 15) s += "4";
        else if (i == 20) s += string_char_at("89ab", irandom(3) + 1);
        else s += string_char_at(hex, irandom(15) + 1);
    }
    return s;
};
pad = function(n) { return string_replace_all(string_format(n, 2, 0), " ", "0"); };
iso_now = function() {
    var d = date_current_datetime();
    return string(date_get_year(d)) + "-" + pad(date_get_month(d)) + "-" + pad(date_get_day(d)) + "T"
        + pad(date_get_hour(d)) + ":" + pad(date_get_minute(d)) + ":" + pad(floor(date_get_second(d))) + ".000Z";
};
save_queue = function() {
    var b = buffer_create(1024, buffer_grow, 1);
    buffer_write(b, buffer_string, json_stringify(queue));
    buffer_save(b, "mana_queue.dat");
    buffer_delete(b);
};
load_queue = function() {
    var b = buffer_load("mana_queue.dat");
    if (b == -1) return [];
    var s = buffer_read(b, buffer_string);
    buffer_delete(b);
    try { return json_parse(s); } catch (e) { return []; }
};
track = function(name, props) {
    if (!consent) return;
    array_push(queue, { type: "track", event: name, channel: "app", version: "0.1.0",
        message_id: uuid4(), anonymous_id: anon_id, session_id: session_id, original_timestamp: iso_now(),
        context: { app: { platform: "windows", storefront: "steam", app_version: "1.0.0", build: "1", engine: "other" },
                   locale: "en", library_name: "my-game", library_version: "1.0.0" },
        properties: props });
    save_queue();
    if (array_length(queue) >= 50) flush();
};
flush = function() {
    if (!consent || inflight != -1 || array_length(queue) == 0) return;
    inflight_n = min(50, array_length(queue));
    var batch = [];
    for (var i = 0; i < inflight_n; i++) array_push(batch, queue[i]);
    inflight = http_request(MANALENS_URL, "POST", headers, json_stringify({ events: batch }));
};
set_consent = function(on) {
    consent = on;
    ini_open("manalens.ini");
    ini_write_string("manalens", "consent", on ? "1" : "0");
    if (!on) {
        ini_key_delete("manalens", "anon_id"); ini_key_delete("manalens", "first_open_at"); ini_close();
        queue = []; if (file_exists("mana_queue.dat")) file_delete("mana_queue.dat");
        return;
    }
    anon_id = ini_read_string("manalens", "anon_id", "");
    var first = (anon_id == "");
    if (first) { anon_id = uuid4(); ini_write_string("manalens", "anon_id", anon_id); ini_write_string("manalens", "first_open_at", iso_now()); }
    var first_at = ini_read_string("manalens", "first_open_at", "");
    ini_close();
    queue = load_queue(); session_id = uuid4(); session_start_ms = current_time;
    track("game_open", { is_first_open: first, first_open_at: first_at });
    track("session_start", {});
    flush();
};
ini_open("manalens.ini"); var c = ini_read_string("manalens", "consent", "0"); ini_close();
if (c == "1") set_consent(true);
call_later(30, time_source_units_seconds, flush, true);
GML (Async HTTP, Game End and Clean Up events)
// obj_analytics, Async - HTTP event
if (async_load[? "id"] != inflight) exit;
if (async_load[? "status"] == 1) exit; // still downloading
inflight = -1;
if (!consent) exit;
var code = async_load[? "http_status"];
if (is_undefined(code)) code = 0;
if (code >= 200 && code < 300) { array_delete(queue, 0, inflight_n); save_queue(); backoff = 2; flush(); }
else if (code == 0 || code >= 500 || code == 408 || code == 429) {
    call_later(backoff, time_source_units_seconds, flush); backoff = min(backoff * 2, 300); }
else { array_delete(queue, 0, inflight_n); save_queue(); } // refused data: drop the batch

// obj_analytics, Game End event
track("session_end", { duration_s: floor((current_time - session_start_ms) / 1000) });

// obj_analytics, Clean Up event
ds_map_destroy(headers);

// Consent screen button, Left Released event:
// obj_analytics.set_consent(true);
Your events
// Your events. Replace the example values with your own.
obj_analytics.track("level_start", { level: 3, attempt: 1 });
obj_analytics.track("level_complete", { level: 3, attempt: 1, score: 1200 });
obj_analytics.track("level_fail", { level: 3, attempt: 1, score: 400 });

From our guide How to add analytics to a GameMaker game, which explains each line and lists the engine manual pages we checked.

How to use it

  1. Pick your engine: GameMaker, Construct 3, Defold, LÖVE (Love2D), Ren'Py, Bevy, Pico-8, RPG Maker MZ, and Phaser.
  2. Enter your publishable key, your game version, the build and the store. The key stays in your browser.
  3. Tick the events to add, such as level start, level complete and level fail, and type your own event names if you need more.
  4. Copy each block into the file its label names, then copy the event calls into your gameplay code.
  5. Run a development build, check that the response has "rejected":0, and copy the link to this setup for your team.

How it works

The generator does not write new network code. It takes the code from our analytics guide for each engine, where every engine function is checked against the engine's official manual, and fills in your key, endpoint, version, build, store and game name. It then writes one track call per event in your engine's syntax.

Every event is the same JSON envelope that the Godot, Unity, and Unreal SDKs send: type "track", channel "app", a new UUID version 4 as message_id, a saved UUID as anonymous_id, a session_id, an ISO 8601 timestamp and context.app with platform, store, version, build and engine "other". The code sends up to 50 events per request.

The queue is saved to disk, so events survive a crash or a lost connection. A batch is kept and retried later after a network error, a 5xx, 408 or 429 response, with a longer wait each time. It is dropped after any other 4xx. Events older than 7 days are dropped before sending, because the API refuses a batch that holds one.

request = POST {endpoint}/v0/events with x-publishable-key and {"events": [1 to 50 events]}
retry wait = 2 seconds, doubled after each failure, at most 300 seconds
send = only events newer than 7 days, only after consent

Worked example

Example with made-up inputs: Defold, version 0.9.2, build 204, two events

With Defold picked, the generator fills main/analytics.lua from the Defold guide with your key, version "0.9.2", build "204" and library_name "lighthouse". It then writes 2 calls in Lua table syntax: manalens.track("level_complete", { level = 3, attempt = 1, score = 1200 }) and manalens.track("resource", { flow = "sink", currency = "gold", amount = 250, item = "potion" }).

The level_complete call has 3 properties and the resource call has 4, as in our event conventions. If a player finishes 60 levels offline, the queue holds 60 events and sends them in 2 requests of up to 50 when the connection returns.

How to read the result

Paste the setup blocks once, then call track from your gameplay code where each moment happens. Keep the example property names, such as level, attempt and score, so every build sends the same names.

Keep the consent box ticked unless you have checked that you may collect analytics without asking. With it ticked, the code sends nothing and saves no ID until the player agrees, and it deletes the ID and the queue when they say no.

Set build to dev in the first test build, so you can filter those events out later. Our practice is to check one batch, then switch off the network, play for a minute and check that the queued events arrive once.

Why not use an old analytics tutorial?

Many engine tutorials still send events to Google Universal Analytics, which Google shut down in 2023, so that code sends nothing. Others send one request per event with no queue, which loses events offline and on quit. This code queues to disk, sends in batches, retries with backoff and respects consent, which is what an SDK does for you in Unity, Godot, or Unreal.

Which events should I send first?

Start with the moments that show where players stop: level start, level complete and level fail, with the level and the attempt. Add resource events if your game has an economy, and purchase events if it sells anything. The setup code already sends game open, session start and session end, so retention and playtime work from the first build.

Limits

It covers 9 engines without an official Manalens SDK. Unity, Godot, and Unreal have official SDKs that do this for you. GDevelop, MonoGame, Raylib, HaxeFlixel and Stride have guides but are not in the generator yet.

The code is a starting point that you own. It does not add your consent screen, a settings toggle or a privacy notice, and you should test it on every platform you ship.

Pico-8 has no HTTP inside the cart. Its code sends a numeric event code and one value through GPIO to the HTML page, so it works only in the web export.

Frequently asked questions

How do I add analytics to a GameMaker game?

Use http_request() with a header map and a JSON body, and read the reply in the Async HTTP event. Pick GameMaker in this generator to get the Create event code with a saved player ID, a buffer file queue, retries and consent, plus a track call for each event you pick. Our GameMaker guide explains each line.

Does the code work offline?

Yes. Each event goes into a queue that is saved to disk, or to local storage on web exports. The code sends up to 50 events per request when it can, keeps the batch after a network error and tries again later with a longer wait, and drops events older than 7 days, which the API would refuse.

Is the publishable key safe to put in my game?

Yes. The publishable key can only send events, not read any data, so it is made to ship in a game build. Never put the secret key in a game: it belongs on your server only. The generator refuses a key that does not look like a publishable key, so a pasted secret key shows a warning.

How does the consent flag work?

With consent on, the code sends no event and saves no player ID until you call the consent function with true, for example from a button on your first screen. Calling it with false deletes the ID and the queue. You build the screen and the text. The generator shows the exact consent call for your engine.

Can I use this code with another analytics backend?

The queue, retry and consent parts work with any HTTP endpoint, but the event format is the Manalens app event envelope, sent with an x-publishable-key header. Another backend needs its own body and headers. For Manalens, the same events also join the website visits and Steam data of the same project.

Is my key sent anywhere?

No. The code is built in your browser from guide code that is already on the page. Your key, endpoint, version and build are not sent to our server and are not saved in the page link. Only the engine and the events you tick are kept in the link, so a shared link opens the same setup.

Sources

Last reviewed . The code is built in your browser. Your key, endpoint and build are not sent to our server and are not kept in the page link.

Next step

Once events arrive, Manalens shows retention, level funnels, playtime and errors by build, next to your Steam wishlists and website visits for the same players. See player analytics →

Explore more game tools →