@metarouter/react-native-sdk 0.1.17 → 0.1.19

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.
package/README.md CHANGED
@@ -8,7 +8,7 @@ A lightweight React Native analytics SDK that transmits events to your MetaRoute
8
8
  ## Installation
9
9
 
10
10
  ```sh
11
- npm install @metarouter/react-native-sdk
11
+ npm install @metarouter/react-native-sdk @react-native-async-storage/async-storage react-native-device-info
12
12
  ```
13
13
 
14
14
  ## Usage
@@ -176,6 +176,47 @@ The analytics client provides the following methods:
176
176
  - 🔧 **TypeScript Support**: Full TypeScript support included
177
177
  - 🚀 **Lightweight**: Minimal overhead and dependencies
178
178
  - 🔄 **Reset Capability**: Easily reset analytics state for testing or logout scenarios
179
+ - 🐛 **Debug Support**: Built-in debugging tools for troubleshooting
180
+
181
+ ## Debugging
182
+
183
+ If you're not seeing API calls being made, here are some steps to troubleshoot:
184
+
185
+ ### 1. Enable Debug Logging
186
+
187
+ ```js
188
+ // Initialize with debug enabled
189
+ const analytics = await MetaRouter.analytics.init({
190
+ writeKey: "your-write-key",
191
+ ingestionEndpoint: "https://your-ingestion-endpoint.com",
192
+ debug: true, // This enables detailed logging
193
+ });
194
+
195
+ // Or enable debug logging after initialization
196
+ analytics.enableDebugLogging?.();
197
+ ```
198
+
199
+ ### 2. Check Debug Information
200
+
201
+ ```js
202
+ // Get current state information
203
+ const debugInfo = analytics.getDebugInfo?.();
204
+ console.log("Analytics debug info:", debugInfo);
205
+ ```
206
+
207
+ ### 3. Force Flush Events
208
+
209
+ ```js
210
+ // Manually flush events to see if they're being sent
211
+ await analytics.flush();
212
+ ```
213
+
214
+ ### 4. Common Issues
215
+
216
+ - **Network Permissions**: Ensure your app has network permissions
217
+ - **AsyncStorage**: The SDK uses AsyncStorage for anonymous ID persistence
218
+ - **Endpoint URL**: Verify your ingestion endpoint is correct and accessible
219
+ - **Write Key**: Ensure your write key is valid
179
220
 
180
221
  ## License
181
222
 
@@ -1,3 +1,4 @@
1
1
  import type { MetaRouterInterface } from './types';
2
+ import 'react-native-get-random-values';
2
3
  export declare const MetaRouter: MetaRouterInterface;
3
4
  //# sourceMappingURL=MetaRouter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"MetaRouter.d.ts","sourceRoot":"","sources":["../src/MetaRouter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,eAAO,MAAM,UAAU,EAAE,mBAMxB,CAAC"}
1
+ {"version":3,"file":"MetaRouter.d.ts","sourceRoot":"","sources":["../src/MetaRouter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,gCAAgC,CAAC;AAExC,eAAO,MAAM,UAAU,EAAE,mBAMxB,CAAC"}
package/lib/MetaRouter.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { initAnalytics, getAnalyticsClient, resetAnalytics } from './analytics/init';
2
+ import 'react-native-get-random-values';
2
3
  export const MetaRouter = {
3
4
  analytics: {
4
5
  init: (opts) => initAnalytics(opts),
@@ -1 +1 @@
1
- {"version":3,"file":"IdentityManager.d.ts","sourceRoot":"","sources":["../../src/analytics/IdentityManager.ts"],"names":[],"mappings":"AAEA,qBAAa,eAAe;IACxB,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,OAAO,CAAC,CAAS;IAGnB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,cAAc,IAAI,MAAM,GAAG,IAAI;IAI/B,QAAQ,CAAC,MAAM,EAAE,MAAM;IAKvB,KAAK,CAAC,OAAO,EAAE,MAAM;IAIrB,SAAS,IAAI,MAAM,GAAG,SAAS;IAI/B,UAAU,IAAI,MAAM,GAAG,SAAS;IAIhC,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC;IAS3D,KAAK;CAKN"}
1
+ {"version":3,"file":"IdentityManager.d.ts","sourceRoot":"","sources":["../../src/analytics/IdentityManager.ts"],"names":[],"mappings":"AAGA,qBAAa,eAAe;IACxB,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,OAAO,CAAC,CAAS;IAGnB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAY3B,cAAc,IAAI,MAAM,GAAG,IAAI;IAI/B,QAAQ,CAAC,MAAM,EAAE,MAAM;IAKvB,KAAK,CAAC,OAAO,EAAE,MAAM;IAKrB,SAAS,IAAI,MAAM,GAAG,SAAS;IAI/B,UAAU,IAAI,MAAM,GAAG,SAAS;IAIhC,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC;IAS3D,KAAK;CAMN"}
@@ -1,19 +1,31 @@
1
1
  import { getAnonymousId } from "./utils/anonymousId";
2
+ import { log, warn } from "./utils/logger";
2
3
  export class IdentityManager {
3
4
  constructor() {
4
5
  this.anonymousId = null;
5
6
  }
6
7
  async init() {
7
- this.anonymousId = await getAnonymousId(); // load or generate
8
+ try {
9
+ this.anonymousId = await getAnonymousId(); // load or generate
10
+ log('IdentityManager initialized with anonymous ID:', this.anonymousId);
11
+ }
12
+ catch (error) {
13
+ warn('Failed to initialize IdentityManager, using fallback anonymous ID', error);
14
+ // Fallback: generate a temporary anonymous ID
15
+ this.anonymousId = `fallback-${Date.now()}-${Math.random().toString(36).substring(2, 10)}`;
16
+ log('Using fallback anonymous ID:', this.anonymousId);
17
+ }
8
18
  }
9
19
  getAnonymousId() {
10
20
  return this.anonymousId;
11
21
  }
12
22
  identify(userId) {
13
23
  this.userId = userId;
24
+ log('User identified:', userId);
14
25
  }
15
26
  group(groupId) {
16
27
  this.groupId = groupId;
28
+ log('User grouped:', groupId);
17
29
  }
18
30
  getUserId() {
19
31
  return this.userId;
@@ -34,5 +46,6 @@ export class IdentityManager {
34
46
  this.anonymousId = null;
35
47
  this.userId = undefined;
36
48
  this.groupId = undefined;
49
+ log('IdentityManager reset');
37
50
  }
38
51
  }
@@ -1,6 +1,7 @@
1
1
  import { InitOptions } from "./types";
2
2
  export declare class MetaRouterAnalyticsClient {
3
3
  private initialized;
4
+ private initPromise;
4
5
  private queue;
5
6
  private flushIntervalMs;
6
7
  private flushTimer;
@@ -11,6 +12,7 @@ export declare class MetaRouterAnalyticsClient {
11
12
  private identityManager;
12
13
  constructor(options: InitOptions);
13
14
  private init;
15
+ waitForInitialization(): Promise<void>;
14
16
  private startFlushLoop;
15
17
  private now;
16
18
  private enqueue;
@@ -26,6 +28,23 @@ export declare class MetaRouterAnalyticsClient {
26
28
  * This updates internal identity state and enqueues an alias event.
27
29
  */
28
30
  alias(newUserId: string): void;
31
+ /**
32
+ * Enable debug logging for troubleshooting
33
+ */
34
+ enableDebugLogging(): void;
35
+ /**
36
+ * Get current state for debugging
37
+ */
38
+ getDebugInfo(): {
39
+ initialized: boolean;
40
+ queueLength: number;
41
+ endpoint: string;
42
+ writeKey: string | undefined;
43
+ flushIntervalMs: number;
44
+ anonymousId: string | null;
45
+ userId: string | undefined;
46
+ groupId: string | undefined;
47
+ };
29
48
  flush(): Promise<{
30
49
  anonymousId: string;
31
50
  writeKey: string;
@@ -1 +1 @@
1
- {"version":3,"file":"MetaRouterAnalyticsClient.d.ts","sourceRoot":"","sources":["../../src/analytics/MetaRouterAnalyticsClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,WAAW,EAAE,MAAM,SAAS,CAAC;AAOlD,qBAAa,yBAAyB;IACpC,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,UAAU,CAA+B;IACjD,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO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AAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAyC;IACzD,OAAO,CAAC,oBAAoB,CAAwC;IACpE,OAAO,CAAC,eAAe,CAAkB;gBAE7B,OAAO,EAAE,WAAW;YAalB,IAAI;IA4BZ,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5C,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,GAAG;IAIX,OAAO,CAAC,OAAO;IAMf,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,oBAAoB,CAK1B;IAGF,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAMrD,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAOrD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAMnD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAOnD,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAMrD;;;MAGE;IAEF,KAAK,CAAC,SAAS,EAAE,MAAM;IAOvB;;OAEG;IACH,kBAAkB;IAKlB;;OAEG;IACH,YAAY;;;;;;;;;;IAaN,KAAK;;;;;;;;;;;;;IA6DX,KAAK;IAML,OAAO;CAQR"}
@@ -24,16 +24,34 @@ export class MetaRouterAnalyticsClient {
24
24
  this.flushIntervalMs = flushInterval !== null && flushInterval !== void 0 ? flushInterval : 10000;
25
25
  setDebugLogging((_a = options.debug) !== null && _a !== void 0 ? _a : false);
26
26
  this.identityManager = new IdentityManager();
27
- this.init();
27
+ this.initPromise = this.init();
28
+ log('Analytics client constructor completed, initialization in progress...');
28
29
  }
29
30
  async init() {
30
- if (this.initialized)
31
+ if (this.initialized) {
32
+ log('Analytics client already initialized');
31
33
  return;
32
- await this.identityManager.init();
33
- this.startFlushLoop();
34
- this.setupAppStateListener();
35
- this.initialized = true;
36
- log('Analytics client initialized');
34
+ }
35
+ log('Starting analytics client initialization...');
36
+ try {
37
+ await this.identityManager.init();
38
+ log('IdentityManager initialized successfully');
39
+ this.startFlushLoop();
40
+ log('Flush loop started with interval:', this.flushIntervalMs, 'ms');
41
+ this.setupAppStateListener();
42
+ log('App state listener setup completed');
43
+ this.initialized = true;
44
+ log('Analytics client initialization completed successfully');
45
+ }
46
+ catch (error) {
47
+ warn('Analytics client initialization failed:', error);
48
+ // Still mark as initialized to prevent infinite retries
49
+ this.initialized = true;
50
+ throw error;
51
+ }
52
+ }
53
+ async waitForInitialization() {
54
+ await this.initPromise;
37
55
  }
38
56
  startFlushLoop() {
39
57
  this.flushTimer = setInterval(() => this.flush(), this.flushIntervalMs);
@@ -50,39 +68,75 @@ export class MetaRouterAnalyticsClient {
50
68
  this.appStateSubscription = AppState.addEventListener('change', this.handleAppStateChange);
51
69
  }
52
70
  track(event, properties) {
71
+ log('Tracking event:', event, 'with properties:', properties);
53
72
  this.enqueue({ type: 'track', event, properties, timestamp: this.now() });
73
+ log('Event enqueued, queue length:', this.queue.length);
54
74
  }
55
75
  identify(userId, traits) {
76
+ log('Identifying user:', userId, 'with traits:', traits);
56
77
  this.identityManager.identify(userId);
57
78
  this.enqueue({ type: 'identify', userId, traits, timestamp: this.now() });
79
+ log('Identify event enqueued, queue length:', this.queue.length);
58
80
  }
59
81
  page(name, properties) {
82
+ log('Tracking page:', name, 'with properties:', properties);
60
83
  this.enqueue({ type: 'page', event: name, properties, timestamp: this.now() });
84
+ log('Page event enqueued, queue length:', this.queue.length);
61
85
  }
62
86
  group(groupId, traits) {
87
+ log('Grouping user:', groupId, 'with traits:', traits);
63
88
  this.identityManager.group(groupId);
64
89
  this.enqueue({ type: 'group', groupId, traits, timestamp: this.now() });
90
+ log('Group event enqueued, queue length:', this.queue.length);
65
91
  }
66
92
  screen(name, properties) {
93
+ log('Tracking screen:', name, 'with properties:', properties);
67
94
  this.enqueue({ type: 'screen', event: name, properties, timestamp: this.now() });
95
+ log('Screen event enqueued, queue length:', this.queue.length);
68
96
  }
69
97
  /**
70
98
  * Alias an anonymous user to a known user ID.
71
99
  * This updates internal identity state and enqueues an alias event.
72
100
  */
73
101
  alias(newUserId) {
102
+ log('Aliasing user to:', newUserId);
74
103
  this.identityManager.identify(newUserId);
75
104
  this.enqueue({ type: 'alias', userId: newUserId, timestamp: this.now() });
105
+ log('Alias event enqueued, queue length:', this.queue.length);
106
+ }
107
+ /**
108
+ * Enable debug logging for troubleshooting
109
+ */
110
+ enableDebugLogging() {
111
+ setDebugLogging(true);
112
+ log('Debug logging enabled');
113
+ }
114
+ /**
115
+ * Get current state for debugging
116
+ */
117
+ getDebugInfo() {
118
+ return {
119
+ initialized: this.initialized,
120
+ queueLength: this.queue.length,
121
+ endpoint: this.endpoint,
122
+ writeKey: this.writeKey ? '***' + this.writeKey.slice(-4) : undefined,
123
+ flushIntervalMs: this.flushIntervalMs,
124
+ anonymousId: this.identityManager.getAnonymousId(),
125
+ userId: this.identityManager.getUserId(),
126
+ groupId: this.identityManager.getGroupId(),
127
+ };
76
128
  }
77
129
  async flush() {
78
130
  const anonId = this.identityManager.getAnonymousId();
79
131
  if (!anonId) {
80
- log('Anonymous ID not yet ready, delaying flush');
132
+ warn('Anonymous ID not yet ready, delaying flush');
81
133
  // Anon ID not yet ready, delay flushing
82
134
  return;
83
135
  }
84
- if (this.queue.length === 0)
136
+ if (this.queue.length === 0) {
137
+ log('No events to flush');
85
138
  return;
139
+ }
86
140
  const batch = this.queue.map((event) => ({
87
141
  ...event,
88
142
  anonymousId: anonId,
@@ -94,20 +148,35 @@ export class MetaRouterAnalyticsClient {
94
148
  log(`Flushing ${batch.length} events to ${this.endpoint}/v1/batch`);
95
149
  try {
96
150
  await retryWithBackoff(async () => {
97
- await fetch(this.endpoint + '/v1/batch', {
151
+ log('Making API call to:', this.endpoint + '/v1/batch');
152
+ const response = await fetch(this.endpoint + '/v1/batch', {
98
153
  method: 'POST',
99
154
  headers: {
100
155
  'Content-Type': 'application/json',
101
156
  },
102
157
  body: JSON.stringify({ batch }),
103
158
  });
159
+ if (!response.ok) {
160
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
161
+ }
162
+ log('API call successful, status:', response.status);
163
+ return response;
104
164
  });
105
- log('Flush completed');
165
+ log('Flush completed successfully');
106
166
  return batch;
107
167
  }
108
168
  catch (err) {
109
169
  warn('Flush failed, re-queueing events', err);
110
- this.queue.unshift(...batch); // re-queue enriched events
170
+ // Re-queue the original events (not the enriched ones)
171
+ this.queue.unshift(...this.queue.splice(0, 0, ...batch.map(event => ({
172
+ type: event.type,
173
+ event: event.event,
174
+ properties: event.properties,
175
+ userId: event.userId,
176
+ traits: event.traits,
177
+ groupId: event.groupId,
178
+ timestamp: event.timestamp,
179
+ }))));
111
180
  }
112
181
  }
113
182
  reset() {
@@ -1,11 +1,12 @@
1
1
  import { getContextInfo } from "./utils/contextInfo";
2
+ import { v4 as uuidv4 } from 'uuid';
2
3
  export function enrichEvent(event, anonymousId, writeKey) {
3
4
  var _a;
4
5
  const enriched = {
5
6
  ...event,
6
7
  anonymousId,
7
8
  writeKey,
8
- messageId: `${Date.now()}-${crypto.randomUUID()}`,
9
+ messageId: uuidv4(),
9
10
  sentAt: new Date().toISOString(),
10
11
  timestamp: (_a = event.timestamp) !== null && _a !== void 0 ? _a : new Date().toISOString(),
11
12
  context: getContextInfo(),
@@ -1 +1 @@
1
- {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/analytics/init.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAM/D,wBAAsB,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAiBrF;AAED,wBAAgB,kBAAkB,IAAI,kBAAkB,CAEvD;AAED,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAMpD"}
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/analytics/init.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAM/D,wBAAsB,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAuBrF;AAED,wBAAgB,kBAAkB,IAAI,kBAAkB,CAEvD;AAED,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAMpD"}
@@ -7,6 +7,8 @@ export async function initAnalytics(options) {
7
7
  if (initialized)
8
8
  return analyticsInterface;
9
9
  client = new MetaRouterAnalyticsClient(options);
10
+ // Wait for the client to fully initialize (including anonymous ID loading)
11
+ await client.waitForInitialization();
10
12
  analyticsInterface = {
11
13
  track: (event, props) => client.track(event, props),
12
14
  identify: (userId, traits) => client.identify(userId, traits),
@@ -15,6 +17,8 @@ export async function initAnalytics(options) {
15
17
  alias: (newUserId) => client.alias(newUserId),
16
18
  flush: () => client.flush(),
17
19
  cleanup: () => client.cleanup(),
20
+ enableDebugLogging: () => client.enableDebugLogging(),
21
+ getDebugInfo: () => client.getDebugInfo(),
18
22
  };
19
23
  initialized = true;
20
24
  setRealClient(analyticsInterface);
@@ -22,6 +22,8 @@ export interface AnalyticsInterface {
22
22
  alias: (newUserId: string) => void;
23
23
  flush: () => void;
24
24
  cleanup: () => void;
25
+ enableDebugLogging?: () => void;
26
+ getDebugInfo?: () => Record<string, any>;
25
27
  }
26
28
  export interface EventContext {
27
29
  library: {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/analytics/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,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OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;KACf,CAAC;IACF,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,oBAAqB,SAAQ,YAAY;IACxD,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,YAAY,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB"}
@@ -1 +1 @@
1
- {"version":3,"file":"anonymousId.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/anonymousId.ts"],"names":[],"mappings":"AAKA,wBAAsB,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAOtD"}
1
+ {"version":3,"file":"anonymousId.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/anonymousId.ts"],"names":[],"mappings":"AAMA,wBAAsB,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAsBtD"}
@@ -1,11 +1,26 @@
1
1
  import AsyncStorage from '@react-native-async-storage/async-storage';
2
2
  import { v4 as uuidv4 } from 'uuid';
3
+ import { log, warn } from './logger';
3
4
  const STORAGE_KEY = 'metarouter:anonymous_id';
4
5
  export async function getAnonymousId() {
5
- const existing = await AsyncStorage.getItem(STORAGE_KEY);
6
- if (existing)
7
- return existing;
6
+ try {
7
+ const existing = await AsyncStorage.getItem(STORAGE_KEY);
8
+ if (existing) {
9
+ log('Retrieved existing anonymous ID from storage');
10
+ return existing;
11
+ }
12
+ }
13
+ catch (error) {
14
+ warn('Failed to read anonymous ID from storage, generating new one', error);
15
+ }
8
16
  const anonId = uuidv4();
9
- await AsyncStorage.setItem(STORAGE_KEY, anonId);
17
+ log('Generated new anonymous ID:', anonId);
18
+ try {
19
+ await AsyncStorage.setItem(STORAGE_KEY, anonId);
20
+ log('Stored anonymous ID in storage');
21
+ }
22
+ catch (error) {
23
+ warn('Failed to store anonymous ID in storage, but continuing with generated ID', error);
24
+ }
10
25
  return anonId;
11
26
  }
@@ -1,4 +1,5 @@
1
1
  export declare function setDebugLogging(value: boolean): void;
2
2
  export declare function log(...args: any[]): void;
3
3
  export declare function warn(...args: any[]): void;
4
+ export declare function error(...args: any[]): void;
4
5
  //# sourceMappingURL=logger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/logger.ts"],"names":[],"mappings":"AAEA,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,QAE7C;AAED,wBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAEjC;AAED,wBAAgB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAElC"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/logger.ts"],"names":[],"mappings":"AAEA,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,QAE7C;AAED,wBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAEjC;AAED,wBAAgB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAGlC;AAED,wBAAgB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAGnC"}
@@ -7,6 +7,10 @@ export function log(...args) {
7
7
  console.log('[MetaRouter]', ...args);
8
8
  }
9
9
  export function warn(...args) {
10
- if (enabled)
11
- console.warn('[MetaRouter]', ...args);
10
+ // Always log warnings, even when debug is disabled
11
+ console.warn('[MetaRouter]', ...args);
12
+ }
13
+ export function error(...args) {
14
+ // Always log errors, even when debug is disabled
15
+ console.error('[MetaRouter]', ...args);
12
16
  }
package/lib/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { MetaRouter } from "./MetaRouter";
2
2
  export default MetaRouter;
3
3
  export { MetaRouter };
4
- export { MetaRouterProvider, useMetaRouter } from "./react/context";
4
+ export { MetaRouterProvider } from "./react/context";
5
+ export { useMetaRouter } from "./react/useMetaRouter";
5
6
  export type { InitOptions, AnalyticsInterface } from "./analytics/types";
6
7
  export type { MetaRouterInterface } from "./types";
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,eAAe,UAAU,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,CAAC;AACtB,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACpE,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,eAAe,UAAU,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,CAAC;AACtB,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,YAAY,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC"}
package/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { MetaRouter } from "./MetaRouter";
2
2
  export default MetaRouter;
3
3
  export { MetaRouter };
4
- export { MetaRouterProvider, useMetaRouter } from "./react/context";
4
+ export { MetaRouterProvider } from "./react/context";
5
+ export { useMetaRouter } from "./react/useMetaRouter";
@@ -5,5 +5,4 @@ export declare const MetaRouterProvider: ({ children, instance, }: {
5
5
  children: ReactNode;
6
6
  instance: MetaRouterInterface;
7
7
  }) => React.JSX.Element;
8
- export declare const useMetaRouter: () => MetaRouterInterface;
9
8
  //# sourceMappingURL=context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/react/context.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAA6B,SAAS,EAAE,MAAM,OAAO,CAAC;AACpE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAEpD,eAAO,MAAM,iBAAiB,2CAE7B,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,yBAGhC;IACD,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,mBAAmB,CAAC;CAC/B,sBAMA,CAAC;AAEF,eAAO,MAAM,aAAa,QAAO,mBAMhC,CAAC"}
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/react/context.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAA6B,SAAS,EAAE,MAAM,OAAO,CAAC;AACpE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAEpD,eAAO,MAAM,iBAAiB,2CAE7B,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,yBAGhC;IACD,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,mBAAmB,CAAC;CAC/B,sBAMA,CAAC"}
@@ -1,12 +1,5 @@
1
- import React, { createContext, useContext } from "react";
1
+ import React, { createContext } from "react";
2
2
  export const MetaRouterContext = createContext(null);
3
3
  export const MetaRouterProvider = ({ children, instance, }) => {
4
4
  return (React.createElement(MetaRouterContext.Provider, { value: instance }, children));
5
5
  };
6
- export const useMetaRouter = () => {
7
- const ctx = useContext(MetaRouterContext);
8
- if (!ctx) {
9
- throw new Error("useMetaRouter must be used within a MetaRouterProvider");
10
- }
11
- return ctx;
12
- };
@@ -1,3 +1,5 @@
1
- import { MetaRouter } from "../MetaRouter";
2
- export declare const useMetaRouter: () => typeof MetaRouter;
1
+ import type { AnalyticsInterface } from "../analytics/types";
2
+ export declare const useMetaRouter: () => {
3
+ analytics: AnalyticsInterface;
4
+ };
3
5
  //# sourceMappingURL=useMetaRouter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useMetaRouter.d.ts","sourceRoot":"","sources":["../../src/react/useMetaRouter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAG3C,eAAO,MAAM,aAAa,QAAO,OAAO,UAKrC,CAAC"}
1
+ {"version":3,"file":"useMetaRouter.d.ts","sourceRoot":"","sources":["../../src/react/useMetaRouter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAE7D,eAAO,MAAM,aAAa,QAAO;IAAE,SAAS,EAAE,kBAAkB,CAAA;CAQ7D,CAAC"}
@@ -4,5 +4,7 @@ export const useMetaRouter = () => {
4
4
  const ctx = useContext(MetaRouterContext);
5
5
  if (!ctx)
6
6
  throw new Error("useMetaRouter must be used within MetaRouterProvider");
7
- return ctx;
7
+ return {
8
+ analytics: ctx.analytics.getClient(),
9
+ };
8
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metarouter/react-native-sdk",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "exports": {
@@ -36,6 +36,7 @@
36
36
  "url": "https://github.com/metarouterio/react-native-sdk"
37
37
  },
38
38
  "dependencies": {
39
+ "react-native-get-random-values": "^1.11.0",
39
40
  "uuid": "^11.1.0"
40
41
  },
41
42
  "peerDependencies": {