kewa-react-native 1.0.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 (42) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +592 -0
  3. package/lib/KewaProvider.d.ts +26 -0
  4. package/lib/KewaProvider.js +91 -0
  5. package/lib/core/EventManager.d.ts +13 -0
  6. package/lib/core/EventManager.js +129 -0
  7. package/lib/core/KewaAnalytics.d.ts +41 -0
  8. package/lib/core/KewaAnalytics.js +317 -0
  9. package/lib/index.d.ts +23 -0
  10. package/lib/index.js +94 -0
  11. package/lib/types/config.d.ts +32 -0
  12. package/lib/types/config.js +2 -0
  13. package/lib/types/events.d.ts +104 -0
  14. package/lib/types/events.js +2 -0
  15. package/lib/types/index.d.ts +3 -0
  16. package/lib/types/index.js +19 -0
  17. package/lib/types/response.d.ts +15 -0
  18. package/lib/types/response.js +2 -0
  19. package/lib/utils/DeviceInfo.d.ts +4 -0
  20. package/lib/utils/DeviceInfo.js +32 -0
  21. package/lib/utils/NetworkManager.d.ts +17 -0
  22. package/lib/utils/NetworkManager.js +77 -0
  23. package/lib/utils/StorageManager.d.ts +16 -0
  24. package/lib/utils/StorageManager.js +120 -0
  25. package/lib/utils/constants.d.ts +26 -0
  26. package/lib/utils/constants.js +29 -0
  27. package/lib/utils/debug.d.ts +3 -0
  28. package/lib/utils/debug.js +17 -0
  29. package/package.json +59 -0
  30. package/src/KewaProvider.tsx +87 -0
  31. package/src/core/EventManager.ts +160 -0
  32. package/src/core/KewaAnalytics.ts +419 -0
  33. package/src/index.tsx +89 -0
  34. package/src/types/config.ts +33 -0
  35. package/src/types/events.ts +123 -0
  36. package/src/types/index.ts +3 -0
  37. package/src/types/response.ts +16 -0
  38. package/src/utils/DeviceInfo.ts +29 -0
  39. package/src/utils/NetworkManager.ts +85 -0
  40. package/src/utils/StorageManager.ts +121 -0
  41. package/src/utils/constants.ts +26 -0
  42. package/src/utils/debug.ts +15 -0
@@ -0,0 +1,85 @@
1
+ import NetInfo from '@react-native-community/netinfo';
2
+ import { ContactData, KewaEvent, KewaResponse } from '../types';
3
+ import { StorageManager } from './StorageManager';
4
+
5
+ export class NetworkManager {
6
+ private appUrl: string = '';
7
+ private apiKey: string = '';
8
+ private projectId: string = '';
9
+ private isOnline: boolean = true;
10
+ private unsubscribe: (() => void) | null = null;
11
+
12
+ constructor(appUrl: string, apiKey: string, projectId: string) {
13
+ this.appUrl = appUrl.replace(/\/$/, '');
14
+ this.apiKey = apiKey;
15
+ this.projectId = projectId;
16
+ this.setupNetworkMonitoring();
17
+ }
18
+
19
+ private getTrackUrl(): string {
20
+ return `${this.appUrl}/t/${this.projectId}/mtc/event/track`;
21
+ }
22
+
23
+ private getContactUrl(): string {
24
+ return `${this.appUrl}/t/${this.projectId}/mtc`;
25
+ }
26
+
27
+ private setupNetworkMonitoring(): void {
28
+ this.unsubscribe = NetInfo.addEventListener(state => {
29
+ this.isOnline = state.isConnected ?? true;
30
+ });
31
+ }
32
+
33
+ async sendEvent(event: KewaEvent): Promise<KewaResponse> {
34
+ const eventPayload = {
35
+ event: event.eventName,
36
+ timestamp: event.timestamp,
37
+ data: event.eventData,
38
+ contact: event.contactData,
39
+ kewa_device_id: event.deviceId,
40
+ device: event.deviceInfo,
41
+ };
42
+
43
+ return this.post(this.getTrackUrl(), eventPayload);
44
+ }
45
+
46
+ async updateContact(contact: ContactData): Promise<KewaResponse> {
47
+ const ktcId = await StorageManager.getKtcId();
48
+ const deviceId = await StorageManager.getDeviceId();
49
+ return this.post(this.getContactUrl(), { contact, mtc_id: ktcId, kewa_device_id: deviceId });
50
+ }
51
+
52
+ private async post(url: string, body: Record<string, unknown>): Promise<KewaResponse> {
53
+ const response = await fetch(url, {
54
+ method: 'POST',
55
+ headers: {
56
+ 'Content-Type': 'application/json',
57
+ 'secret': `${this.apiKey}`,
58
+ 'User-Agent': 'Kewa-SDK/1.0.0',
59
+ },
60
+ body: JSON.stringify(body),
61
+ });
62
+
63
+ if (!response.ok) {
64
+ throw new Error(`Kewa Analytics SDK: HTTP error! status: ${response.status}`);
65
+ }
66
+
67
+ const responseData: KewaResponse = await response.json();
68
+
69
+ if (!responseData.success) {
70
+ throw new Error('Kewa Analytics SDK: Responded with an error ' + responseData.message);
71
+ }
72
+
73
+ return responseData;
74
+ }
75
+
76
+ getNetworkStatus(): boolean {
77
+ return this.isOnline;
78
+ }
79
+
80
+ cleanup(): void {
81
+ if (this.unsubscribe) {
82
+ this.unsubscribe();
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,121 @@
1
+ import AsyncStorage from '@react-native-async-storage/async-storage';
2
+ import { ContactData, KewaEvent } from '../types';
3
+ import { KEWA_CONSTANTS } from '../utils/constants';
4
+
5
+ export class StorageManager {
6
+ static async getKtcId(): Promise<string | null> {
7
+ try {
8
+ return await AsyncStorage.getItem(KEWA_CONSTANTS.STORAGE_KEYS.KTC_ID);
9
+ } catch (error) {
10
+ console.error('Failed to get ktc_id:', error);
11
+ return null;
12
+ }
13
+ }
14
+
15
+ static async setKtcId(ktcId: string): Promise<void> {
16
+ try {
17
+ await AsyncStorage.setItem(KEWA_CONSTANTS.STORAGE_KEYS.KTC_ID, ktcId);
18
+ } catch (error) {
19
+ console.error('Failed to set ktc_id:', error);
20
+ }
21
+ }
22
+
23
+ static async removeKtcId(): Promise<void> {
24
+ try {
25
+ await AsyncStorage.removeItem(KEWA_CONSTANTS.STORAGE_KEYS.KTC_ID);
26
+ } catch (error) {
27
+ console.error('Failed to remove ktc_id:', error);
28
+ }
29
+ }
30
+
31
+ static async getDeviceId(): Promise<string | null> {
32
+ try {
33
+ return await AsyncStorage.getItem(KEWA_CONSTANTS.STORAGE_KEYS.DEVICE_ID);
34
+ } catch (error) {
35
+ console.error('Failed to get device_id:', error);
36
+ return null;
37
+ }
38
+ }
39
+
40
+ static async setDeviceId(deviceId: string): Promise<void> {
41
+ try {
42
+ await AsyncStorage.setItem(KEWA_CONSTANTS.STORAGE_KEYS.DEVICE_ID, deviceId);
43
+ } catch (error) {
44
+ console.error('Failed to set device_id:', error);
45
+ }
46
+ }
47
+
48
+ static async removeDeviceId(): Promise<void> {
49
+ try {
50
+ await AsyncStorage.removeItem(KEWA_CONSTANTS.STORAGE_KEYS.DEVICE_ID);
51
+ } catch (error) {
52
+ console.error('Failed to remove device_id:', error);
53
+ }
54
+ }
55
+
56
+ static async getUserProperties(): Promise<ContactData> {
57
+ try {
58
+ const props = await AsyncStorage.getItem(KEWA_CONSTANTS.STORAGE_KEYS.USER_PROPERTIES);
59
+ return props ? JSON.parse(props) : {};
60
+ } catch (error) {
61
+ console.error('Kewa Analytics SDK: Failed to get user properties:', error);
62
+ return {};
63
+ }
64
+ }
65
+
66
+ static async setUserProperties(properties: ContactData): Promise<void> {
67
+ try {
68
+ await AsyncStorage.setItem(
69
+ KEWA_CONSTANTS.STORAGE_KEYS.USER_PROPERTIES,
70
+ JSON.stringify(properties)
71
+ );
72
+ } catch (error) {
73
+ console.error('Kewa Analytics SDK: Failed to set user properties:', error);
74
+ }
75
+ }
76
+
77
+ static async removeUserProperties(): Promise<void> {
78
+ try {
79
+ await AsyncStorage.removeItem(KEWA_CONSTANTS.STORAGE_KEYS.USER_PROPERTIES);
80
+ } catch (error) {
81
+ console.error('Kewa Analytics SDK: Failed to remove user properties:', error);
82
+ }
83
+ }
84
+
85
+ static async getQueuedEvents(): Promise<KewaEvent[]> {
86
+ try {
87
+ const events = await AsyncStorage.getItem(KEWA_CONSTANTS.STORAGE_KEYS.QUEUED_EVENTS);
88
+ return events ? JSON.parse(events) : [];
89
+ } catch (error) {
90
+ console.error('Kewa Analytics SDK: Failed to get queued events:', error);
91
+ return [];
92
+ }
93
+ }
94
+
95
+ static async setQueuedEvents(events: KewaEvent[]): Promise<void> {
96
+ try {
97
+ // Limit queue size
98
+ const limitedEvents = events.slice(-KEWA_CONSTANTS.DEFAULTS.MAX_QUEUE_SIZE);
99
+ await AsyncStorage.setItem(
100
+ KEWA_CONSTANTS.STORAGE_KEYS.QUEUED_EVENTS,
101
+ JSON.stringify(limitedEvents)
102
+ );
103
+ } catch (error) {
104
+ console.error('Kewa Analytics SDK: Failed to set queued events:', error);
105
+ }
106
+ }
107
+
108
+ static async addQueuedEvent(event: KewaEvent): Promise<void> {
109
+ const events = await this.getQueuedEvents();
110
+ events.push(event);
111
+ await this.setQueuedEvents(events);
112
+ }
113
+
114
+ static async clearQueuedEvents(): Promise<void> {
115
+ try {
116
+ await AsyncStorage.removeItem(KEWA_CONSTANTS.STORAGE_KEYS.QUEUED_EVENTS);
117
+ } catch (error) {
118
+ console.error('Kewa Analytics SDK: Failed to clear queued events:', error);
119
+ }
120
+ }
121
+ }
@@ -0,0 +1,26 @@
1
+ export const KEWA_CONSTANTS = {
2
+ STORAGE_KEYS: {
3
+ KTC_ID: 'kewa_ktc_id',
4
+ DEVICE_ID: 'kewa_device_id',
5
+ QUEUED_EVENTS: 'kewa_queued_events',
6
+ USER_PROPERTIES: 'kewa_user_properties',
7
+ },
8
+ EVENTS: {
9
+ APP_LAUNCH: 'app_launch',
10
+ APP_FOREGROUND: 'app_foreground',
11
+ APP_BACKGROUND: 'app_background',
12
+ USER_LOGIN: 'user_login',
13
+ USER_LOGOUT: 'user_logout',
14
+ USER_REGISTRATION: 'user_registration',
15
+ SCREEN_VIEW: 'screen_view',
16
+ ERROR: 'error',
17
+ BUTTON_PRESS: 'button_press',
18
+ FORM_SUBMIT: 'form_submit',
19
+ },
20
+ DEFAULTS: {
21
+ BATCH_SIZE: 10,
22
+ MAX_QUEUE_SIZE: 100,
23
+ RETRY_ATTEMPTS: 3,
24
+ RETRY_DELAY_MS: 1000,
25
+ },
26
+ } as const;
@@ -0,0 +1,15 @@
1
+ let debugEnabled = false;
2
+
3
+ export function setDebugLogging(enabled: boolean): void {
4
+ debugEnabled = enabled;
5
+ }
6
+
7
+ export function isDebugLoggingEnabled(): boolean {
8
+ return debugEnabled;
9
+ }
10
+
11
+ export function kewaDebug(...args: unknown[]): void {
12
+ if (debugEnabled) {
13
+ console.log('[Kewa]', ...args);
14
+ }
15
+ }