Skip to content

Commit c805d12

Browse files
authored
Feat: add SetDefaultEventParameters and have EventLog to take in a IEnumerable (#1359)
* feat(analytics): Add SetDefaultEventParameters and overload LogEvent Adds the `SetDefaultEventParameters` function to the Firebase Analytics SDK. This allows users to set default parameters for all events. Also adds an overload to the `LogEvent` function to accept an `IEnumerable<Parameter>` in addition to `params Parameter[]`. * feat(analytics): Add release notes for SetDefaultEventParameters * fixup: Formatting and wording changes * fixup: reword readme changes to be consistent with csharp changes
1 parent 81f3e2d commit c805d12

File tree

3 files changed

+102
-8
lines changed

3 files changed

+102
-8
lines changed

analytics/src/FirebaseAnalytics.cs

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
using System.Threading.Tasks;
18+
using System.Collections.Generic;
1819

1920
namespace Firebase.Analytics {
2021

@@ -206,18 +207,79 @@ public static void LogEvent(string name) {
206207
///
207208
/// @param[in] parameters A parameter array of `Parameter` instances.
208209
public static void LogEvent(string name, params Parameter[] parameters) {
209-
// Convert the Parameter array into a StringList and VariantList to pass to C++
210+
LogEvent(name, (IEnumerable<Parameter>)parameters);
211+
}
212+
213+
/// @brief Log an event with associated parameters.
214+
///
215+
/// An Event is an important occurrence in your app that you want to measure.
216+
/// You can report up to 500 different types of events per app and you can
217+
/// associate up to 25 unique parameters with each Event type.
218+
///
219+
/// Some common events are in the reference guide via the
220+
/// FirebaseAnalytics.Event* constants, but you may also choose to specify
221+
/// custom event types that are associated with your specific app.
222+
///
223+
/// @param[in] name Name of the event to log. Should contain 1 to 40
224+
/// alphanumeric characters or underscores. The name must start with an
225+
/// alphabetic character. Some event names are reserved.
226+
/// See the FirebaseAnalytics.Event properties for the list of reserved event
227+
/// names.
228+
/// The "firebase_" prefix is reserved and should not be used. Note that event
229+
/// names are case-sensitive and that logging two events whose names differ
230+
/// only in case will result in two distinct events.
231+
///
232+
/// @param[in] parameters An enumerable list of `Parameter` instances.
233+
public static void LogEvent(string name, IEnumerable<Parameter> parameters) {
210234
StringList parameterNames = new StringList();
211235
VariantList parameterValues = new VariantList();
212-
213-
foreach (Parameter p in parameters) {
214-
parameterNames.Add(p.Name);
215-
parameterValues.Add(Firebase.Variant.FromObject(p.Value));
236+
if (parameters != null) {
237+
foreach (Parameter p in parameters) {
238+
parameterNames.Add(p.Name);
239+
parameterValues.Add(Firebase.Variant.FromObject(p.Value));
240+
}
216241
}
217-
218242
FirebaseAnalyticsInternal.LogEvent(name, parameterNames, parameterValues);
219243
}
220244

245+
/// @brief Adds parameters that will be set on every event logged from the SDK.
246+
///
247+
/// Adds parameters that will be set on every event logged from the SDK,
248+
/// including automatic ones. The values passed in the parameters bundle will
249+
/// be added to the map of default event parameters. These parameters persist
250+
/// across app runs. They are of lower precedence than event parameters, so if
251+
/// an event parameter and a parameter set using this API have the same name,
252+
/// the value of the event parameter will be used. The same limitations on
253+
/// event parameters apply to default event parameters.
254+
///
255+
/// @param[in] parameters A parameter array of `Parameter` instances.
256+
public static void SetDefaultEventParameters(params Parameter[] parameters){
257+
SetDefaultEventParameters((IEnumerable<Parameter>)parameters);
258+
}
259+
260+
/// @brief Adds parameters that will be set on every event logged from the SDK.
261+
///
262+
/// Adds parameters that will be set on every event logged from the SDK,
263+
/// including automatic ones. The values passed in the parameters bundle will
264+
/// be added to the map of default event parameters. These parameters persist
265+
/// across app runs. They are of lower precedence than event parameters, so if
266+
/// an event parameter and a parameter set using this API have the same name,
267+
/// the value of the event parameter will be used. The same limitations on
268+
/// event parameters apply to default event parameters.
269+
///
270+
/// @param[in] parameters An enumerable list of `Parameter` instances.
271+
public static void SetDefaultEventParameters(IEnumerable<Parameter> parameters){
272+
StringList parameterNames = new StringList();
273+
VariantList parameterValues = new VariantList();
274+
if (parameters != null) {
275+
foreach (Parameter p in parameters) {
276+
parameterNames.Add(p.Name);
277+
parameterValues.Add(Firebase.Variant.FromObject(p.Value));
278+
}
279+
}
280+
FirebaseAnalyticsInternal.SetDefaultEventParameters(parameterNames, parameterValues);
281+
}
282+
221283
/// Clears all analytics data for this app from the device and resets the app
222284
/// instance id.
223285
public static void ResetAnalyticsData() {

analytics/src/swig/analytics.i

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void LogEvent(const char* name, std::vector<std::string> parameter_names,
6363
if (parameter_names.size() != parameter_values.size()) {
6464
firebase::LogError("LogEvent for %s given different list sizes (%d, %d)",
6565
name, parameter_names.size(), parameter_values.size());
66-
return;
66+
return;
6767
}
6868

6969
size_t number_of_parameters = parameter_names.size();
@@ -78,6 +78,26 @@ void LogEvent(const char* name, std::vector<std::string> parameter_names,
7878
delete[] parameters;
7979
}
8080

81+
// Internal version of SetDefaultEventParameters that takes in two vectors
82+
// of known types and converts them into C++ parameters to pass them along to
83+
// the public SetDefaultEventParameters
84+
void SetDefaultEventParameters(std::vector<std::string> parameter_names,
85+
std::vector<firebase::Variant> parameter_values) {
86+
if (parameter_names.size() != parameter_values.size()) {
87+
firebase::LogError(
88+
"SetDefaultEventParameters given different list sizes (%d, %d)",
89+
parameter_names.size(), parameter_values.size());
90+
return;
91+
}
92+
std::vector<Parameter> parameters;
93+
94+
for(size_t i = 0; i < parameter_names.size(); ++i) {
95+
parameters.push_back(Parameter(parameter_names[i].c_str(), parameter_values[i]));
96+
}
97+
98+
SetDefaultEventParameters(parameters);
99+
}
100+
81101
// Converts from a generic int, int map to the C++ Consent enums
82102
void SetConsentWithInts(const std::map<int, int>& settings) {
83103
std::map<ConsentType, ConsentStatus> converted;
@@ -95,9 +115,12 @@ void SetConsentWithInts(const std::map<int, int>& settings) {
95115
// Initialize / Terminate implicitly called when App is created / destroyed.
96116
%ignore firebase::analytics::Initialize;
97117
%ignore firebase::analytics::Terminate;
98-
// Ignore the SendEvent that takes a Parameter array, as we handle it
118+
// Ignore the LogEvent that takes a Parameter array, as we handle it
99119
// with a custom version instead.
100120
%ignore firebase::analytics::LogEvent(const char*, const Parameter*, size_t);
121+
// Ignore the SetDefaultEventParameters that takes a Parameter array, as we
122+
// handle it with a custom version instead.
123+
%ignore firebase::analytics::SetDefaultEventParameters(const Parameter*, size_t);
101124
// Ignore SetConsent, in order to convert the types with our own function.
102125
%ignore firebase::analytics::SetConsent;
103126
// Ignore the Parameter class, as we don't want to expose that to C# at all.
@@ -120,6 +143,8 @@ namespace firebase {
120143
namespace analytics {
121144
void LogEvent(const char* name, std::vector<std::string> parameter_names,
122145
std::vector<firebase::Variant> parameter_values);
146+
void SetDefaultEventParameters(std::vector<std::string> parameter_names,
147+
std::vector<firebase::Variant> parameter_values);
123148
void SetConsentWithInts(const std::map<int, int>& settings);
124149
} // namespace analytics
125150
} // namespace firebase

docs/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ Support
109109

110110
Release Notes
111111
-------------
112+
### Upcoming
113+
- Changes
114+
- Analytics: Added `SetDefaultEventParameters()` which allows developers to
115+
specify a list of parameters that will be set on every event logged.
116+
- Analytics: Added a new `LogEvent()` that take in a IEnumerable of
117+
parameters.
118+
112119
### 13.5.0
113120
- Changes
114121
- Firebase AI: Add support for receiving Live API Transcripts.

0 commit comments

Comments
 (0)