Skip to content

Commit

Permalink
Added analytics.set_default_event_parameters() (#35)
Browse files Browse the repository at this point in the history
* Added set_default_event_parameters

* Update analytics.gui_script
  • Loading branch information
britzl authored Oct 17, 2024
1 parent 56f368c commit db8ca83
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 72 deletions.
8 changes: 7 additions & 1 deletion example/analytics.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function init(self)
firebase.set_callback(function(self, message_id, message)
if message_id == firebase.MSG_INITIALIZED then
if analytics then
local default_params = {
foo = "bar",
bar = 124,
baz = true
}
analytics.set_default_event_parameters(default_params)
analytics.set_callback(analytics_callback)
analytics.initialize()
analytics.get_id()
Expand All @@ -40,7 +46,7 @@ function init(self)
end)
firebase.initialize()
end
gui.animate(gui.get_node("pie"), "rotation.z", 360, gui.EASING_LINEAR, 2, 0, function() end, gui.PLAYBACK_LOOP_FORWARD)
gui.animate(gui.get_node("pie"), "euler.z", 360, gui.EASING_LINEAR, 2, 0, function() end, gui.PLAYBACK_LOOP_FORWARD)
end

function on_input(self, action_id, action)
Expand Down
19 changes: 9 additions & 10 deletions firebase_analytics/api/firebase.script_api
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
- name: callback
type: function
desc: Callback function that is executed on any event in the SDK.

parameters:
- name: self
type: object
Expand Down Expand Up @@ -48,7 +47,6 @@
- name: log
type: function
desc: Log an event without parameters.

parameters:
- name: name
type: string
Expand All @@ -57,7 +55,6 @@
- name: log_string
type: function
desc: Log an event with one string parameter.

parameters:
- name: name
type: string
Expand All @@ -72,7 +69,6 @@
- name: log_int
type: function
desc: Log an event with one integer parameter.

parameters:
- name: name
type: string
Expand All @@ -87,7 +83,6 @@
- name: log_number
type: function
desc: Log an event with one float parameter.

parameters:
- name: name
type: string
Expand All @@ -102,19 +97,25 @@
- name: log_table
type: function
desc: Log an event with table parameters.

parameters:
- name: name
type: string
desc: Event name
- name: PARAMeters_table
- name: parameters_table
type: table
desc: Table with parameters (key-value pairs)

- name: set_default_event_params
type: function
desc: Log an event with table parameters.
parameters:
- name: default_params
type: table
desc: Table with default parameters (key-value pairs)

- name: set_user_id
type: function
desc: Sets the user ID property.

parameters:
- name: user_id
type: string
Expand All @@ -123,7 +124,6 @@
- name: set_user_property
type: function
desc: Set a user property to the given value.

parameters:
- name: name
type: string
Expand All @@ -143,7 +143,6 @@
- name: set_enabled
type: function
desc: Sets whether analytics collection is enabled for this app on this device.

parameters:
- name: key
type: boolean
Expand Down
78 changes: 72 additions & 6 deletions firebase_analytics/src/firebase_analytics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static int Lua_LogString(lua_State* L)
CheckIfValueValid(L, param, event_name, param_name);

OpenEvent();
AddParamString(param_name, param);
AddEventParamString(param_name, param);
SendEvent(event_name);
CloseEvent();
return 0;
Expand All @@ -111,7 +111,7 @@ static int Lua_LogInt(lua_State* L)
CheckIfParamValid(L, param_name, event_name);

OpenEvent();
AddParamInt(param_name, param);
AddEventParamInt(param_name, param);
SendEvent(event_name);
CloseEvent();
return 0;
Expand All @@ -128,7 +128,7 @@ static int Lua_LogNumber(lua_State* L)
CheckIfParamValid(L, param_name, event_name);

OpenEvent();
AddParamNumber(param_name, param);
AddEventParamNumber(param_name, param);
SendEvent(event_name);
CloseEvent();
return 0;
Expand Down Expand Up @@ -174,13 +174,13 @@ static int Lua_LogTable(lua_State* L)
CheckIfValueValid(L, param_value, event_name, param_name);
return 0;
}
AddParamString(param_name, param_value);
AddEventParamString(param_name, param_value);
break;
case LUA_TBOOLEAN:
AddParamInt(param_name, lua_toboolean(L, -1));
AddEventParamInt(param_name, lua_toboolean(L, -1));
break;
case LUA_TNUMBER:
AddParamNumber(param_name, lua_tonumber(L, -1));
AddEventParamNumber(param_name, lua_tonumber(L, -1));
break;
default: /* other values */
lua_pop(L, 3);
Expand Down Expand Up @@ -224,6 +224,71 @@ static int Lua_SetUserProperty(lua_State* L)
return 0;
}

static int Lua_SetDefaultEventParameters(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);

char* event_name = "default";

luaL_checktype(L, 1, LUA_TTABLE);
OpenDefaultEventParams();
lua_pushvalue(L, 1);
lua_pushnil(L);
int size = 0;
while (lua_next(L, -2) != 0)
{
if (size == MAX_ELEMENTS) {
lua_pop(L, 1);
CloseDefaultEventParams();
return luaL_error(L, "Too many parameters in '%s'", event_name);
}
const char* param_name = lua_tostring(L, -2);
// Can't use CheckIfParamValid here, because cleanup needed if it's invalid
if (strlen(param_name) > MAX_PARAM_LENGTH)
{
lua_pop(L, 2);
CloseDefaultEventParams();
CheckIfParamValid(L, param_name, event_name);
return 0;
}
int t = lua_type(L, -1);
const char* param_value;
switch (t) {
case LUA_TSTRING:
param_value = lua_tostring(L, -1);
// Can't use CheckIfValueValid here, because cleanup needed if it's invalid
if (strlen(param_value) > MAX_VALUE_LENGTH)
{
lua_pop(L, 2);
CloseDefaultEventParams();
CheckIfValueValid(L, param_value, event_name, param_name);
return 0;
}
AddDefaultEventParamString(param_name, param_value);
break;
case LUA_TBOOLEAN:
AddDefaultEventParamInt(param_name, lua_toboolean(L, -1));
break;
case LUA_TNUMBER:
AddDefaultEventParamNumber(param_name, lua_tonumber(L, -1));
break;
default: /* other values */
lua_pop(L, 2);
CloseDefaultEventParams();
return luaL_error(L, "Wrong type for table attribute '%s', type: '%s' in event '%s'", param_name, luaL_typename(L, -1), event_name);
break;
}
lua_pop(L, 1);
++size;
}

CloseDefaultEventParams();

lua_pop(L, 1);

return 0;
}

static int Lua_SetUserId(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);
Expand All @@ -246,6 +311,7 @@ static const luaL_reg Module_methods[] =
{"reset", Lua_Reset},
{"set_enabled", Lua_SetEnabled},
{"set_user_property", Lua_SetUserProperty},
{"set_default_event_parameters", Lua_SetDefaultEventParameters},
{"set_user_id", Lua_SetUserId},
{0, 0}
};
Expand Down
77 changes: 58 additions & 19 deletions firebase_analytics/src/firebase_analytics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,91 @@ void SetAnalyticsCollectionEnabled(bool enabled) {

// ---

NSMutableDictionary *g_bundle;
NSMutableDictionary *g_EventParams;
NSMutableDictionary *g_DefaultEventParams;

static void ReleaseBundle() {
for (NSString *key in g_bundle)
static void ReleaseDictionary(NSMutableDictionary *dict) {
for (NSString *key in dict)
{
[[g_bundle objectForKey:key] release];
[[dict objectForKey:key] release];
[key release];
}
[g_bundle release];
g_bundle = nil;
[dict release];
}

static void ReleaseEventParams() {
ReleaseDictionary(g_EventParams);
g_EventParams = nil;
}
static void ReleaseDefaultEventParams() {
ReleaseDictionary(g_DefaultEventParams);
g_DefaultEventParams = nil;
}

void OpenEvent() {
if (g_bundle != nil)
if (g_EventParams != nil)
{
ReleaseEventParams();
}
g_EventParams = [[NSMutableDictionary alloc] init];
}
void OpenDefaultEventParams() {
if (g_DefaultEventParams != nil)
{
ReleaseBundle();
ReleaseDefaultEventParams();
}
g_bundle = [[NSMutableDictionary alloc] init];
g_DefaultEventParams = [[NSMutableDictionary alloc] init];
}

void AddParamString(const char* param_name, const char* param) {
void AddParamStringToDict(NSMutableDictionary *dict, const char* param_name, const char* param) {
NSString *key = [[NSString alloc] initWithUTF8String:param_name];
NSString *value = [[NSString alloc] initWithUTF8String:param];
[g_bundle setObject:value forKey:key];
[dict setObject:value forKey:key];
}

void AddParamNumber(const char* param_name, double param) {
void AddParamNumberToDict(NSMutableDictionary *dict, const char* param_name, double param) {
NSString *key = [[NSString alloc] initWithUTF8String:param_name];
NSNumber *value = [[NSNumber alloc] initWithDouble:param];
[g_bundle setObject:value forKey:key];
[dict setObject:value forKey:key];
}

void AddParamInt(const char* param_name, int param) {
void AddParamIntToDict(NSMutableDictionary *dict, const char* param_name, int param) {
NSString *key = [[NSString alloc] initWithUTF8String:param_name];
NSNumber *value = [[NSNumber alloc] initWithInt:param];
[g_bundle setObject:value forKey:key];
[dict setObject:value forKey:key];
}

void AddEventParamString(const char* param_name, const char* param) {
AddParamStringToDict(g_EventParams, param_name, param);
}
void AddDefaultEventParamString(const char* param_name, const char* param) {
AddParamStringToDict(g_DefaultEventParams, param_name, param);
}
void AddEventParamNumber(const char* param_name, double param) {
AddParamNumberToDict(g_EventParams, param_name, param);
}
void AddDefaultEventParamNumber(const char* param_name, double param) {
AddParamNumberToDict(g_DefaultEventParams, param_name, param);
}
void AddEventParamInt(const char* param_name, int param) {
AddParamIntToDict(g_EventParams, param_name, param);
}
void AddDefaultEventParamInt(const char* param_name, int param) {
AddParamIntToDict(g_DefaultEventParams, param_name, param);
}

void SendEvent(const char* event_name) {
NSString *name = [[NSString alloc] initWithUTF8String:event_name];
[FIRAnalytics logEventWithName:name parameters:g_bundle];
[FIRAnalytics logEventWithName:name parameters:g_EventParams];
[name release];
}
void SetDefaultEventParameters() {
[FIRAnalytics setDefaultEventParameters:g_DefaultEventParams];
}

void CloseEvent() {
ReleaseBundle();
ReleaseEventParams();
}
void CloseDefaultEventParams() {
ReleaseDefaultEventParams();
}

// ---
Expand Down
Loading

0 comments on commit db8ca83

Please sign in to comment.