Skip to content

Commit a047cec

Browse files
Add SetDefaultParameters API to Firebase Analytics Unity (revised tests)
This commit introduces the `FirebaseAnalytics.SetDefaultParameters()` method to your Unity SDK, wrapping the underlying C++ `firebase::analytics::SetDefaultEventParameters()` and `firebase::analytics::ClearDefaultEventParameters()` functions. My testing approach has been updated based on your feedback: NUnit tests were removed, and calls to the new API have been integrated into the existing TestApp (UIHandler.cs) for manual verification and TestAppAutomated (UIHandlerAutomated.cs) for automated execution. These tests will exercise the API surface but not programmatically verify results from the Unity side. Key changes: - Modified `analytics/src/swig/analytics.i`: - Ignored the original C++ `SetDefaultEventParameters` and `ClearDefaultEventParameters` functions. - Added a C++ helper function `SetDefaultEventParametersHelper` that takes vectors of parameter names and Variant values, converts them to a `std::map`, and calls the C++ `SetDefaultEventParameters`. - Exposed `SetDefaultEventParametersHelper` and `ClearDefaultEventParameters` to C# via SWIG. - Modified `analytics/src/FirebaseAnalytics.cs`: - Added the public static method `SetDefaultParameters(IDictionary<string, object> parameters)`. - If `parameters` is null or empty, the method calls the internal `ClearDefaultEventParameters()`. - Otherwise, it converts the dictionary into `StringList` and `VariantList` and calls the internal `SetDefaultEventParametersHelper()`. - Removed `analytics/testapp/Assets/Firebase/Sample/Analytics/AnalyticsTest.cs` (NUnit test file). - Modified `analytics/testapp/Assets/Firebase/Sample/Analytics/UIHandler.cs`: - Added a new button and method `TestSetDefaultParameters()` to call `FirebaseAnalytics.SetDefaultParameters` with various inputs (single param, multiple params, null, empty dictionary) for manual testing. - Modified `analytics/testapp/Assets/Firebase/Sample/Analytics/UIHandlerAutomated.cs`: - Added calls to `FirebaseAnalytics.SetDefaultParameters` with various inputs to the automated test sequence to ensure the API is exercised.
1 parent 7fbd65a commit a047cec

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

analytics/src/FirebaseAnalytics.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,30 @@ public static void SetConsent(System.Collections.Generic.IDictionary< ConsentTyp
248248
FirebaseAnalyticsInternal.SetConsentWithInts(consentSettingsMap);
249249
}
250250

251+
/// @brief Sets default event parameters.
252+
///
253+
/// These parameters are logged with all events, in addition to the parameters passed to the
254+
/// LogEvent() call. They are useful for logging common parameters with all events.
255+
/// Default event parameters are overridden by event-specific parameters if the names are the
256+
/// same. Default parameters are removed if the dictionary is null or empty.
257+
///
258+
/// @param[in] parameters The dictionary of parameters to set.
259+
/// If null, clears all default parameters.
260+
public static void SetDefaultParameters(
261+
System.Collections.Generic.IDictionary<string, object> parameters) {
262+
if (parameters == null || parameters.Count == 0) {
263+
FirebaseAnalyticsInternal.ClearDefaultEventParameters();
264+
} else {
265+
StringList parameterNames = new StringList();
266+
VariantList parameterValues = new VariantList();
267+
foreach (var kvp in parameters) {
268+
parameterNames.Add(kvp.Key);
269+
parameterValues.Add(Firebase.Variant.FromObject(kvp.Value));
270+
}
271+
FirebaseAnalyticsInternal.SetDefaultEventParametersHelper(parameterNames, parameterValues);
272+
}
273+
}
274+
251275
/// @brief Sets the duration of inactivity that terminates the current session.
252276
///
253277
/// @note The default value is 30 minutes.

analytics/src/swig/analytics.i

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ void SetConsentWithInts(const std::map<int, int>& settings) {
8787
SetConsent(converted);
8888
}
8989

90+
// Helper function to set default event parameters from vectors of strings and variants.
91+
void SetDefaultEventParametersHelper(
92+
const std::vector<std::string>& parameter_names,
93+
const std::vector<firebase::Variant>& parameter_values) {
94+
if (parameter_names.size() != parameter_values.size()) {
95+
firebase::LogError(
96+
"SetDefaultEventParametersHelper given different list sizes (%d, %d)",
97+
parameter_names.size(), parameter_values.size());
98+
return;
99+
}
100+
101+
std::map<std::string, firebase::Variant> default_parameters;
102+
for (size_t i = 0; i < parameter_names.size(); ++i) {
103+
default_parameters[parameter_names[i]] = parameter_values[i];
104+
}
105+
SetDefaultEventParameters(default_parameters);
106+
}
107+
90108
} // namespace analytics
91109
} // namespace firebase
92110

@@ -100,6 +118,10 @@ void SetConsentWithInts(const std::map<int, int>& settings) {
100118
%ignore firebase::analytics::LogEvent(const char*, const Parameter*, size_t);
101119
// Ignore SetConsent, in order to convert the types with our own function.
102120
%ignore firebase::analytics::SetConsent;
121+
// Ignore SetDefaultEventParameters and ClearDefaultEventParameters, as we handle them
122+
// with a custom version or re-expose ClearDefaultEventParameters.
123+
%ignore firebase::analytics::SetDefaultEventParameters;
124+
%ignore firebase::analytics::ClearDefaultEventParameters;
103125
// Ignore the Parameter class, as we don't want to expose that to C# at all.
104126
%ignore firebase::analytics::Parameter;
105127

@@ -121,5 +143,9 @@ namespace analytics {
121143
void LogEvent(const char* name, std::vector<std::string> parameter_names,
122144
std::vector<firebase::Variant> parameter_values);
123145
void SetConsentWithInts(const std::map<int, int>& settings);
146+
void SetDefaultEventParametersHelper(
147+
const std::vector<std::string>& parameter_names,
148+
const std::vector<firebase::Variant>& parameter_values);
149+
void ClearDefaultEventParameters();
124150
} // namespace analytics
125151
} // namespace firebase

analytics/testapp/Assets/Firebase/Sample/Analytics/UIHandler.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,32 @@ public void AnalyticsSetConsent() {
162162
});
163163
}
164164

165+
public void TestSetDefaultParameters() {
166+
DebugLog("Starting TestSetDefaultParameters...");
167+
168+
DebugLog("Setting single default string parameter: {'default_param_string', 'default_value_1'}");
169+
FirebaseAnalytics.SetDefaultParameters(new Dictionary<string, object>
170+
{
171+
{ "default_param_string", "default_value_1" }
172+
});
173+
174+
DebugLog("Setting multiple default parameters: {'default_param_int', 123, 'default_param_double', 45.67, 'default_param_bool', true}");
175+
FirebaseAnalytics.SetDefaultParameters(new Dictionary<string, object>
176+
{
177+
{ "default_param_int", 123 },
178+
{ "default_param_double", 45.67 },
179+
{ "default_param_bool", true }
180+
});
181+
182+
DebugLog("Clearing default parameters with null.");
183+
FirebaseAnalytics.SetDefaultParameters(null);
184+
185+
DebugLog("Clearing default parameters with empty dictionary.");
186+
FirebaseAnalytics.SetDefaultParameters(new Dictionary<string, object>());
187+
188+
DebugLog("TestSetDefaultParameters completed.");
189+
}
190+
165191
// Get the current app instance ID.
166192
public Task<string> DisplayAnalyticsInstanceId() {
167193
return FirebaseAnalytics.GetAnalyticsInstanceIdAsync().ContinueWithOnMainThread(task => {
@@ -257,6 +283,9 @@ void GUIDisplayControls() {
257283
if (GUILayout.Button("Test SetConsent")) {
258284
AnalyticsSetConsent();
259285
}
286+
if (GUILayout.Button("Test SetDefaultParameters")) {
287+
TestSetDefaultParameters();
288+
}
260289
GUILayout.EndVertical();
261290
GUILayout.EndScrollView();
262291
}

analytics/testapp/Assets/Firebase/Sample/Analytics/UIHandlerAutomated.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public override void Start() {
4040
TestAnalyticsSetConsentDoesNotThrow,
4141
TestInstanceIdChangeAfterReset,
4242
TestResetAnalyticsData,
43+
TestSetDefaultParametersAutomated, // Added new test here
4344
// Temporarily disabled until this test is deflaked. b/143603151
4445
//TestCheckAndFixDependenciesInvalidOperation,
4546
TestCheckAndFixDependenciesDoubleCall,
@@ -108,6 +109,34 @@ Task TestAnalyticsSetConsentDoesNotThrow() {
108109
return true;
109110
});
110111
}
112+
113+
Task TestSetDefaultParametersAutomated() {
114+
DebugLog("Automated Test: Starting TestSetDefaultParametersAutomated...");
115+
116+
DebugLog("Automated Test: Setting single default string parameter: {'auto_default_param_string', 'auto_value_1'}");
117+
FirebaseAnalytics.SetDefaultParameters(new Dictionary<string, object>
118+
{
119+
{ "auto_default_param_string", "auto_value_1" }
120+
});
121+
122+
DebugLog("Automated Test: Setting multiple default parameters: {'auto_default_param_int', 789, 'auto_default_param_double', 12.34, 'auto_default_param_bool', false}");
123+
FirebaseAnalytics.SetDefaultParameters(new Dictionary<string, object>
124+
{
125+
{ "auto_default_param_int", 789 },
126+
{ "auto_default_param_double", 12.34 },
127+
{ "auto_default_param_bool", false }
128+
});
129+
130+
DebugLog("Automated Test: Clearing default parameters with null.");
131+
FirebaseAnalytics.SetDefaultParameters(null);
132+
133+
DebugLog("Automated Test: Clearing default parameters with empty dictionary.");
134+
FirebaseAnalytics.SetDefaultParameters(new Dictionary<string, object>());
135+
136+
DebugLog("Automated Test: TestSetDefaultParametersAutomated completed.");
137+
return Task.FromResult(true); // Indicate successful completion
138+
}
139+
111140
Task TestCheckAndFixDependenciesInvalidOperation() {
112141
// Only run the test on Android, as CheckAndFixDependenciesAsync is short
113142
// lived on other platforms, and thus could finish before the extra call.

0 commit comments

Comments
 (0)