Javascript SDK
Tracking Events

Tracking Events

The SDK auto-tracks default events, including web3 wallet events.

To track custom events, you can use the track function. You must call the init function before sending any events.

import * as airblock from "@airblock/js-sdk";
 
const API_KEY = "app1"; // required
const SERVER_URL = "https://..."; // required
 
airblock.init(API_KEY, SERVER_URL);
airblock.track("swap_initiated"); 

Event Structure

airblock.track(event name, [{event properties}, [{user properties}]])
// Track a basic event
airblock.track("swap_initiated");
 
// Track an event with both event and user properties
airblock.track("swap_initiated", {utm_source: "twitter"}, {sdk_ver: "0.1.1"});
 
// Track an event with event properties
airblock.track("swap_initiated", {utm_source: "twitter"});
 
// Track an event with only user properties
// Notice null event properties
airblock.track("swap_initiated", null, {sdk_ver: "0.1.1"});

The track function adds a few more parameters to the event before sending the event to the server. Here's how an event looks like on the server.

{
    "api_key": "app1",
    "ip_addr": "0.0.0.0", // user's IP address
    "uuid": "916be96d-4b92-4391-89d4-c1288bb2acc6", // user id generated by SDK
    "event_time": 1687431893993,
    "event_type": "swap_initiated", // event name
    "event_properties": { "utm_source": "twitter", "utm_medium": "airdrop" },
    "user_properties": { "sdk_ver": "0.1.1", "wallets": ["metamask"] }
}

Flushing Event Buffer

By default, the SDK sends events to the server in batches. To send buffered events immediately, you can use the flush method.

airblock.track("swap_initiated"); 
airblock.track("swap_completed");
airblock.flush(); // this will send the event