@capgo/capacitor-rudderstack 8.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,256 @@
1
+ package app.capgo.rudderstack;
2
+
3
+ import android.content.Context;
4
+ import androidx.annotation.Nullable;
5
+ import com.getcapacitor.JSObject;
6
+ import com.rudderstack.android.sdk.core.RudderClient;
7
+ import com.rudderstack.android.sdk.core.RudderConfig;
8
+ import com.rudderstack.android.sdk.core.RudderOption;
9
+ import com.rudderstack.android.sdk.core.RudderProperty;
10
+ import com.rudderstack.android.sdk.core.RudderTraits;
11
+ import com.rudderstack.android.sdk.core.RudderTraitsBuilder;
12
+ import java.util.ArrayList;
13
+ import java.util.HashMap;
14
+ import java.util.Iterator;
15
+ import java.util.List;
16
+ import java.util.Map;
17
+ import org.json.JSONArray;
18
+ import org.json.JSONObject;
19
+
20
+ final class RudderStackBridge {
21
+
22
+ private final Context context;
23
+ private @Nullable RudderClient client;
24
+
25
+ RudderStackBridge(final Context context) {
26
+ this.context = context.getApplicationContext();
27
+ }
28
+
29
+ void initialize(final String writeKey, @Nullable final JSObject configJson, @Nullable final JSObject optionsJson) {
30
+ final RudderConfig config = getRudderConfig(configJson);
31
+ final RudderOption options = getRudderOption(optionsJson);
32
+ client = RudderClient.getInstance(context, writeKey, config, options);
33
+
34
+ if (client == null) {
35
+ throw new IllegalStateException("Failed to initialize RudderStack SDK");
36
+ }
37
+ }
38
+
39
+ void identify(final String userId, @Nullable final JSObject traitsJson, @Nullable final JSObject optionsJson) {
40
+ requireClient().identify(userId, getRudderTraits(traitsJson), getRudderOption(optionsJson));
41
+ }
42
+
43
+ void group(final String groupId, @Nullable final JSObject traitsJson, @Nullable final JSObject optionsJson) {
44
+ requireClient().group(groupId, getRudderTraits(traitsJson), getRudderOption(optionsJson));
45
+ }
46
+
47
+ void track(final String eventName, @Nullable final JSObject propertiesJson, @Nullable final JSObject optionsJson) {
48
+ requireClient().track(eventName, getRudderProperty(propertiesJson), getRudderOption(optionsJson));
49
+ }
50
+
51
+ void screen(final String screenName, @Nullable final JSObject propertiesJson, @Nullable final JSObject optionsJson) {
52
+ requireClient().screen(screenName, getRudderProperty(propertiesJson), getRudderOption(optionsJson));
53
+ }
54
+
55
+ void alias(final String newId, @Nullable final JSObject optionsJson) {
56
+ requireClient().alias(newId, getRudderOption(optionsJson));
57
+ }
58
+
59
+ void reset() {
60
+ requireClient().reset();
61
+ }
62
+
63
+ void flush() {
64
+ requireClient().flush();
65
+ }
66
+
67
+ void putDeviceToken(final String deviceToken) {
68
+ RudderClient.putDeviceToken(deviceToken);
69
+ }
70
+
71
+ void putAdvertisingId(final String advertisingId) {
72
+ RudderClient.putAdvertisingId(advertisingId);
73
+ }
74
+
75
+ void putAnonymousId(final String anonymousId) {
76
+ RudderClient.putAnonymousId(anonymousId);
77
+ }
78
+
79
+ void optOut(final boolean optOut) {
80
+ requireClient().optOut(optOut);
81
+ }
82
+
83
+ private RudderClient requireClient() {
84
+ if (client == null) {
85
+ throw new IllegalStateException("RudderStack SDK is not initialized");
86
+ }
87
+
88
+ return client;
89
+ }
90
+
91
+ private RudderConfig getRudderConfig(@Nullable final JSONObject configJson) {
92
+ if (configJson == null) {
93
+ return new RudderConfig.Builder().build();
94
+ }
95
+
96
+ final Map<String, Object> configMap = getMapFromJSON(configJson);
97
+ final RudderConfig.Builder configBuilder = new RudderConfig.Builder();
98
+
99
+ if (configMap.containsKey("dataPlaneUrl") && getString(configMap.get("dataPlaneUrl")) != null) {
100
+ configBuilder.withDataPlaneUrl(getString(configMap.get("dataPlaneUrl")));
101
+ }
102
+ if (configMap.containsKey("flushQueueSize") && getInteger(configMap.get("flushQueueSize")) != null) {
103
+ configBuilder.withFlushQueueSize(getInteger(configMap.get("flushQueueSize")));
104
+ }
105
+ if (configMap.containsKey("dbCountThreshold") && getInteger(configMap.get("dbCountThreshold")) != null) {
106
+ configBuilder.withDbThresholdCount(getInteger(configMap.get("dbCountThreshold")));
107
+ }
108
+ if (configMap.containsKey("configRefreshInterval") && getInteger(configMap.get("configRefreshInterval")) != null) {
109
+ configBuilder.withConfigRefreshInterval(getInteger(configMap.get("configRefreshInterval")));
110
+ }
111
+ if (configMap.containsKey("logLevel") && getInteger(configMap.get("logLevel")) != null) {
112
+ configBuilder.withLogLevel(getInteger(configMap.get("logLevel")));
113
+ }
114
+ if (configMap.containsKey("sleepTimeOut") && getInteger(configMap.get("sleepTimeOut")) != null) {
115
+ configBuilder.withSleepCount(getInteger(configMap.get("sleepTimeOut")));
116
+ }
117
+ if (configMap.containsKey("autoCollectAdvertId")) {
118
+ configBuilder.withAutoCollectAdvertId(getBoolean(configMap.get("autoCollectAdvertId")));
119
+ }
120
+ if (configMap.containsKey("trackLifecycleEvents")) {
121
+ configBuilder.withTrackLifecycleEvents(getBoolean(configMap.get("trackLifecycleEvents")));
122
+ }
123
+ if (configMap.containsKey("recordScreenViews")) {
124
+ configBuilder.withRecordScreenViews(getBoolean(configMap.get("recordScreenViews")));
125
+ }
126
+ if (configMap.containsKey("controlPlaneUrl") && getString(configMap.get("controlPlaneUrl")) != null) {
127
+ configBuilder.withControlPlaneUrl(getString(configMap.get("controlPlaneUrl")));
128
+ }
129
+
130
+ return configBuilder.build();
131
+ }
132
+
133
+ private @Nullable RudderOption getRudderOption(@Nullable final JSONObject optionsJson) {
134
+ if (optionsJson == null) {
135
+ return null;
136
+ }
137
+
138
+ final Map<String, Object> optionsMap = getMapFromJSON(optionsJson);
139
+ final RudderOption option = new RudderOption();
140
+
141
+ if (optionsMap.containsKey("externalIds")) {
142
+ final Map<String, Object> externalIdsMap = castMap(optionsMap.get("externalIds"));
143
+ for (Map.Entry<String, Object> entry : externalIdsMap.entrySet()) {
144
+ final String externalId = getString(entry.getValue());
145
+ if (externalId != null) {
146
+ option.putExternalId(entry.getKey(), externalId);
147
+ }
148
+ }
149
+ }
150
+
151
+ if (optionsMap.containsKey("integrations")) {
152
+ final Map<String, Object> integrationsMap = castMap(optionsMap.get("integrations"));
153
+ for (Map.Entry<String, Object> entry : integrationsMap.entrySet()) {
154
+ option.putIntegration(entry.getKey(), getBoolean(entry.getValue()));
155
+ }
156
+ }
157
+
158
+ return option;
159
+ }
160
+
161
+ private @Nullable RudderTraits getRudderTraits(@Nullable final JSONObject traitsJson) {
162
+ if (traitsJson == null) {
163
+ return null;
164
+ }
165
+
166
+ final Map<String, Object> traitsMap = getMapFromJSON(traitsJson);
167
+ final RudderTraits traits = new RudderTraitsBuilder().build();
168
+ for (Map.Entry<String, Object> entry : traitsMap.entrySet()) {
169
+ traits.put(entry.getKey(), entry.getValue());
170
+ }
171
+ return traits;
172
+ }
173
+
174
+ private @Nullable RudderProperty getRudderProperty(@Nullable final JSONObject propertiesJson) {
175
+ if (propertiesJson == null) {
176
+ return null;
177
+ }
178
+
179
+ return new RudderProperty().putValue(getMapFromJSON(propertiesJson));
180
+ }
181
+
182
+ private Map<String, Object> getMapFromJSON(final JSONObject jsonObject) {
183
+ final Map<String, Object> map = new HashMap<>();
184
+ final Iterator<String> keysIter = jsonObject.keys();
185
+ while (keysIter.hasNext()) {
186
+ final String key = keysIter.next();
187
+ final Object value = jsonObject.isNull(key) ? null : getObject(jsonObject.opt(key));
188
+ if (value != null) {
189
+ map.put(key, value);
190
+ }
191
+ }
192
+ return map;
193
+ }
194
+
195
+ private Object getObject(final Object value) {
196
+ if (value instanceof JSONObject) {
197
+ return getMapFromJSON((JSONObject) value);
198
+ }
199
+ if (value instanceof JSONArray) {
200
+ return getListFromJSON((JSONArray) value);
201
+ }
202
+ return value;
203
+ }
204
+
205
+ private @Nullable String getString(final Object value) {
206
+ if (value instanceof String) {
207
+ return (String) value;
208
+ }
209
+ if (value instanceof Number) {
210
+ return String.valueOf(value);
211
+ }
212
+ return null;
213
+ }
214
+
215
+ private @Nullable Integer getInteger(final Object value) {
216
+ if (value instanceof Number) {
217
+ return ((Number) value).intValue();
218
+ }
219
+ if (value instanceof String) {
220
+ return Integer.valueOf((String) value);
221
+ }
222
+ return null;
223
+ }
224
+
225
+ private boolean getBoolean(final Object value) {
226
+ if (value instanceof Boolean) {
227
+ return (Boolean) value;
228
+ }
229
+ if (value instanceof Number) {
230
+ return ((Number) value).intValue() > 0;
231
+ }
232
+ if (value instanceof String) {
233
+ return Boolean.parseBoolean((String) value);
234
+ }
235
+ return false;
236
+ }
237
+
238
+ private List<Object> getListFromJSON(final JSONArray jsonArray) {
239
+ final List<Object> list = new ArrayList<>();
240
+ for (int i = 0, count = jsonArray.length(); i < count; i++) {
241
+ final Object value = getObject(jsonArray.opt(i));
242
+ if (value != null) {
243
+ list.add(value);
244
+ }
245
+ }
246
+ return list;
247
+ }
248
+
249
+ @SuppressWarnings("unchecked")
250
+ private Map<String, Object> castMap(final Object value) {
251
+ if (value instanceof Map) {
252
+ return (Map<String, Object>) value;
253
+ }
254
+ return new HashMap<>();
255
+ }
256
+ }
@@ -0,0 +1,205 @@
1
+ package app.capgo.rudderstack;
2
+
3
+ import com.getcapacitor.JSObject;
4
+ import com.getcapacitor.Plugin;
5
+ import com.getcapacitor.PluginCall;
6
+ import com.getcapacitor.PluginMethod;
7
+ import com.getcapacitor.annotation.CapacitorPlugin;
8
+
9
+ @CapacitorPlugin(name = "RudderStack")
10
+ public class RudderStackPlugin extends Plugin {
11
+
12
+ private static final String VERSION = BuildConfig.VERSION_NAME;
13
+
14
+ private RudderStackBridge implementation;
15
+
16
+ @Override
17
+ public void load() {
18
+ implementation = new RudderStackBridge(getContext());
19
+ }
20
+
21
+ @PluginMethod
22
+ public void initialize(final PluginCall call) {
23
+ final String writeKey = call.getString("writeKey");
24
+ if (writeKey == null || writeKey.trim().isEmpty()) {
25
+ call.reject("writeKey is required");
26
+ return;
27
+ }
28
+
29
+ try {
30
+ implementation.initialize(writeKey, call.getObject("config"), call.getObject("options"));
31
+ call.resolve();
32
+ } catch (Exception exception) {
33
+ call.reject(exception.getMessage(), exception);
34
+ }
35
+ }
36
+
37
+ @PluginMethod
38
+ public void identify(final PluginCall call) {
39
+ final String userId = call.getString("userId");
40
+ if (userId == null || userId.trim().isEmpty()) {
41
+ call.reject("userId is required");
42
+ return;
43
+ }
44
+
45
+ try {
46
+ implementation.identify(userId, call.getObject("traits"), call.getObject("options"));
47
+ call.resolve();
48
+ } catch (Exception exception) {
49
+ call.reject(exception.getMessage(), exception);
50
+ }
51
+ }
52
+
53
+ @PluginMethod
54
+ public void group(final PluginCall call) {
55
+ final String groupId = call.getString("groupId");
56
+ if (groupId == null || groupId.trim().isEmpty()) {
57
+ call.reject("groupId is required");
58
+ return;
59
+ }
60
+
61
+ try {
62
+ implementation.group(groupId, call.getObject("groupTraits"), call.getObject("options"));
63
+ call.resolve();
64
+ } catch (Exception exception) {
65
+ call.reject(exception.getMessage(), exception);
66
+ }
67
+ }
68
+
69
+ @PluginMethod
70
+ public void track(final PluginCall call) {
71
+ final String eventName = call.getString("eventName");
72
+ if (eventName == null || eventName.trim().isEmpty()) {
73
+ call.reject("eventName is required");
74
+ return;
75
+ }
76
+
77
+ try {
78
+ implementation.track(eventName, call.getObject("properties"), call.getObject("options"));
79
+ call.resolve();
80
+ } catch (Exception exception) {
81
+ call.reject(exception.getMessage(), exception);
82
+ }
83
+ }
84
+
85
+ @PluginMethod
86
+ public void screen(final PluginCall call) {
87
+ final String screenName = call.getString("screenName");
88
+ if (screenName == null || screenName.trim().isEmpty()) {
89
+ call.reject("screenName is required");
90
+ return;
91
+ }
92
+
93
+ try {
94
+ implementation.screen(screenName, call.getObject("properties"), call.getObject("options"));
95
+ call.resolve();
96
+ } catch (Exception exception) {
97
+ call.reject(exception.getMessage(), exception);
98
+ }
99
+ }
100
+
101
+ @PluginMethod
102
+ public void alias(final PluginCall call) {
103
+ final String newId = call.getString("newId");
104
+ if (newId == null || newId.trim().isEmpty()) {
105
+ call.reject("newId is required");
106
+ return;
107
+ }
108
+
109
+ try {
110
+ implementation.alias(newId, call.getObject("options"));
111
+ call.resolve();
112
+ } catch (Exception exception) {
113
+ call.reject(exception.getMessage(), exception);
114
+ }
115
+ }
116
+
117
+ @PluginMethod
118
+ public void reset(final PluginCall call) {
119
+ try {
120
+ implementation.reset();
121
+ call.resolve();
122
+ } catch (Exception exception) {
123
+ call.reject(exception.getMessage(), exception);
124
+ }
125
+ }
126
+
127
+ @PluginMethod
128
+ public void flush(final PluginCall call) {
129
+ try {
130
+ implementation.flush();
131
+ call.resolve();
132
+ } catch (Exception exception) {
133
+ call.reject(exception.getMessage(), exception);
134
+ }
135
+ }
136
+
137
+ @PluginMethod
138
+ public void putDeviceToken(final PluginCall call) {
139
+ final String deviceToken = call.getString("deviceToken");
140
+ if (deviceToken == null || deviceToken.trim().isEmpty()) {
141
+ call.reject("deviceToken is required");
142
+ return;
143
+ }
144
+
145
+ implementation.putDeviceToken(deviceToken);
146
+ call.resolve();
147
+ }
148
+
149
+ @PluginMethod
150
+ public void setAdvertisingId(final PluginCall call) {
151
+ putAdvertisingId(call);
152
+ }
153
+
154
+ @PluginMethod
155
+ public void putAdvertisingId(final PluginCall call) {
156
+ final String advertisingId = call.getString("advertisingId");
157
+ if (advertisingId == null || advertisingId.trim().isEmpty()) {
158
+ call.reject("advertisingId is required");
159
+ return;
160
+ }
161
+
162
+ implementation.putAdvertisingId(advertisingId);
163
+ call.resolve();
164
+ }
165
+
166
+ @PluginMethod
167
+ public void setAnonymousId(final PluginCall call) {
168
+ putAnonymousId(call);
169
+ }
170
+
171
+ @PluginMethod
172
+ public void putAnonymousId(final PluginCall call) {
173
+ final String anonymousId = call.getString("anonymousId");
174
+ if (anonymousId == null || anonymousId.trim().isEmpty()) {
175
+ call.reject("anonymousId is required");
176
+ return;
177
+ }
178
+
179
+ implementation.putAnonymousId(anonymousId);
180
+ call.resolve();
181
+ }
182
+
183
+ @PluginMethod
184
+ public void optOut(final PluginCall call) {
185
+ final Boolean optOut = call.getBoolean("optOut");
186
+ if (optOut == null) {
187
+ call.reject("optOut is required");
188
+ return;
189
+ }
190
+
191
+ try {
192
+ implementation.optOut(optOut);
193
+ call.resolve();
194
+ } catch (Exception exception) {
195
+ call.reject(exception.getMessage(), exception);
196
+ }
197
+ }
198
+
199
+ @PluginMethod
200
+ public void getPluginVersion(final PluginCall call) {
201
+ final JSObject ret = new JSObject();
202
+ ret.put("version", VERSION);
203
+ call.resolve(ret);
204
+ }
205
+ }
File without changes