umeng_analytics_plugin.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/services.dart';
  4. /// Umeng analytics plugin
  5. class UmengAnalyticsPlugin {
  6. /// Method channel
  7. static const MethodChannel _channel = MethodChannel('jitao.tech/umeng_analytics_plugin');
  8. /// Initialize plugin with configurations.
  9. ///
  10. /// [androidKey] is for android app key.
  11. /// [iosKey] is for ios app key.
  12. /// [channel] is distribution for this app, can leave it empty.
  13. /// [logEnabled] turn on or off log, default for false.
  14. /// [encryptEnabled] turn on or off log encrypt, default for false.
  15. /// [sessionContinueMillis] time in milliseconds to upload analytics data.
  16. /// [catchUncaughtExceptions] whether to catch uncaught exceptions, default for true.
  17. /// [pageCollectionMode] how to collect page data, leave it AUTO is ok, for future details, read umeng doc.
  18. static Future<bool> init({
  19. @required String androidKey,
  20. @required String iosKey,
  21. String channel,
  22. bool logEnabled = false,
  23. bool encryptEnabled = false,
  24. int sessionContinueMillis = 30000,
  25. bool catchUncaughtExceptions = true,
  26. String pageCollectionMode = 'AUTO',
  27. }) async {
  28. Map<String, dynamic> map = {
  29. 'androidKey': androidKey,
  30. 'iosKey': iosKey,
  31. 'channel': channel,
  32. 'logEnabled': logEnabled,
  33. 'encryptEnabled': encryptEnabled,
  34. 'sessionContinueMillis': sessionContinueMillis,
  35. 'catchUncaughtExceptions': catchUncaughtExceptions,
  36. 'pageCollectionMode': pageCollectionMode,
  37. };
  38. return _channel.invokeMethod<bool>('init', map);
  39. }
  40. /// Send a page start event for [viewName]
  41. static Future<bool> pageStart(String viewName) async {
  42. Map<String, dynamic> map = {
  43. 'viewName': viewName,
  44. };
  45. return _channel.invokeMethod<bool>('pageStart', map);
  46. }
  47. /// Send a page end event for [viewName]
  48. static Future<bool> pageEnd(String viewName) async {
  49. Map<String, dynamic> map = {
  50. 'viewName': viewName,
  51. };
  52. return _channel.invokeMethod<bool>('pageEnd', map);
  53. }
  54. /// Send a general event for [eventId] with a [label]
  55. static Future<bool> event(String eventId, {String label= 'label'}) async {
  56. Map<String, dynamic> map = {
  57. 'eventId': eventId,
  58. };
  59. if (label != null) {
  60. map['label'] = label;
  61. }
  62. return _channel.invokeMethod<bool>('event', map);
  63. }
  64. }