GravityEnginePlugin.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import Flutter
  2. import UIKit
  3. import GravityEngineSDK
  4. public class GravityEnginePlugin: NSObject, FlutterPlugin {
  5. // Method names
  6. private static let METHOD_INITIALIZE = "initialize"
  7. private static let METHOD_TRACK = "track"
  8. private static let METHOD_TRACK_PAY = "trackPay"
  9. // Arguments
  10. private static let ARG_ACCESS_TOKEN = "accessToken"
  11. private static let ARG_DEBUG = "debug"
  12. private static let ARG_CLIENT_ID = "clientId"
  13. private static let ARG_CHANNEL = "channel"
  14. //ios>>>
  15. private static let ARG_APPID = "appid"
  16. private static let ARG_IDFA = "idfa"
  17. private static let ARG_IDFV = "idfv"
  18. //<<<ios
  19. private static let ARG_EVENT_ID = "eventId"
  20. private static let ARG_EVENT_PARAMS = "eventParams"
  21. private static let ARG_ORDER_NO = "orderNo"
  22. private static let ARG_AMOUNT = "amount"
  23. private static let ARG_CURRENCY = "currency"
  24. private static let ARG_PAY_TYPE = "payType"
  25. private static let ARG_ITEM_NAME = "itemName"
  26. private var gravityEngineSDK: GravityEngineSDK?
  27. private var isInitialized = false
  28. public static func register(with registrar: FlutterPluginRegistrar) {
  29. let channel = FlutterMethodChannel(name: "gravity_engine", binaryMessenger: registrar.messenger())
  30. let instance = GravityEnginePlugin()
  31. registrar.addMethodCallDelegate(instance, channel: channel)
  32. }
  33. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  34. switch call.method {
  35. case GravityEnginePlugin.METHOD_INITIALIZE:
  36. initialize(call: call, result: result)
  37. print("GravityEnginePlugin.METHOD_INITIALIZE = \(call.method)");
  38. case GravityEnginePlugin.METHOD_TRACK:
  39. track(call: call, result: result)
  40. print("GravityEnginePlugin.METHOD_TRACK = \(call.method)");
  41. case GravityEnginePlugin.METHOD_TRACK_PAY:
  42. trackPay(call: call, result: result)
  43. default:
  44. result(FlutterMethodNotImplemented)
  45. }
  46. }
  47. //init
  48. private func initialize(call: FlutterMethodCall, result: @escaping FlutterResult) {
  49. guard let args = call.arguments as? [String: Any],
  50. let accessToken = args[GravityEnginePlugin.ARG_ACCESS_TOKEN] as? String,
  51. let appid = args[GravityEnginePlugin.ARG_APPID] as? String,
  52. let idfa = args[GravityEnginePlugin.ARG_IDFA] as? String,
  53. let idfv = args[GravityEnginePlugin.ARG_IDFV] as? String,
  54. let debug = args[GravityEnginePlugin.ARG_DEBUG] as? Bool,
  55. let clientId = args[GravityEnginePlugin.ARG_CLIENT_ID] as? String,
  56. let channel = args[GravityEnginePlugin.ARG_CHANNEL] as? String else {
  57. result(FlutterError(code: "-1", message: "Missing required arguments", details: nil))
  58. return
  59. }
  60. //配置引力引擎
  61. let config = GEConfig();
  62. config.appid = appid;
  63. config.accessToken = accessToken;
  64. if debug{
  65. config.debugMode = GravityEngineDebugMode.on;
  66. }else{
  67. config.debugMode = [];
  68. }
  69. GravityEngineSDK.start(with: config);
  70. let instance = GravityEngineSDK.sharedInstance(withAppid: config.appid);
  71. //全局引力引擎实例
  72. gravityEngineSDK = instance
  73. // 开启自动采集
  74. gravityEngineSDK?.enableAutoTrack(GravityEngineAutoTrackEventType.eventTypeAll);
  75. //获取当前版本号
  76. var appVersionName = 1
  77. if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  78. appVersionName = Int(appVersion) ?? 1
  79. }
  80. //自动归因
  81. let enableAsa = true
  82. //其他参数
  83. let userCaid1MD5 = ""
  84. let userCaid2MD5 = ""
  85. let enableSyncAttribution = true
  86. //初始化sdk
  87. gravityEngineSDK?.initializeGravityEngine(withClientId: idfv, withUserName: idfv, withVersion: Int32(appVersionName), withAsaEnable:enableAsa, withIdfa:idfa, withIdfv:idfv, withCaid1:userCaid1MD5, withCaid2:userCaid2MD5, withSyncAttribution:enableSyncAttribution, withSuccessCallback: { response in
  88. print("gravity engine initialize success, response is ", response);
  89. self.isInitialized = true
  90. result(response)
  91. }, withErrorCallback: { error in
  92. print("gravity engine initialize failed, and error is", error);
  93. result(FlutterError(code: "-1", message: "Initialization failed", details: error.localizedDescription))
  94. });
  95. // 获取用户信息
  96. // instance?.queryUserInfo(successCallback: { response in
  97. //// print("user info \(response)")
  98. // }, withErrorCallback: { error in
  99. //// print("get user info error \(error)")
  100. // })
  101. //设置公共属性
  102. var superProperties: [String : Any] = ["channel":channel]
  103. gravityEngineSDK?.setSuperProperties(superProperties)
  104. }
  105. //event
  106. private func track(call: FlutterMethodCall, result: @escaping FlutterResult) {
  107. guard let args = call.arguments as? [String: Any],
  108. let eventId = args[GravityEnginePlugin.ARG_EVENT_ID] as? String else {
  109. result(FlutterError(code: "-1", message: "Missing eventId", details: nil))
  110. return
  111. }
  112. guard isInitialized, let sdk = gravityEngineSDK else {
  113. result(FlutterError(code: "-1", message: "SDK not initialized", details: nil))
  114. return
  115. }
  116. let eventParams = args[GravityEnginePlugin.ARG_EVENT_PARAMS] as? [String: Any]
  117. sdk.track(eventId, properties: eventParams)
  118. result(nil)
  119. }
  120. //pay
  121. private func trackPay(call: FlutterMethodCall, result: @escaping FlutterResult) {
  122. guard let args = call.arguments as? [String: Any],
  123. let orderNo = args[GravityEnginePlugin.ARG_ORDER_NO] as? String,
  124. let amount = args[GravityEnginePlugin.ARG_AMOUNT] as? Int,
  125. let currency = args[GravityEnginePlugin.ARG_CURRENCY] as? String,
  126. let itemName = args[GravityEnginePlugin.ARG_ITEM_NAME] as? String,
  127. let payType = args[GravityEnginePlugin.ARG_PAY_TYPE] as? String else {
  128. result(FlutterError(code: "-1", message: "Missing required arguments", details: nil))
  129. return
  130. }
  131. guard isInitialized, let sdk = gravityEngineSDK else {
  132. result(FlutterError(code: "-1", message: "SDK not initialized", details: nil))
  133. return
  134. }
  135. sdk.trackPayEvent(withAmount: Int32(amount), withPayType: payType, withOrderId: orderNo, withPayReason: itemName, withPayMethod: currency)
  136. result(nil)
  137. }
  138. }