mimz-react-native-tracker 1.0.0 → 1.0.1

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/bin/mimz-setup.js CHANGED
@@ -84,20 +84,13 @@ const getTrackingInitCode = (cId, bUrl, appName) => {
84
84
  asyncStorage: AsyncStorage,
85
85
  appName: '${appName}',
86
86
  appVersion: '1.0.0',
87
+ autoTrack: true, // Automatically captures UTMs from deep links and handles background sync
87
88
  });
88
89
  } catch (error) {
89
90
  console.warn('Mimz tracking init failed:', error.message);
90
91
  }
91
92
  };
92
93
  initMimzTracking();
93
-
94
- // Flush tracking data when app goes to background
95
- const subscription = require('react-native').AppState.addEventListener('change', (state) => {
96
- if (state === 'background' || state === 'inactive') {
97
- MimzTracker.flush();
98
- }
99
- });
100
- return () => subscription.remove();
101
94
  }, []);
102
95
  // ── End Mimz Tracking ──────────────────────────────────────────────────`;
103
96
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mimz-react-native-tracker",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Mimz Analytics Tracking SDK for React Native apps. Track visitors, UTM parameters, conversions, contacts, and user engagement in native mobile apps.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -44,4 +44,4 @@
44
44
  "type": "git",
45
45
  "url": ""
46
46
  }
47
- }
47
+ }
package/src/index.d.ts CHANGED
@@ -13,6 +13,8 @@ export interface MimzConfig {
13
13
  appName?: string;
14
14
  /** Your app's version */
15
15
  appVersion?: string;
16
+ /** Optional custom user ID / visitor ID */
17
+ userId?: string;
16
18
  }
17
19
 
18
20
  export interface ConversionData {
@@ -105,6 +107,11 @@ export function identify(userData?: UserData): Promise<void>;
105
107
  */
106
108
  export function setUtmParams(utms?: UtmParams): Promise<void>;
107
109
 
110
+ /**
111
+ * Set a custom user ID (visitorId) manually
112
+ */
113
+ export function setUserId(newUserId: string): Promise<void>;
114
+
108
115
  /**
109
116
  * Get the current visitor ID
110
117
  */
package/src/tracker.js CHANGED
@@ -69,6 +69,7 @@ const API_ENDPOINTS = {
69
69
  * @param {object} config.asyncStorage - The AsyncStorage module (REQUIRED - from @react-native-async-storage/async-storage)
70
70
  * @param {string} [config.appName] - Your app's name (for tracking context)
71
71
  * @param {string} [config.appVersion] - Your app's version (for tracking context)
72
+ * @param {string} [config.userId] - Optional custom user ID / visitor ID (to keep it consistent with other tracking like push notifications)
72
73
  *
73
74
  * @example
74
75
  * import AsyncStorage from '@react-native-async-storage/async-storage';
@@ -76,6 +77,7 @@ const API_ENDPOINTS = {
76
77
  *
77
78
  * await MimzTracker.init({
78
79
  * clientId: '68b58625949a81fd08330b38',
80
+ * userId: 'your_custom_user_id', // Optional
79
81
  * backendUrl: 'https://mimz-backend.onrender.com',
80
82
  * asyncStorage: AsyncStorage,
81
83
  * appName: 'MyApp',
@@ -88,7 +90,7 @@ const init = async (config = {}) => {
88
90
  return;
89
91
  }
90
92
 
91
- const { clientId: cId, backendUrl, asyncStorage, appName: an, appVersion: av } = config;
93
+ const { clientId: cId, backendUrl, asyncStorage, appName: an, appVersion: av, userId: customUserId } = config;
92
94
 
93
95
  // Validate required fields
94
96
  if (!cId) {
@@ -114,12 +116,17 @@ const init = async (config = {}) => {
114
116
  sessionStartTime = Date.now();
115
117
 
116
118
  // ── Restore or create visitor ID ────────────────────────────────────────
117
- const storedVisitorId = await storage.get('visitorId');
118
- if (storedVisitorId) {
119
- visitorId = storedVisitorId;
120
- } else {
121
- visitorId = generateUniqueId();
119
+ if (customUserId) {
120
+ visitorId = customUserId;
122
121
  await storage.set('visitorId', visitorId);
122
+ } else {
123
+ const storedVisitorId = await storage.get('visitorId');
124
+ if (storedVisitorId) {
125
+ visitorId = storedVisitorId;
126
+ } else {
127
+ visitorId = generateUniqueId();
128
+ await storage.set('visitorId', visitorId);
129
+ }
123
130
  }
124
131
 
125
132
  // ── Create a new visit ID for this session ────────────────────────────
@@ -173,12 +180,56 @@ const init = async (config = {}) => {
173
180
  const trackingData = await _buildTrackingPayload();
174
181
  await _postVisitorData(trackingData);
175
182
 
183
+ // ── Auto-Tracking (Deep Links & App State) ────────────────────────────
184
+ if (config.autoTrack !== false) {
185
+ _setupAutoTracking();
186
+ }
187
+
176
188
  console.log('[MimzTracker] ✅ Initialized successfully');
177
189
  console.log(`[MimzTracker] visitorId: ${visitorId}`);
178
190
  console.log(`[MimzTracker] visitId: ${visitId}`);
179
191
  console.log(`[MimzTracker] sessionCount: ${sessionCount}`);
180
192
  };
181
193
 
194
+ /**
195
+ * Automatically sets up listeners for deep links and app lifecycle
196
+ */
197
+ const _setupAutoTracking = () => {
198
+ try {
199
+ const { AppState, Linking } = require('react-native');
200
+
201
+ // 1. Flush data when app goes to background
202
+ if (AppState) {
203
+ AppState.addEventListener('change', (state) => {
204
+ if (state === 'background' || state === 'inactive') {
205
+ flush();
206
+ }
207
+ });
208
+ console.log('[MimzTracker] 🔄 Auto-flush on background enabled');
209
+ }
210
+
211
+ // 2. Automatically capture UTMs from deep links
212
+ if (Linking) {
213
+ // Handle app opened from background via deep link
214
+ Linking.addEventListener('url', (event) => {
215
+ if (event.url) {
216
+ trackDeepLink(event.url, 'auto_linking');
217
+ }
218
+ });
219
+
220
+ // Handle app started cold via deep link
221
+ Linking.getInitialURL().then((url) => {
222
+ if (url) {
223
+ trackDeepLink(url, 'auto_initial_url');
224
+ }
225
+ });
226
+ console.log('[MimzTracker] 🔗 Auto-deep-link UTM tracking enabled');
227
+ }
228
+ } catch (error) {
229
+ console.warn('[MimzTracker] ⚠️ Could not setup auto-tracking. Are you in a React Native environment?');
230
+ }
231
+ };
232
+
182
233
  /**
183
234
  * Track a deep link URL — extracts and stores UTM parameters
184
235
  * Call this whenever your app receives a deep link / universal link
@@ -447,6 +498,25 @@ const setUtmParams = async (utms = {}) => {
447
498
  console.log('[MimzTracker] 🏷️ UTM params set:', utms);
448
499
  };
449
500
 
501
+ /**
502
+ * Set a custom user ID (visitorId) manually
503
+ * Use this when the user logs in or when you receive a push notification ID
504
+ *
505
+ * @param {string} newUserId
506
+ */
507
+ const setUserId = async (newUserId) => {
508
+ if (!newUserId) return;
509
+ visitorId = newUserId;
510
+ await storage.set('visitorId', visitorId);
511
+ console.log(`[MimzTracker] 🆔 User ID set to: ${newUserId}`);
512
+
513
+ if (isInitialized) {
514
+ // Re-post visitor data with updated user info
515
+ const trackingData = await _buildTrackingPayload();
516
+ await _postVisitorData(trackingData);
517
+ }
518
+ };
519
+
450
520
  /**
451
521
  * Get the current visitor ID
452
522
  * @returns {string|null}
@@ -639,6 +709,7 @@ module.exports = {
639
709
  trackContact,
640
710
  identify,
641
711
  setUtmParams,
712
+ setUserId,
642
713
  getVisitorId,
643
714
  getVisitId,
644
715
  getSessionDuration,