Skip to content

Set user_hash, user_id, email in intercomSettings #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions intercom_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Bump Intercom iOS SDK version to 18.2.0
* Added API `isUserLoggedIn`.
* Added API `fetchLoggedInUserAttributes`.
* Fixed [#479](https://github.com/v3rm0n/intercom_flutter/issues/479).
* Fixed [#481](https://github.com/v3rm0n/intercom_flutter/issues/481).

## 9.1.1

Expand Down
2 changes: 0 additions & 2 deletions intercom_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ But you can pre-define some Intercom settings, if you want (optional).
- [ ] handlePush
- [ ] displayCarousel
- [ ] displayHelpCenterCollections
- [ ] isUserLoggedIn
- [ ] fetchLoggedInUserAttributes

## Using Intercom keys with `--dart-define`

Expand Down
2 changes: 1 addition & 1 deletion intercom_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
intercom_flutter_platform_interface: ^2.0.2
intercom_flutter_web: ^1.1.4
intercom_flutter_web: ^1.1.5

dev_dependencies:
flutter_test:
Expand Down
5 changes: 5 additions & 0 deletions intercom_flutter_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.1.5

* Set user_hash, user_id, email in intercomSettings.
* Implemented methods `isUserLoggedIn` and `fetchLoggedInUserAttributes`.

## 1.1.4

* Updated dependency `intercom_flutter_platform_interface: ^2.0.2`.
Expand Down
2 changes: 0 additions & 2 deletions intercom_flutter_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ But you can pre-define some Intercom settings, if you want (optional).
- handlePush
- displayCarousel
- displayHelpCenterCollections
- isUserLoggedIn
- fetchLoggedInUserAttributes

[1]: ../intercom_flutter

Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,13 @@ void main() {
testWidgets('displayHome', (WidgetTester _) async {
expect(plugin.displayHome(), completes);
});

testWidgets('isUserLoggedIn', (WidgetTester _) async {
expect(plugin.isUserLoggedIn(), completes);
});

testWidgets('fetchLoggedInUserAttributes', (WidgetTester _) async {
expect(plugin.fetchLoggedInUserAttributes(), completes);
});
});
}
58 changes: 54 additions & 4 deletions intercom_flutter_web/lib/intercom_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
globalContext.callMethod(
'Intercom'.toJS,
'update'.toJS,
{'user_hash': userHash}.jsify(),
updateIntercomSettings('user_hash', userHash).jsify(),
);
print("user hash added");
}
Expand All @@ -93,7 +93,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
globalContext.callMethod(
'Intercom'.toJS,
'update'.toJS,
{'user_id': userId}.jsify(),
updateIntercomSettings('user_id', userId).jsify(),
);
// send the success callback only as web does not support the statusCallback.
statusCallback?.onSuccess?.call();
Expand All @@ -102,7 +102,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
globalContext.callMethod(
'Intercom'.toJS,
'update'.toJS,
{'email': email}.jsify(),
updateIntercomSettings('email', email).jsify(),
);
// send the success callback only as web does not support the statusCallback.
statusCallback?.onSuccess?.call();
Expand All @@ -120,7 +120,7 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
globalContext.callMethod(
'Intercom'.toJS,
'update'.toJS,
{'user_id': userId}.jsify(),
updateIntercomSettings('user_id', userId).jsify(),
);
// send the success callback only as web does not support the statusCallback.
statusCallback?.onSuccess?.call();
Expand Down Expand Up @@ -213,6 +213,11 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {

@override
Future<void> logout() async {
// shutdown will effectively clear out any user data that you have been passing through the JS API.
// but not from intercomSettings
// so manually clear some intercom settings
removeIntercomSettings(['user_hash', 'user_id', 'email']);
// shutdown
globalContext.callMethod('Intercom'.toJS, 'shutdown'.toJS);
print("logout");
}
Expand Down Expand Up @@ -292,6 +297,35 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {
print("Launched Home space");
}

@override
Future<bool> isUserLoggedIn() async {
// There is no direct JS API available
// Here we check if intercomSettings has user_id or email then user is
// logged in
var settings = getIntercomSettings();
var user_id = settings['user_id'] as String? ?? "";
var email = settings['email'] as String? ?? "";

return user_id.isNotEmpty || email.isNotEmpty;
}

@override
Future<Map<String, dynamic>> fetchLoggedInUserAttributes() async {
// There is no direct JS API available
// Just return the user_id or email from intercomSettings
var settings = getIntercomSettings();
var user_id = settings['user_id'] as String? ?? "";
var email = settings['email'] as String? ?? "";

if (user_id.isNotEmpty) {
return {'user_id': user_id};
} else if (email.isNotEmpty) {
return {'email': email};
}

return {};
}

/// get the [window.intercomSettings]
Map<dynamic, dynamic> getIntercomSettings() {
if (globalContext.hasProperty('intercomSettings'.toJS).toDart) {
Expand All @@ -316,4 +350,20 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform {

return intercomSettings;
}

/// Remove properties from [window.intercomSettings]
Map<dynamic, dynamic> removeIntercomSettings(List<String> keys) {
var intercomSettings = getIntercomSettings();

// remove the keys
for (var key in keys) {
intercomSettings.remove(key);
}

// Update the [window.intercomSettings]
globalContext.setProperty(
"intercomSettings".toJS, intercomSettings.jsify());

return intercomSettings;
}
}
2 changes: 1 addition & 1 deletion intercom_flutter_web/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: intercom_flutter_web
description: Web platform implementation of intercom_flutter
version: 1.1.4
version: 1.1.5
homepage: https://github.com/v3rm0n/intercom_flutter

flutter:
Expand Down
Loading