-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppcuesPlugin.kt
More file actions
184 lines (165 loc) · 5.59 KB
/
Copy pathAppcuesPlugin.kt
File metadata and controls
184 lines (165 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.appcues.sdk.capacitor
import android.content.Intent
import android.net.Uri
import com.appcues.AnalyticType
import com.appcues.AnalyticsListener
import com.appcues.Appcues
import com.getcapacitor.JSObject
import com.getcapacitor.Plugin
import com.getcapacitor.PluginCall
import com.getcapacitor.PluginMethod
import com.getcapacitor.annotation.CapacitorPlugin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONObject
@Suppress("unused")
@CapacitorPlugin(name = "Appcues")
class AppcuesPlugin : Plugin() {
private lateinit var implementation: Appcues
private val mainScope = CoroutineScope(Dispatchers.Main)
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun initialize(call: PluginCall) {
val accountId = call.getString("accountId")
val applicationId = call.getString("applicationId")
if (accountId != null && applicationId != null) {
implementation = Appcues(context, accountId, applicationId) {
AppcuesPluginConfig(call).applyAppcuesConfig(this)
}
implementation.analyticsListener = object : AnalyticsListener {
override fun trackedAnalytic(
type: AnalyticType,
value: String?,
properties: Map<String, Any>?,
isInternal: Boolean
) {
val data = JSObject().apply {
put("analytic", type.name)
put("value", value ?: "")
put("properties", properties ?: emptyMap<String, Any>())
put("isInternal", isInternal)
}
notifyListeners("analytics", data)
}
}
call.resolve()
}
}
@PluginMethod
fun version(call: PluginCall) {
call.resolve(
JSObject().apply {
put("version", implementation.version)
}
)
}
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun identify(call: PluginCall) {
val userId = call.getString("userId")
if (userId != null) {
implementation.identify(userId, call.getPropertiesMap())
}
call.resolve()
}
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun group(call: PluginCall) {
val groupId = call.getString("groupId")
implementation.group(groupId, call.getPropertiesMap())
call.resolve()
}
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun anonymous(call: PluginCall) {
implementation.anonymous()
call.resolve()
}
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun screen(call: PluginCall) {
val title = call.getString("title")
if (title != null) {
implementation.screen(title, call.getPropertiesMap())
}
call.resolve()
}
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun track(call: PluginCall) {
val name = call.getString("name")
if (name != null) {
implementation.track(name, call.getPropertiesMap())
}
call.resolve()
}
@PluginMethod
fun show(call: PluginCall) {
val experienceId = call.getString("experienceId")
if (experienceId != null) {
mainScope.launch {
if (implementation.show(experienceId)) {
call.resolve()
} else {
call.reject("unable to show experience $experienceId")
}
}
} else {
call.reject("Missing experienceId")
}
}
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun debug(call: PluginCall) {
implementation.debug(activity)
call.resolve()
}
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
fun reset(call: PluginCall) {
implementation.reset()
call.resolve()
}
@PluginMethod
fun didHandleURL(call: PluginCall) {
val url = call.getString("url")
val uri = Uri.parse(url)
if (activity != null) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = uri
val handled = implementation.onNewIntent(activity, intent)
call.resolve(
JSObject().apply {
put("handled", handled)
}
)
} else {
call.reject("no-activity", "unable to handle the URL, no current running Activity found")
}
}
private fun PluginCall.getPropertiesMap(): Map<String, Any>? {
return data.getJSObject("properties")?.let {
mapProperties(it)
}
}
private fun mapProperties(obj: JSONObject): Map<String, Any> {
val mapped = mutableMapOf<String, Any>()
obj.keys().forEach {
val value = obj[it]
mapped[it] = when (value) {
is JSONObject -> mapProperties(value)
is JSONArray -> mapProperties(value)
else -> value
}
}
return mapped
}
private fun mapProperties(array: JSONArray): List<Any> {
val mapped = mutableListOf<Any>()
(0 until array.length()).forEach {
val value = array.get(it)
mapped.add(
when (value) {
is JSONObject -> mapProperties(value)
is JSONArray -> mapProperties(value)
else -> value
}
)
}
return mapped
}
}