Skip to content

Commit 8d2812c

Browse files
authored
Support FirebaseAnalytics.GetSessionIdAsync(), and add to testapp. (#526)
* Add test to Analytics for GetSessionId. * Add SWIG_FUTURE macro for Future<int64_t>. * Name method GetSessionIdAsync. * Try explicitly using long long. * Remove explicit SWIG_FUTURE * Revert "Remove explicit SWIG_FUTURE" This reverts commit 808fd9b. * Change future type to long. * Include <cstdint> before stdint.i, and use int64_t again. * Revert to using long long instead of int64_t. * Remove cstdint, no longer needed. * Revert "Remove cstdint, no longer needed." This reverts commit 5afb56b. * Add explanatory comment. * Fix testapp. * Fix delay in C# code. * Add more debug output. * Build error * Remove usage of InnerException * Move SetConsent test first. * Fix build error.
1 parent f6b920b commit 8d2812c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

analytics/src/swig/analytics.i

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@
6666
%include "firebase/analytics/parameter_names.h"
6767
%include "firebase/analytics/user_property_names.h"
6868

69-
69+
// Including cstdint before stdint.i ensures the int64_t typedef is correct,
70+
// otherwise on some platforms it is defined as "long long int" instead of
71+
// "long int".
72+
#include <cstdint>
7073
%include "stdint.i"
7174

7275
namespace firebase {
@@ -288,6 +291,10 @@ class ParameterCopy : private firebase::analytics::Parameter {
288291
}
289292
%}
290293

294+
// GetSessionId returns Future<long long> in SWIG.
295+
%include "app/src/swig/future.i"
296+
%SWIG_FUTURE(Future_LongLong, long, internal, long long, FirebaseException)
297+
291298
%include "analytics/src/include/firebase/analytics.h"
292299

293300
%rename(ConsentType) firebase::analytics::ConsentType;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ public Task<string> DisplayAnalyticsInstanceId() {
151151
}).Unwrap();
152152
}
153153

154+
// Get the current app session ID
155+
public Task<long> DisplaySessionId() {
156+
return FirebaseAnalytics.GetSessionIdAsync().ContinueWithOnMainThread(task => {
157+
if (task.IsCanceled) {
158+
DebugLog("Session ID fetch was canceled.");
159+
} else if (task.IsFaulted) {
160+
DebugLog(String.Format("Encounted an error fetching session ID {0}",
161+
task.Exception.ToString()));
162+
} else if (task.IsCompleted) {
163+
DebugLog(String.Format("Session ID: {0}", task.Result));
164+
}
165+
return task;
166+
}).Unwrap();
167+
}
168+
154169
// Output text to the debug log text field, as well as the console.
155170
public void DebugLog(string s) {
156171
print(s);
@@ -207,6 +222,9 @@ void GUIDisplayControls() {
207222
if (GUILayout.Button("Show Analytics Instance ID")) {
208223
DisplayAnalyticsInstanceId();
209224
}
225+
if (GUILayout.Button("Show Session ID")) {
226+
DisplaySessionId();
227+
}
210228
if (GUILayout.Button("Test SetConsent")) {
211229
AnalyticsSetConsent();
212230
}

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

Lines changed: 22 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+
TestGetSessionId,
3334
TestAnalyticsSetConsentDoesNotThrow,
3435
TestInstanceIdChangeAfterReset,
3536
TestResetAnalyticsData,
@@ -194,6 +195,27 @@ Task TestInstanceIdChangeAfterReset() {
194195
return tcs.Task;
195196
}
196197

198+
Task TestGetSessionId() {
199+
// Depending on platform, GetSessionId needs a few seconds for Analytics
200+
// to initialize (especially on iOS simulator). Pause for 5 seconds before
201+
// running this test.
202+
var tcs = new TaskCompletionSource<bool>();
203+
Task.Delay(TimeSpan.FromSeconds(5)).ContinueWithOnMainThread(task_ => {
204+
base.DisplaySessionId().ContinueWithOnMainThread(task => {
205+
if (task.IsCanceled) {
206+
tcs.TrySetException(new Exception("Unexpectedly canceled"));
207+
} else if (task.IsFaulted) {
208+
tcs.TrySetException(task.Exception);
209+
} else if (task.Result == 0) {
210+
tcs.TrySetException(new Exception("Zero ID returned"));
211+
} else {
212+
tcs.TrySetResult(true);
213+
}
214+
});
215+
});
216+
return tcs.Task;
217+
}
218+
197219
Task TestResetAnalyticsData() {
198220
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
199221
base.DisplayAnalyticsInstanceId().ContinueWithOnMainThread(task => {

0 commit comments

Comments
 (0)