Skip to content

Commit a5dc49c

Browse files
Add Analytics SetConsent method, with test. (#560)
* Add test for Analytics SetConsent method. * Add std_map swig include. * Update SWIG wrapper to create SetConsent(IDictionary) * Add namespace * Fix up the ConsentMap, and the Internal function * Remove commented out using statements Co-authored-by: a-maurice <amaurice@google.com>
1 parent b3711a6 commit a5dc49c

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

analytics/src/swig/analytics.i

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
%{#include "app/src/export_fix.h"%}
2222
#endif
2323

24+
%include "std_map.i"
25+
2426
%pragma(csharp) moduleclassmodifiers="public sealed class"
2527
%pragma(csharp) modulecode=%{
2628
// Hold a reference to the default app when methods from this module are
@@ -45,6 +47,11 @@
4547
#include "analytics/src/include/firebase/analytics/user_property_names.h"
4648
%}
4749

50+
%rename(kConsentTypeAdStorage) firebase::analytics::kConsentTypeAdStorage;
51+
%rename(kConsentTypeAnalyticsStorage) firebase::analytics::kConsentTypeAnalyticsStorage;
52+
%rename(kConsentStatusGranted) firebase::analytics::kConsentStatusGranted;
53+
%rename(kConsentStatusDenied) firebase::analytics::kConsentStatusDenied;
54+
4855
// Constant renaming must happen before SWIG_CONSTANT_HEADERS is included.
4956
%rename(kParameterAchievementId) firebase::analytics::kParameterAchievementID;
5057
%rename(kParameterGroupId) firebase::analytics::kParameterGroupID;
@@ -231,6 +238,9 @@ class ParameterCopy : private firebase::analytics::Parameter {
231238
// Initialize / Terminate implicitly called when App is created / destroyed.
232239
%ignore Initialize;
233240
%ignore Terminate;
241+
// SetConsent handled via SetConsentInternal below.
242+
%ignore SetConsent;
243+
234244
} // namespace analytics
235245
} // namespace firebase
236246

@@ -278,5 +288,49 @@ class ParameterCopy : private firebase::analytics::Parameter {
278288
}
279289
%}
280290

281-
282291
%include "analytics/src/include/firebase/analytics.h"
292+
293+
%rename(ConsentType) firebase::analytics::ConsentType;
294+
%rename(ConsentStatus) firebase::analytics::ConsentStatus;
295+
// Add a swig C++ function to call into the Analytics C++ implementation.
296+
%{
297+
namespace firebase {
298+
namespace analytics {
299+
300+
void SetConsentInternal(std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus> *ptr) {
301+
firebase::analytics::SetConsent(*ptr);
302+
}
303+
304+
} // namespace analytics
305+
} // namespace firebase
306+
%}
307+
// The definition on the C++ side, so that swig is aware of the function's existence.
308+
void SetConsentInternal(std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus> *ptr);
309+
310+
%typemap(csclassmodifiers) firebase::analytics::ConsentType "enum";
311+
%typemap(csclassmodifiers) firebase::analytics::ConsentStatus "enum";
312+
313+
%typemap(csclassmodifiers) std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus> "internal class"
314+
%template(ConsentMap) std::map<firebase::analytics::ConsentType, firebase::analytics::ConsentStatus>;
315+
316+
namespace firebase {
317+
namespace analytics {
318+
319+
%pragma(csharp) modulecode=%{
320+
/// @brief Sets the applicable end user consent state (e.g., for device
321+
/// identifiers) for this app on this device.
322+
///
323+
/// Use the consent map to specify individual consent type values. Settings are
324+
/// persisted across app sessions. By default consent types are set to
325+
/// "granted".
326+
public static void SetConsent(System.Collections.Generic.IDictionary<ConsentType, ConsentStatus> consentSettings) {
327+
ConsentMap consentSettingsMap = new ConsentMap();
328+
foreach(var kv in consentSettings) {
329+
consentSettingsMap[kv.Key] = kv.Value;
330+
}
331+
SetConsentInternal(consentSettingsMap);
332+
}
333+
%}
334+
335+
} // namespace analytics
336+
} // namespace firebase

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Firebase.Sample.Analytics {
1717
using Firebase.Analytics;
1818
using Firebase.Extensions;
1919
using System;
20+
using System.Collections.Generic;
2021
using System.Threading.Tasks;
2122
using UnityEngine;
2223

@@ -121,6 +122,20 @@ public void ResetAnalyticsData() {
121122
FirebaseAnalytics.ResetAnalyticsData();
122123
}
123124

125+
public void AnalyticsSetConsent() {
126+
FirebaseAnalytics.SetConsent(new Dictionary<ConsentType, ConsentStatus>()
127+
{
128+
{ ConsentType.AnalyticsStorage, ConsentStatus.Denied },
129+
{ ConsentType.AdStorage, ConsentStatus.Denied }
130+
});
131+
FirebaseAnalytics.SetConsent(new Dictionary<ConsentType, ConsentStatus>());
132+
FirebaseAnalytics.SetConsent(new Dictionary<ConsentType, ConsentStatus>()
133+
{
134+
{ ConsentType.AnalyticsStorage, ConsentStatus.Granted },
135+
{ ConsentType.AdStorage, ConsentStatus.Granted }
136+
});
137+
}
138+
124139
// Get the current app instance ID.
125140
public Task<string> DisplayAnalyticsInstanceId() {
126141
return FirebaseAnalytics.GetAnalyticsInstanceIdAsync().ContinueWithOnMainThread(task => {
@@ -192,6 +207,9 @@ void GUIDisplayControls() {
192207
if (GUILayout.Button("Show Analytics Instance ID")) {
193208
DisplayAnalyticsInstanceId();
194209
}
210+
if (GUILayout.Button("Test SetConsent")) {
211+
AnalyticsSetConsent();
212+
}
195213
GUILayout.EndVertical();
196214
GUILayout.EndScrollView();
197215
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public override void Start() {
3030
TestAnalyticsScoreDoesNotThrow,
3131
TestAnalyticsGroupJoinDoesNotThrow,
3232
TestAnalyticsLevelUpDoesNotThrow,
33+
TestAnalyticsSetConsentDoesNotThrow,
3334
TestInstanceIdChangeAfterReset,
3435
TestResetAnalyticsData,
3536
// Temporarily disabled until this test is deflaked. b/143603151
@@ -87,6 +88,12 @@ Task TestAnalyticsLevelUpDoesNotThrow() {
8788
});
8889
}
8990

91+
Task TestAnalyticsSetConsentDoesNotThrow() {
92+
return WrapWithTask(() => {
93+
base.AnalyticsSetConsent();
94+
return true;
95+
});
96+
}
9097
Task TestCheckAndFixDependenciesInvalidOperation() {
9198
// Only run the test on Android, as CheckAndFixDependenciesAsync is short
9299
// lived on other platforms, and thus could finish before the extra call.

0 commit comments

Comments
 (0)