Skip to content

Commit c732301

Browse files
committed
feat(firebase_analytics): add getSessionId
1 parent 4e9bb69 commit c732301

File tree

9 files changed

+112
-0
lines changed

9 files changed

+112
-0
lines changed

packages/firebase_analytics/firebase_analytics/android/src/main/java/io/flutter/plugins/firebase/analytics/FlutterFirebaseAnalyticsPlugin.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.google.android.gms.tasks.Tasks;
1414
import com.google.firebase.FirebaseApp;
1515
import com.google.firebase.analytics.FirebaseAnalytics;
16+
17+
import io.flutter.Log;
1618
import io.flutter.embedding.engine.plugins.FlutterPlugin;
1719
import io.flutter.plugin.common.BinaryMessenger;
1820
import io.flutter.plugin.common.MethodCall;
@@ -136,6 +138,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
136138
case "Analytics#getAppInstanceId":
137139
methodCallTask = handleGetAppInstanceId();
138140
break;
141+
case "Analytics#getSessionId":
142+
methodCallTask = handleGetSessionId();
143+
break;
139144
default:
140145
result.notImplemented();
141146
return;
@@ -154,6 +159,22 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
154159
});
155160
}
156161

162+
private Task<Long> handleGetSessionId() {
163+
TaskCompletionSource<Long> taskCompletionSource = new TaskCompletionSource<>();
164+
165+
cachedThreadPool.execute(
166+
() -> {
167+
try {
168+
Log.d("ANALYTICS: ", analytics.getSessionId().toString());
169+
taskCompletionSource.setResult(Tasks.await(analytics.getSessionId()));
170+
} catch (Exception e) {
171+
taskCompletionSource.setException(e);
172+
}
173+
});
174+
175+
return taskCompletionSource.getTask();
176+
}
177+
157178
private Task<Void> handleLogEvent(final Map<String, Object> arguments) {
158179
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
159180

packages/firebase_analytics/firebase_analytics/ios/Classes/FLTFirebaseAnalyticsPlugin.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,25 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
7272
[self setDefaultEventParameters:call.arguments withMethodCallResult:methodCallResult];
7373
} else if ([@"Analytics#getAppInstanceId" isEqualToString:call.method]) {
7474
[self getAppInstanceIdWithMethodCallResult:methodCallResult];
75+
} else if ([@"Analytics#getSessionId" isEqualToString:call.method]) {
76+
[self getSessionIdWithMethodCallResult:methodCallResult];
7577
} else {
7678
result(FlutterMethodNotImplemented);
7779
}
7880
}
7981

8082
#pragma mark - Firebase Analytics API
8183

84+
- (void)getSessionIdWithMethodCallResult:(FLTFirebaseMethodCallResult *)result {
85+
[FIRAnalytics sessionIDWithCompletion:^(int64_t sessionID, NSError * _Nullable error) {
86+
if (error != nil) {
87+
result.error(nil, nil, nil, error);
88+
} else {
89+
result.success([NSNumber numberWithLongLong:sessionID]);
90+
}
91+
}];
92+
}
93+
8294
- (void)logEvent:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
8395
NSString *eventName = arguments[kFLTFirebaseAnalyticsEventName];
8496
id parameterMap = arguments[kFLTFirebaseAnalyticsParameters];

packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class FirebaseAnalytics extends FirebasePluginPlatform {
7575
return _delegate.getAppInstanceId();
7676
}
7777

78+
/// Retrieves the session id from the client. Returns null if
79+
/// analyticsStorageConsentGranted is false or session is expired.
80+
Future<int?> getSessionId() {
81+
return _delegate.getSessionId();
82+
}
83+
7884
/// Logs a custom Flutter Analytics event with the given [name] and event
7985
/// [parameters].
8086
///

packages/firebase_analytics/firebase_analytics_platform_interface/lib/src/method_channel/method_channel_firebase_analytics.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ class MethodChannelFirebaseAnalytics extends FirebaseAnalyticsPlatform {
4444
return Future.value(true);
4545
}
4646

47+
@override
48+
Future<int?> getSessionId() {
49+
try {
50+
return channel.invokeMethod<int>('Analytics#getSessionId');
51+
} catch (e, s) {
52+
convertPlatformException(e, s);
53+
}
54+
}
55+
4756
@override
4857
Future<void> logEvent({
4958
required String name,

packages/firebase_analytics/firebase_analytics_platform_interface/lib/src/platform_interface/platform_interface_firebase_analytics.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ abstract class FirebaseAnalyticsPlatform extends PlatformInterface {
7474
throw UnimplementedError('getAppInstanceId() is not implemented');
7575
}
7676

77+
Future<int?> getSessionId() {
78+
throw UnimplementedError('getSessionId() is not implemented');
79+
}
80+
7781
/// Logs a custom Flutter Analytics event with the given [name] and event
7882
/// [parameters].
7983
///

packages/firebase_analytics/firebase_analytics_platform_interface/test/method_channel_tests/method_channel_firebase_analytics_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ void main() {
133133
);
134134
});
135135

136+
test('getSessionId', () async {
137+
await analytics.getSessionId();
138+
expect(
139+
methodCallLogger,
140+
<Matcher>[
141+
isMethodCall(
142+
'Analytics#getSessionId',
143+
arguments: null,
144+
),
145+
],
146+
);
147+
});
148+
136149
test('logEvent', () async {
137150
await analytics.logEvent(
138151
name: 'test-event',

packages/firebase_analytics/firebase_analytics_platform_interface/test/platform_interface_tests/platform_interface_analytics_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ void main() {
211211
),
212212
);
213213
});
214+
215+
test('throws if .getSessionId() not implemented', () async {
216+
await expectLater(
217+
() => firebaseAnalyticsPlatform.getSessionId(),
218+
throwsA(
219+
isA<UnimplementedError>().having(
220+
(e) => e.message,
221+
'message',
222+
'getSessionId() is not implemented',
223+
),
224+
),
225+
);
226+
});
214227
});
215228
}
216229

packages/firebase_analytics/firebase_analytics_web/lib/firebase_analytics_web.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class FirebaseAnalyticsWeb extends FirebaseAnalyticsPlatform {
4343
return analytics_interop.Analytics.isSupported();
4444
}
4545

46+
@override
47+
Future<int?> getSessionId() {
48+
// TODO: change UnimplementedError to UnsupportedError
49+
throw UnimplementedError('getSessionId() is not supported on Web.');
50+
}
51+
4652
@override
4753
Future<void> logEvent({
4854
required String name,

tests/integration_test/firebase_analytics/firebase_analytics_e2e_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io';
6+
57
import 'package:firebase_analytics/firebase_analytics.dart';
68
import 'package:firebase_core/firebase_core.dart';
79
import 'package:flutter/foundation.dart';
@@ -272,5 +274,31 @@ void main() {
272274
expect(result2, isA<String>());
273275
}
274276
});
277+
278+
test(
279+
'getSessionId',
280+
() async {
281+
// FIXME: Android returns null for getSessionId in tests,
282+
// but works when running in the context of an app.
283+
// Need to investigate why this is the case.
284+
if (kIsWeb) {
285+
await expectLater(
286+
FirebaseAnalytics.instance.getSessionId(),
287+
throwsA(isA<UnimplementedError>()),
288+
);
289+
} else {
290+
await expectLater(
291+
FirebaseAnalytics.instance.setConsent(
292+
analyticsStorageConsentGranted: true,
293+
adStorageConsentGranted: true,
294+
),
295+
completes,
296+
);
297+
298+
final result = await FirebaseAnalytics.instance.getSessionId();
299+
expect(result, isA<int>());
300+
}
301+
},
302+
);
275303
});
276304
}

0 commit comments

Comments
 (0)