-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #6 from catoverse/feature/cloud-logger
App Analytics
- Loading branch information
Showing
13 changed files
with
198 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,50 @@ | ||
import 'package:firebase_analytics/firebase_analytics.dart'; | ||
import 'package:firebase_analytics/observer.dart'; | ||
import 'package:firebase_crashlytics/firebase_crashlytics.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
|
||
class AnalyticsService { | ||
final FirebaseAnalytics _analytics = FirebaseAnalytics(); | ||
|
||
FirebaseAnalyticsObserver getAnalyticsObserver() => | ||
FirebaseAnalyticsObserver(analytics: _analytics); | ||
|
||
Future logAppOpen() async { | ||
if (kReleaseMode) { | ||
await _analytics.logAppOpen(); | ||
} | ||
} | ||
|
||
void logLogin() { | ||
if (kReleaseMode) { | ||
_analytics.logLogin(loginMethod: 'google'); | ||
} | ||
} | ||
|
||
Future logShare(String itemId, | ||
{String contentType = 'Post', String method = 'in_app_share'}) async { | ||
if (kReleaseMode) { | ||
await _analytics.logShare( | ||
contentType: contentType, | ||
itemId: itemId, | ||
method: method, | ||
); | ||
} | ||
} | ||
|
||
Future logEvent(String name, {Map<String, dynamic> params = const {}}) async { | ||
if (kReleaseMode) { | ||
await _analytics.logEvent( | ||
name: name, | ||
parameters: params, | ||
); | ||
} | ||
} | ||
|
||
void setUserIdentifier(String userId) { | ||
if (kReleaseMode) { | ||
FirebaseCrashlytics.instance.setUserIdentifier(userId); | ||
_analytics.setUserId(userId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'package:firebase_crashlytics/firebase_crashlytics.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:logger/logger.dart'; | ||
|
||
class CrashlyticsService { | ||
static CrashlyticsService? _instance; | ||
|
||
static CrashlyticsService getInstance() { | ||
if (_instance == null) { | ||
_instance = CrashlyticsService._(FirebaseCrashlytics.instance); | ||
} | ||
|
||
return _instance!; | ||
} | ||
|
||
final FirebaseCrashlytics _crashlyticsService; | ||
CrashlyticsService._( | ||
this._crashlyticsService, | ||
); | ||
|
||
void recordFlutterErrorToCrashlytics(FlutterErrorDetails details) { | ||
_crashlyticsService.recordFlutterError(details); | ||
} | ||
|
||
Future logToCrashlytics( | ||
Level level, List<String> lines, StackTrace stacktrace, | ||
{required bool logwarnings}) async { | ||
if (level == Level.error || level == Level.wtf) { | ||
await _crashlyticsService.recordError( | ||
lines.join('\n'), | ||
stacktrace, | ||
printDetails: true, | ||
fatal: true, | ||
); | ||
} | ||
if (level == Level.warning && logwarnings) { | ||
await _crashlyticsService.recordError( | ||
lines.join('\n'), | ||
stacktrace, | ||
printDetails: true, | ||
); | ||
} | ||
if (level == Level.info || level == Level.verbose || level == Level.debug) { | ||
await _crashlyticsService.log(lines.join('\n')); | ||
} | ||
} | ||
|
||
Future setCustomKeysToTrack(String key, dynamic value) async { | ||
await _crashlyticsService.setCustomKey(key, value); | ||
} | ||
|
||
// Be very careful when you excute this code it will crash the app | ||
// So, be sure to remove it after usage | ||
void crashApp() { | ||
_crashlyticsService.crash(); | ||
} | ||
} | ||
|
||
class CrashlyticsOutput extends LogOutput { | ||
final bool logWarnings; | ||
CrashlyticsOutput({this.logWarnings = false}); | ||
|
||
@override | ||
void output(OutputEvent event) { | ||
try { | ||
CrashlyticsService.getInstance().logToCrashlytics( | ||
event.level, event.lines, StackTrace.current, | ||
logwarnings: logWarnings); | ||
} catch (e) { | ||
print('CRASHLYTICS FAILED: $e'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.