GravityEnginePlugin.swift 6.8 KB

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