Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions launchdarkly-server-sdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ can be attached to the event as JSON.
@tparam string key The name of the event
@tparam context context An opaque context object from @{makeUser} or @{makeContext}
@tparam[opt] table data A value to be associated with an event
@tparam[optchain] number metric A value to be associated with an event
@tparam[opt] number metric A value to be associated with an event. To send a metric without data, pass nil as data.
@treturn nil
*/
static int
Expand All @@ -1813,19 +1813,20 @@ LuaLDClientTrack(lua_State *const l)

LDContext *context = (LDContext *) luaL_checkudata(l, 3, "LaunchDarklyContext");

LDValue value;
if (lua_isnil(l, 4)) {
value = NULL;
} else {
value = LuaValueToJSON(l, 4);
}
const bool has_data = !lua_isnoneornil(l, 4);
const bool has_metric = lua_gettop(l) == 5 && lua_isnumber(l, 5);

if (lua_gettop(l) == 5 && lua_isnumber(l, 5)) {
if (has_metric) {
const double metric = luaL_checknumber(l, 5);

// The C++ SDK requires a data value with a metric. Use JSON null if the caller gives no data.
LDValue value = has_data ? LuaValueToJSON(l, 4) : LDValue_NewNull();

LDServerSDK_TrackMetric(*client, *context, key, metric, value);
} else if (has_data) {
LDServerSDK_TrackData(*client, *context, key, LuaValueToJSON(l, 4));
} else {
LDServerSDK_TrackData(*client, *context, key, value);
LDServerSDK_TrackEvent(*client, *context, key);
}

return 0;
Expand Down
15 changes: 15 additions & 0 deletions test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ function TestAll:testIdentify()
makeTestClient():identify(context)
end

function TestAll:testTrack()
local c = makeTestClient()
c:track("event", context)
c:track("event", context, { a = "b" })
c:track("event", context, { a = "b" }, 42)
c:track("event", context, { a = "b" }, nil)
end

function TestAll:testTrackWithNilData()
local c = makeTestClient()
c:track("event", context, nil)
c:track("event", context, nil, 42)
c:track("event", context, nil, nil)
end


function TestAll:testAllFlags()
u.assertEquals(makeTestClient():allFlags(user), {})
Expand Down
Loading