react-native-mparticle 2.8.0 → 2.9.0

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.
Files changed (37) hide show
  1. package/README.md +252 -94
  2. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  3. package/android/gradle/wrapper/gradle-wrapper.properties +6 -0
  4. package/android/gradle.properties +26 -0
  5. package/android/gradlew +160 -0
  6. package/android/gradlew.bat +90 -0
  7. package/android/libs/java-json.jar +0 -0
  8. package/android/src/main/java/com/mparticle/react/MParticleModule.kt +2 -2
  9. package/android/src/test/java/com/mparticle/react/IdentityApiTest.java +230 -0
  10. package/android/src/test/java/com/mparticle/react/MParticleUserTest.java +173 -0
  11. package/android/src/test/java/com/mparticle/react/testutils/MockMParticleUser.java +109 -0
  12. package/android/src/test/java/com/mparticle/react/testutils/MockMap.java +203 -0
  13. package/android/src/test/java/com/mparticle/react/testutils/MockReadableArray.java +68 -0
  14. package/android/src/test/java/com/mparticle/react/testutils/MockWritableMap.java +4 -0
  15. package/android/src/test/java/com/mparticle/react/testutils/Mutable.java +13 -0
  16. package/app.plugin.js +1 -0
  17. package/ios/RNMParticle/RNMPRokt.mm +25 -11
  18. package/ios/RNMParticle/RNMParticle.mm +38 -28
  19. package/js/codegenSpecs/NativeMParticle.ts +3 -4
  20. package/js/rokt/rokt-layout-view.android.tsx +2 -1
  21. package/lib/codegenSpecs/NativeMParticle.d.ts +3 -4
  22. package/lib/codegenSpecs/NativeMParticle.js.map +1 -1
  23. package/lib/rokt/rokt-layout-view.android.js +1 -0
  24. package/lib/rokt/rokt-layout-view.android.js.map +1 -1
  25. package/package.json +28 -4
  26. package/plugin/build/withMParticle.d.ts +60 -0
  27. package/plugin/build/withMParticle.js +30 -0
  28. package/plugin/build/withMParticleAndroid.d.ts +6 -0
  29. package/plugin/build/withMParticleAndroid.js +266 -0
  30. package/plugin/build/withMParticleIOS.d.ts +6 -0
  31. package/plugin/build/withMParticleIOS.js +362 -0
  32. package/plugin/src/withMParticle.ts +106 -0
  33. package/plugin/src/withMParticleAndroid.ts +359 -0
  34. package/plugin/src/withMParticleIOS.ts +459 -0
  35. package/plugin/tsconfig.json +8 -0
  36. package/react-native-mparticle.podspec +11 -0
  37. package/SECURITY.md +0 -9
@@ -0,0 +1,359 @@
1
+ import {
2
+ ConfigPlugin,
3
+ withMainApplication,
4
+ withAppBuildGradle,
5
+ } from '@expo/config-plugins';
6
+ import { mergeContents } from '@expo/config-plugins/build/utils/generateCode';
7
+ import { MParticlePluginProps } from './withMParticle';
8
+
9
+ // Tag used for mergeContents to identify code blocks added by this plugin
10
+ const MPARTICLE_TAG = 'react-native-mparticle';
11
+
12
+ /**
13
+ * Get the mParticle log level string for Android
14
+ */
15
+ function getAndroidLogLevel(
16
+ logLevel?: MParticlePluginProps['logLevel']
17
+ ): string | null {
18
+ switch (logLevel) {
19
+ case 'none':
20
+ return 'MParticle.LogLevel.NONE';
21
+ case 'error':
22
+ return 'MParticle.LogLevel.ERROR';
23
+ case 'warning':
24
+ return 'MParticle.LogLevel.WARNING';
25
+ case 'debug':
26
+ return 'MParticle.LogLevel.DEBUG';
27
+ case 'verbose':
28
+ return 'MParticle.LogLevel.VERBOSE';
29
+ default:
30
+ return null;
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Get the mParticle environment string for Android
36
+ */
37
+ function getAndroidEnvironment(
38
+ environment?: MParticlePluginProps['environment']
39
+ ): string | null {
40
+ switch (environment) {
41
+ case 'development':
42
+ return 'MParticle.Environment.Development';
43
+ case 'production':
44
+ return 'MParticle.Environment.Production';
45
+ case 'autoDetect':
46
+ return 'MParticle.Environment.AutoDetect';
47
+ default:
48
+ return null;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Generate mParticle initialization code for Kotlin MainApplication
54
+ */
55
+ function generateKotlinInitCode(props: MParticlePluginProps): string {
56
+ const {
57
+ androidApiKey,
58
+ androidApiSecret,
59
+ logLevel,
60
+ environment,
61
+ useEmptyIdentifyRequest = true,
62
+ dataPlanId,
63
+ dataPlanVersion,
64
+ } = props;
65
+
66
+ const lines: string[] = [
67
+ '// mParticle SDK initialization',
68
+ 'val mParticleOptions = MParticleOptions.builder(this)',
69
+ ` .credentials("${androidApiKey}", "${androidApiSecret}")`,
70
+ ];
71
+
72
+ const androidLogLevel = getAndroidLogLevel(logLevel);
73
+ if (androidLogLevel) {
74
+ lines.push(` .logLevel(${androidLogLevel})`);
75
+ }
76
+
77
+ const androidEnvironment = getAndroidEnvironment(environment);
78
+ if (androidEnvironment) {
79
+ lines.push(` .environment(${androidEnvironment})`);
80
+ }
81
+
82
+ if (dataPlanId) {
83
+ const versionParam = dataPlanVersion ? `, ${dataPlanVersion}` : '';
84
+ lines.push(` .dataplan("${dataPlanId}"${versionParam})`);
85
+ }
86
+
87
+ if (useEmptyIdentifyRequest) {
88
+ lines.push(' .identify(IdentityApiRequest.withEmptyUser().build())');
89
+ }
90
+
91
+ lines.push(' .build()');
92
+ lines.push('MParticle.start(mParticleOptions)');
93
+
94
+ return lines.join('\n ');
95
+ }
96
+
97
+ /**
98
+ * Generate mParticle initialization code for Java MainApplication
99
+ */
100
+ function generateJavaInitCode(props: MParticlePluginProps): string {
101
+ const {
102
+ androidApiKey,
103
+ androidApiSecret,
104
+ logLevel,
105
+ environment,
106
+ useEmptyIdentifyRequest = true,
107
+ dataPlanId,
108
+ dataPlanVersion,
109
+ } = props;
110
+
111
+ const lines: string[] = [
112
+ '// mParticle SDK initialization',
113
+ 'MParticleOptions.Builder optionsBuilder = MParticleOptions.builder(this)',
114
+ ` .credentials("${androidApiKey}", "${androidApiSecret}")`,
115
+ ];
116
+
117
+ const androidLogLevel = getAndroidLogLevel(logLevel);
118
+ if (androidLogLevel) {
119
+ lines.push(` .logLevel(${androidLogLevel})`);
120
+ }
121
+
122
+ const androidEnvironment = getAndroidEnvironment(environment);
123
+ if (androidEnvironment) {
124
+ lines.push(` .environment(${androidEnvironment})`);
125
+ }
126
+
127
+ if (dataPlanId) {
128
+ const versionParam = dataPlanVersion ? `, ${dataPlanVersion}` : '';
129
+ lines.push(` .dataplan("${dataPlanId}"${versionParam})`);
130
+ }
131
+
132
+ if (useEmptyIdentifyRequest) {
133
+ lines.push(' .identify(IdentityApiRequest.withEmptyUser().build())');
134
+ }
135
+
136
+ // Java needs semicolons
137
+ lines.push(';');
138
+ lines.push('MParticle.start(optionsBuilder.build());');
139
+
140
+ return lines.join('\n ');
141
+ }
142
+
143
+ /**
144
+ * Generate mParticle import statements for Kotlin
145
+ */
146
+ function getKotlinImports(): string {
147
+ return `import com.mparticle.MParticle
148
+ import com.mparticle.MParticleOptions
149
+ import com.mparticle.identity.IdentityApiRequest`;
150
+ }
151
+
152
+ /**
153
+ * Generate mParticle import statements for Java
154
+ */
155
+ function getJavaImports(): string {
156
+ return `import com.mparticle.MParticle;
157
+ import com.mparticle.MParticleOptions;
158
+ import com.mparticle.identity.IdentityApiRequest;`;
159
+ }
160
+
161
+ /**
162
+ * Add mParticle configuration to MainApplication
163
+ * Handles both Kotlin and Java
164
+ */
165
+ const withMParticleMainApplication: ConfigPlugin<MParticlePluginProps> = (
166
+ config,
167
+ props
168
+ ) => {
169
+ return withMainApplication(config, config => {
170
+ const { contents, language } = config.modResults;
171
+
172
+ // Check if mParticle is already initialized
173
+ if (
174
+ contents.includes('MParticleOptions') ||
175
+ contents.includes('mParticleOptions')
176
+ ) {
177
+ return config;
178
+ }
179
+
180
+ const isKotlin = language === 'kt';
181
+
182
+ if (isKotlin) {
183
+ config.modResults.contents = addMParticleToKotlinMainApplication(
184
+ contents,
185
+ props
186
+ );
187
+ } else if (language === 'java') {
188
+ config.modResults.contents = addMParticleToJavaMainApplication(
189
+ contents,
190
+ props
191
+ );
192
+ } else {
193
+ console.warn(
194
+ `[react-native-mparticle] Unsupported MainApplication language: ${language}. ` +
195
+ 'mParticle initialization must be added manually.'
196
+ );
197
+ }
198
+
199
+ return config;
200
+ });
201
+ };
202
+
203
+ /**
204
+ * Add mParticle import and initialization to Kotlin MainApplication
205
+ */
206
+ function addMParticleToKotlinMainApplication(
207
+ contents: string,
208
+ props: MParticlePluginProps
209
+ ): string {
210
+ // Add import statements using mergeContents
211
+ const withImports = mergeContents({
212
+ src: contents,
213
+ newSrc: getKotlinImports(),
214
+ anchor: /^package .+$/m,
215
+ offset: 1, // Add after package declaration
216
+ tag: `${MPARTICLE_TAG}-import`,
217
+ comment: '//',
218
+ });
219
+
220
+ // Generate initialization code
221
+ const initCode = generateKotlinInitCode(props);
222
+
223
+ // Find the right place to add initialization code
224
+ // Try ApplicationLifecycleDispatcher first (Expo pattern), then super.onCreate()
225
+ let result = withImports.contents;
226
+
227
+ if (
228
+ result.includes('ApplicationLifecycleDispatcher.onApplicationCreate(this)')
229
+ ) {
230
+ const withInit = mergeContents({
231
+ src: result,
232
+ newSrc: `\n ${initCode}\n`,
233
+ anchor: /ApplicationLifecycleDispatcher\.onApplicationCreate\(this\)/,
234
+ offset: 1, // Add after the anchor
235
+ tag: `${MPARTICLE_TAG}-init`,
236
+ comment: '//',
237
+ });
238
+ result = withInit.contents;
239
+ } else if (result.includes('super.onCreate()')) {
240
+ const withInit = mergeContents({
241
+ src: result,
242
+ newSrc: `\n ${initCode}\n`,
243
+ anchor: /super\.onCreate\(\)/,
244
+ offset: 1, // Add after the anchor
245
+ tag: `${MPARTICLE_TAG}-init`,
246
+ comment: '//',
247
+ });
248
+ result = withInit.contents;
249
+ }
250
+
251
+ return result;
252
+ }
253
+
254
+ /**
255
+ * Add mParticle import and initialization to Java MainApplication
256
+ */
257
+ function addMParticleToJavaMainApplication(
258
+ contents: string,
259
+ props: MParticlePluginProps
260
+ ): string {
261
+ // Add import statements using mergeContents
262
+ const withImports = mergeContents({
263
+ src: contents,
264
+ newSrc: getJavaImports(),
265
+ anchor: /^package .+;$/m,
266
+ offset: 1, // Add after package declaration
267
+ tag: `${MPARTICLE_TAG}-import`,
268
+ comment: '//',
269
+ });
270
+
271
+ // Generate initialization code
272
+ const initCode = generateJavaInitCode(props);
273
+
274
+ // Find the right place to add initialization code
275
+ let result = withImports.contents;
276
+
277
+ if (
278
+ result.includes('ApplicationLifecycleDispatcher.onApplicationCreate(this);')
279
+ ) {
280
+ const withInit = mergeContents({
281
+ src: result,
282
+ newSrc: `\n ${initCode}\n`,
283
+ anchor: /ApplicationLifecycleDispatcher\.onApplicationCreate\(this\);/,
284
+ offset: 1, // Add after the anchor
285
+ tag: `${MPARTICLE_TAG}-init`,
286
+ comment: '//',
287
+ });
288
+ result = withInit.contents;
289
+ } else if (result.includes('super.onCreate();')) {
290
+ const withInit = mergeContents({
291
+ src: result,
292
+ newSrc: `\n ${initCode}\n`,
293
+ anchor: /super\.onCreate\(\);/,
294
+ offset: 1, // Add after the anchor
295
+ tag: `${MPARTICLE_TAG}-init`,
296
+ comment: '//',
297
+ });
298
+ result = withInit.contents;
299
+ }
300
+
301
+ return result;
302
+ }
303
+
304
+ /**
305
+ * Add kit dependencies to app/build.gradle
306
+ */
307
+ const withMParticleAppBuildGradle: ConfigPlugin<MParticlePluginProps> = (
308
+ config,
309
+ props
310
+ ) => {
311
+ return withAppBuildGradle(config, config => {
312
+ const { contents } = config.modResults;
313
+
314
+ if (!props.androidKits || props.androidKits.length === 0) {
315
+ return config;
316
+ }
317
+
318
+ // Check if kits are already added
319
+ const kitsAlreadyAdded = props.androidKits.every(kit =>
320
+ contents.includes(`com.mparticle:${kit}`)
321
+ );
322
+
323
+ if (kitsAlreadyAdded) {
324
+ return config;
325
+ }
326
+
327
+ // Generate kit dependency lines
328
+ // Use + for version to auto-match core SDK version
329
+ const kitDependencies = props.androidKits
330
+ .map(kit => ` implementation "com.mparticle:${kit}:+"`)
331
+ .join('\n');
332
+
333
+ // Use mergeContents for idempotent injection
334
+ const withKits = mergeContents({
335
+ src: contents,
336
+ newSrc: `\n // mParticle kits\n${kitDependencies}`,
337
+ anchor: /dependencies\s*\{/,
338
+ offset: 1, // Add after the opening brace
339
+ tag: `${MPARTICLE_TAG}-kits`,
340
+ comment: '//',
341
+ });
342
+
343
+ config.modResults.contents = withKits.contents;
344
+ return config;
345
+ });
346
+ };
347
+
348
+ /**
349
+ * Apply all Android-specific mParticle configurations
350
+ */
351
+ export const withMParticleAndroid: ConfigPlugin<MParticlePluginProps> = (
352
+ config,
353
+ props
354
+ ) => {
355
+ config = withMParticleMainApplication(config, props);
356
+ config = withMParticleAppBuildGradle(config, props);
357
+
358
+ return config;
359
+ };