@amplitude/plugin-engagement-react-native 0.1.1-alpha.10

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 (44) hide show
  1. package/LICENSE +20 -0
  2. package/PluginEngagementReactNative.podspec +22 -0
  3. package/README.md +95 -0
  4. package/android/build.gradle +89 -0
  5. package/android/gradle.properties +5 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/com/amplitude/pluginengagementreactnative/PluginEngagementReactNativeModule.kt +183 -0
  8. package/android/src/main/java/com/amplitude/pluginengagementreactnative/PluginEngagementReactNativePackage.kt +33 -0
  9. package/ios/AmplitudeEngagementAdapter.swift +121 -0
  10. package/ios/PluginEngagementReactNative.h +5 -0
  11. package/ios/PluginEngagementReactNative.mm +87 -0
  12. package/lib/module/AmplitudeEngagement.js +42 -0
  13. package/lib/module/AmplitudeEngagement.js.map +1 -0
  14. package/lib/module/AmplitudeEngagementPlugin.js +29 -0
  15. package/lib/module/AmplitudeEngagementPlugin.js.map +1 -0
  16. package/lib/module/NativePluginEngagementReactNative.js +5 -0
  17. package/lib/module/NativePluginEngagementReactNative.js.map +1 -0
  18. package/lib/module/index.js +64 -0
  19. package/lib/module/index.js.map +1 -0
  20. package/lib/module/logger.js +53 -0
  21. package/lib/module/logger.js.map +1 -0
  22. package/lib/module/package.json +1 -0
  23. package/lib/module/types.js +2 -0
  24. package/lib/module/types.js.map +1 -0
  25. package/lib/typescript/package.json +1 -0
  26. package/lib/typescript/src/AmplitudeEngagement.d.ts +17 -0
  27. package/lib/typescript/src/AmplitudeEngagement.d.ts.map +1 -0
  28. package/lib/typescript/src/AmplitudeEngagementPlugin.d.ts +10 -0
  29. package/lib/typescript/src/AmplitudeEngagementPlugin.d.ts.map +1 -0
  30. package/lib/typescript/src/NativePluginEngagementReactNative.d.ts +18 -0
  31. package/lib/typescript/src/NativePluginEngagementReactNative.d.ts.map +1 -0
  32. package/lib/typescript/src/index.d.ts +13 -0
  33. package/lib/typescript/src/index.d.ts.map +1 -0
  34. package/lib/typescript/src/logger.d.ts +28 -0
  35. package/lib/typescript/src/logger.d.ts.map +1 -0
  36. package/lib/typescript/src/types.d.ts +7 -0
  37. package/lib/typescript/src/types.d.ts.map +1 -0
  38. package/package.json +168 -0
  39. package/src/AmplitudeEngagement.ts +62 -0
  40. package/src/AmplitudeEngagementPlugin.ts +47 -0
  41. package/src/NativePluginEngagementReactNative.ts +28 -0
  42. package/src/index.tsx +78 -0
  43. package/src/logger.ts +72 -0
  44. package/src/types.ts +7 -0
@@ -0,0 +1,28 @@
1
+ import type { TurboModule } from 'react-native';
2
+ import { TurboModuleRegistry } from 'react-native';
3
+ import type { GuideOrSurvey } from './types';
4
+
5
+ export interface Spec extends TurboModule {
6
+ newInstance(apiKey: string): number;
7
+
8
+ boot(id: number, user_id: string, device_id: string): void;
9
+ setThemeMode(
10
+ id: number,
11
+ themeMode: 'auto' | 'dark_mode' | 'light_mode' /* ThemeMode */
12
+ ): void;
13
+ reset(id: number, key: string, stepIndex: number): void;
14
+
15
+ list(id: number): GuideOrSurvey[];
16
+
17
+ show(id: number, key: string, stepIndex: number): void;
18
+ screen(id: number, screenName: string): void;
19
+ closeAll(id: number): void;
20
+ forwardEvent(id: number, event: Object /* BaseEvent */): void;
21
+ addCallback(id: number, key: string, func: () => void): void;
22
+
23
+ handleURL(id: number, url: string): boolean;
24
+ }
25
+
26
+ export default TurboModuleRegistry.getEnforcing<Spec>(
27
+ 'PluginEngagementReactNative'
28
+ );
package/src/index.tsx ADDED
@@ -0,0 +1,78 @@
1
+ import { AmplitudeEngagement } from './AmplitudeEngagement';
2
+ import { AmplitudeEngagementPlugin } from './AmplitudeEngagementPlugin';
3
+ import type { GuideOrSurvey } from './types';
4
+ import type { BaseEvent, Plugin } from '@amplitude/analytics-types';
5
+
6
+ let globalEngagement: AmplitudeEngagement = null as any;
7
+ let toNotify: ((engagement: AmplitudeEngagement) => void)[] = [];
8
+
9
+ async function getGlobalEngagement(): Promise<AmplitudeEngagement> {
10
+ if (globalEngagement) {
11
+ return globalEngagement;
12
+ } else {
13
+ return await new Promise((resolve, _reject) => {
14
+ toNotify.push((engagement) => {
15
+ resolve(engagement);
16
+ });
17
+ });
18
+ }
19
+ }
20
+
21
+ export function init(apiKey: string): void {
22
+ // Initialize the plugin with the API key
23
+ if (globalEngagement) {
24
+ throw new Error('AmplitudeEngagement already initialized');
25
+ }
26
+ globalEngagement = new AmplitudeEngagement(apiKey);
27
+ toNotify.forEach((callback) => {
28
+ callback(globalEngagement);
29
+ });
30
+ toNotify = [];
31
+ }
32
+
33
+ export async function boot(user_id: string, device_id: string): Promise<void> {
34
+ const e = await getGlobalEngagement();
35
+ return e.boot(user_id, device_id);
36
+ }
37
+
38
+ export async function list(): Promise<GuideOrSurvey[]> {
39
+ const e = await getGlobalEngagement();
40
+ return e.list();
41
+ }
42
+
43
+ export async function show(key: string, stepIndex: number): Promise<void> {
44
+ const e = await getGlobalEngagement();
45
+ return e.show(key, stepIndex);
46
+ }
47
+
48
+ export async function screen(screenName: string): Promise<void> {
49
+ const e = await getGlobalEngagement();
50
+ return e.screen(screenName);
51
+ }
52
+
53
+ export async function closeAll(): Promise<void> {
54
+ const e = await getGlobalEngagement();
55
+ return e.closeAll();
56
+ }
57
+
58
+ export async function forwardEvent(event: BaseEvent): Promise<void> {
59
+ const e = await getGlobalEngagement();
60
+ return e.forwardEvent(event);
61
+ }
62
+
63
+ export async function addCallback(
64
+ key: string,
65
+ func: () => void
66
+ ): Promise<void> {
67
+ const e = await getGlobalEngagement();
68
+ return e.addCallback(key, func);
69
+ }
70
+
71
+ export async function handleURL(url: string): Promise<boolean> {
72
+ const e = await getGlobalEngagement();
73
+ return e.handleURL(url);
74
+ }
75
+
76
+ export function getPlugin(): Plugin {
77
+ return new AmplitudeEngagementPlugin();
78
+ }
package/src/logger.ts ADDED
@@ -0,0 +1,72 @@
1
+ export enum AmplitudeLogLevel {
2
+ verbose = 0,
3
+ debug = 1,
4
+ warn = 2,
5
+ error = 3,
6
+ none = 4,
7
+ }
8
+
9
+ export interface AmplitudeLogger {
10
+ disable(): void;
11
+ enable(logLevel: AmplitudeLogLevel): void;
12
+ log(...args: any[]): void;
13
+ warn(...args: any[]): void;
14
+ error(...args: any[]): void;
15
+ debug(...args: any[]): void;
16
+ }
17
+
18
+ export class Logger implements AmplitudeLogger {
19
+ private prefix: string;
20
+ private currentLogLevel: AmplitudeLogLevel = AmplitudeLogLevel.debug;
21
+
22
+ constructor(name: string = 'AmplitudeEngagementSwift') {
23
+ this.prefix = `[${name}]`;
24
+ }
25
+
26
+ disable(): void {
27
+ this.currentLogLevel = AmplitudeLogLevel.none;
28
+ }
29
+
30
+ enable(logLevel: AmplitudeLogLevel): void {
31
+ this.currentLogLevel = logLevel;
32
+ }
33
+
34
+ log(...args: any[]): void {
35
+ if (this.currentLogLevel <= AmplitudeLogLevel.verbose) {
36
+ console.info(this.format(args, 'INFO'));
37
+ }
38
+ }
39
+
40
+ warn(...args: any[]): void {
41
+ if (this.currentLogLevel <= AmplitudeLogLevel.warn) {
42
+ console.warn(this.format(args, 'WARNING'));
43
+ }
44
+ }
45
+
46
+ error(...args: any[]): void {
47
+ if (this.currentLogLevel <= AmplitudeLogLevel.error) {
48
+ console.error(this.format(args, 'ERROR'));
49
+ }
50
+ }
51
+
52
+ debug(...args: any[]): void {
53
+ if (this.currentLogLevel <= AmplitudeLogLevel.debug) {
54
+ console.debug(this.format(args, 'DEBUG'));
55
+ }
56
+ }
57
+
58
+ private format(
59
+ args: any[],
60
+ level: 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR'
61
+ ): string {
62
+ const message = args.map((a) => String(a)).join(' ');
63
+
64
+ const emoji = {
65
+ DEBUG: '🔍',
66
+ INFO: 'ℹ️',
67
+ WARNING: '⚠️',
68
+ ERROR: '🔴',
69
+ }[level];
70
+ return `${this.prefix} ${emoji} ${level}: ${message}`;
71
+ }
72
+ }
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export type GuideOrSurvey = {
2
+ id: number;
3
+ step: number;
4
+ title: string;
5
+ };
6
+
7
+ export type ThemeMode = 'auto' | 'dark_mode' | 'light_mode';