@metarouter/react-native-sdk 0.1.24 → 0.1.26
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 +5 -5
- package/lib/analytics/IdentityManager.d.ts +3 -3
- package/lib/analytics/IdentityManager.d.ts.map +1 -1
- package/lib/analytics/IdentityManager.js +29 -6
- package/lib/analytics/MetaRouterAnalyticsClient.d.ts +7 -6
- package/lib/analytics/MetaRouterAnalyticsClient.d.ts.map +1 -1
- package/lib/analytics/MetaRouterAnalyticsClient.js +68 -53
- package/lib/analytics/init.js +7 -7
- package/lib/analytics/proxy/proxyClient.d.ts.map +1 -1
- package/lib/analytics/proxy/proxyClient.js +5 -2
- package/lib/analytics/types.d.ts +2 -2
- package/lib/analytics/types.d.ts.map +1 -1
- package/lib/analytics/utils/identityStorage.d.ts +7 -0
- package/lib/analytics/utils/identityStorage.d.ts.map +1 -0
- package/lib/analytics/utils/identityStorage.js +28 -0
- package/package.json +1 -1
- package/lib/analytics/utils/anonymousId.d.ts +0 -8
- package/lib/analytics/utils/anonymousId.d.ts.map +0 -1
- package/lib/analytics/utils/anonymousId.js +0 -32
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ import MetaRouter from "@metarouter/react-native-sdk";
|
|
|
21
21
|
// Initialize the analytics client
|
|
22
22
|
const analytics = await MetaRouter.analytics.init({
|
|
23
23
|
writeKey: "your-write-key",
|
|
24
|
-
|
|
24
|
+
ingestionHost: "https://your-ingestion-endpoint.com",
|
|
25
25
|
debug: true, // Optional: enable debug mode
|
|
26
26
|
flushInterval: 30, // Optional: flush events every 30 seconds
|
|
27
27
|
});
|
|
@@ -46,7 +46,7 @@ const App = () => {
|
|
|
46
46
|
// Initialize the analytics client
|
|
47
47
|
await MetaRouter.analytics.init({
|
|
48
48
|
writeKey: "your-write-key",
|
|
49
|
-
|
|
49
|
+
ingestionHost: "https://your-ingestion-endpoint.com",
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
setMetaRouterInstance(MetaRouter);
|
|
@@ -91,7 +91,7 @@ import MetaRouter from "@metarouter/react-native-sdk";
|
|
|
91
91
|
// Initialize
|
|
92
92
|
const analytics = await MetaRouter.analytics.init({
|
|
93
93
|
writeKey: "your-write-key",
|
|
94
|
-
|
|
94
|
+
ingestionHost: "https://your-ingestion-endpoint.com/events",
|
|
95
95
|
});
|
|
96
96
|
|
|
97
97
|
// Track events
|
|
@@ -136,7 +136,7 @@ Initializes the analytics client and returns a promise that resolves to the clie
|
|
|
136
136
|
**Options:**
|
|
137
137
|
|
|
138
138
|
- `writeKey` (string, required): Your write key
|
|
139
|
-
- `
|
|
139
|
+
- `ingestionHost` (string, required): Your MetaRouter ingestor host
|
|
140
140
|
- `debug` (boolean, optional): Enable debug mode
|
|
141
141
|
- `flushInterval` (number, optional): Interval in seconds to flush events
|
|
142
142
|
|
|
@@ -188,7 +188,7 @@ If you're not seeing API calls being made, here are some steps to troubleshoot:
|
|
|
188
188
|
// Initialize with debug enabled
|
|
189
189
|
const analytics = await MetaRouter.analytics.init({
|
|
190
190
|
writeKey: "your-write-key",
|
|
191
|
-
|
|
191
|
+
ingestionHost: "https://your-ingestion-endpoint.com",
|
|
192
192
|
debug: true, // This enables detailed logging
|
|
193
193
|
});
|
|
194
194
|
|
|
@@ -24,12 +24,12 @@ export declare class IdentityManager {
|
|
|
24
24
|
* Sets the user ID for the current session.
|
|
25
25
|
* @param userId - The user ID to set.
|
|
26
26
|
*/
|
|
27
|
-
identify(userId: string): void
|
|
27
|
+
identify(userId: string): Promise<void>;
|
|
28
28
|
/**
|
|
29
29
|
* Sets the group ID for the current session.
|
|
30
30
|
* @param groupId - The group ID to set.
|
|
31
31
|
*/
|
|
32
|
-
group(groupId: string): void
|
|
32
|
+
group(groupId: string): Promise<void>;
|
|
33
33
|
/**
|
|
34
34
|
* Retrieves the current user ID.
|
|
35
35
|
* @returns The user ID or undefined if not set.
|
|
@@ -50,6 +50,6 @@ export declare class IdentityManager {
|
|
|
50
50
|
* Resets the identity manager to its initial state.
|
|
51
51
|
* Clears all user and group IDs, and sets the anonymous ID to null.
|
|
52
52
|
*/
|
|
53
|
-
reset(): void
|
|
53
|
+
reset(): Promise<void>;
|
|
54
54
|
}
|
|
55
55
|
//# sourceMappingURL=IdentityManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdentityManager.d.ts","sourceRoot":"","sources":["../../src/analytics/IdentityManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"IdentityManager.d.ts","sourceRoot":"","sources":["../../src/analytics/IdentityManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAG1D;;;;;;GAMG;AACH,qBAAa,eAAe;IACxB,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,OAAO,CAAC,CAAS;IAEzB;;;KAGC;IACK,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAe3B;;;OAGG;IACH,cAAc,IAAI,MAAM,GAAG,IAAI;IAI/B;;;OAGG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM;IAU7B;;;OAGG;IACG,KAAK,CAAC,OAAO,EAAE,MAAM;IAU3B;;;OAGG;IACH,SAAS,IAAI,MAAM,GAAG,SAAS;IAI/B;;;OAGG;IACH,UAAU,IAAI,MAAM,GAAG,SAAS;IAIhC;;;;OAIG;IACH,eAAe,CAAC,CAAC,SAAS,YAAY,EAAE,KAAK,EAAE,CAAC,GAAG,iBAAiB;IASpE;;;OAGG;IACG,KAAK;CAaZ"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getAnonymousId } from "./utils/anonymousId";
|
|
2
1
|
import { log, warn } from "./utils/logger";
|
|
2
|
+
import { USER_ID_KEY, GROUP_ID_KEY, ANONYMOUS_ID_KEY, getIdentityField, setIdentityField, removeIdentityField } from './utils/identityStorage';
|
|
3
3
|
/**
|
|
4
4
|
* Manages user, group, and anonymous identity for analytics events.
|
|
5
5
|
* - Loads or generates a persistent anonymous ID.
|
|
@@ -17,8 +17,11 @@ export class IdentityManager {
|
|
|
17
17
|
*/
|
|
18
18
|
async init() {
|
|
19
19
|
try {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
// Load userId and groupId from storage
|
|
21
|
+
this.anonymousId = await getIdentityField(ANONYMOUS_ID_KEY);
|
|
22
|
+
this.userId = await getIdentityField(USER_ID_KEY) || undefined;
|
|
23
|
+
this.groupId = await getIdentityField(GROUP_ID_KEY) || undefined;
|
|
24
|
+
log('IdentityManager initialized with anonymous ID:', this.anonymousId, 'userId:', this.userId, 'groupId:', this.groupId);
|
|
22
25
|
}
|
|
23
26
|
catch (error) {
|
|
24
27
|
warn('Failed to initialize IdentityManager, using fallback anonymous ID', error);
|
|
@@ -38,16 +41,28 @@ export class IdentityManager {
|
|
|
38
41
|
* Sets the user ID for the current session.
|
|
39
42
|
* @param userId - The user ID to set.
|
|
40
43
|
*/
|
|
41
|
-
identify(userId) {
|
|
44
|
+
async identify(userId) {
|
|
42
45
|
this.userId = userId;
|
|
46
|
+
try {
|
|
47
|
+
await setIdentityField(USER_ID_KEY, userId);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
warn('Failed to persist userId', error);
|
|
51
|
+
}
|
|
43
52
|
log('User identified:', userId);
|
|
44
53
|
}
|
|
45
54
|
/**
|
|
46
55
|
* Sets the group ID for the current session.
|
|
47
56
|
* @param groupId - The group ID to set.
|
|
48
57
|
*/
|
|
49
|
-
group(groupId) {
|
|
58
|
+
async group(groupId) {
|
|
50
59
|
this.groupId = groupId;
|
|
60
|
+
try {
|
|
61
|
+
await setIdentityField(GROUP_ID_KEY, groupId);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
warn('Failed to persist groupId', error);
|
|
65
|
+
}
|
|
51
66
|
log('User grouped:', groupId);
|
|
52
67
|
}
|
|
53
68
|
/**
|
|
@@ -82,10 +97,18 @@ export class IdentityManager {
|
|
|
82
97
|
* Resets the identity manager to its initial state.
|
|
83
98
|
* Clears all user and group IDs, and sets the anonymous ID to null.
|
|
84
99
|
*/
|
|
85
|
-
reset() {
|
|
100
|
+
async reset() {
|
|
86
101
|
this.anonymousId = null;
|
|
87
102
|
this.userId = undefined;
|
|
88
103
|
this.groupId = undefined;
|
|
104
|
+
try {
|
|
105
|
+
await removeIdentityField(ANONYMOUS_ID_KEY);
|
|
106
|
+
await removeIdentityField(USER_ID_KEY);
|
|
107
|
+
await removeIdentityField(GROUP_ID_KEY);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
warn('Failed to clear userId/groupId/anonymousId from storage', error);
|
|
111
|
+
}
|
|
89
112
|
log('IdentityManager reset');
|
|
90
113
|
}
|
|
91
114
|
}
|
|
@@ -12,12 +12,13 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
12
12
|
private queue;
|
|
13
13
|
private flushIntervalMs;
|
|
14
14
|
private flushTimer;
|
|
15
|
-
private
|
|
15
|
+
private ingestionHost;
|
|
16
16
|
private writeKey;
|
|
17
17
|
private context;
|
|
18
18
|
private appState;
|
|
19
19
|
private appStateSubscription;
|
|
20
20
|
private identityManager;
|
|
21
|
+
private static readonly MAX_QUEUE_SIZE;
|
|
21
22
|
/**
|
|
22
23
|
* Initializes the analytics client with the provided options.
|
|
23
24
|
* @param options - The initialization options.
|
|
@@ -87,10 +88,10 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
87
88
|
*/
|
|
88
89
|
screen(name: string, properties?: Record<string, any>): void;
|
|
89
90
|
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
* Alias an anonymous user to a known user ID.
|
|
92
|
+
* This updates internal identity state and enqueues an alias event.
|
|
93
|
+
* @param newUserId - The new user ID to alias to.
|
|
94
|
+
*/
|
|
94
95
|
alias(newUserId: string): void;
|
|
95
96
|
/**
|
|
96
97
|
* Enable debug logging for troubleshooting
|
|
@@ -102,7 +103,7 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
102
103
|
getDebugInfo(): {
|
|
103
104
|
initialized: boolean;
|
|
104
105
|
queueLength: number;
|
|
105
|
-
|
|
106
|
+
ingestionHost: string;
|
|
106
107
|
writeKey: string | undefined;
|
|
107
108
|
flushIntervalMs: number;
|
|
108
109
|
anonymousId: string | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MetaRouterAnalyticsClient.d.ts","sourceRoot":"","sources":["../../src/analytics/MetaRouterAnalyticsClient.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"MetaRouterAnalyticsClient.d.ts","sourceRoot":"","sources":["../../src/analytics/MetaRouterAnalyticsClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,WAAW,EACZ,MAAM,SAAS,CAAC;AAQjB;;;;;;GAMG;AACH,qBAAa,yBAAyB;IACpC,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,UAAU,CAA+B;IACjD,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,QAAQ,CAAyC;IACzD,OAAO,CAAC,oBAAoB,CAAwC;IACpE,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAM;IAE5C;;;OAGG;gBACS,OAAO,EAAE,WAAW;IAchC;;;OAGG;YACW,IAAI;IA8BlB;;;OAGG;IACG,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5C;;OAEG;IACH,OAAO,CAAC,cAAc;IAItB;;;OAGG;IACH,OAAO,CAAC,GAAG;IAIX;;;OAGG;IACH,OAAO,CAAC,OAAO;IAef;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAO7B;;;OAGG;IACH,OAAO,CAAC,oBAAoB,CAK1B;IAEF;;;;OAIG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAMrD;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAOrD;;;;OAIG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAWnD;;;;OAIG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAOnD;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAWrD;;;;OAIG;IACH,KAAK,CAAC,SAAS,EAAE,MAAM;IAOvB;;OAEG;IACH,kBAAkB;IAKlB;;OAEG;IACH,YAAY;;;;;;;;;;IAaZ;;OAEG;IACG,KAAK;;;;;;;;;;IAkDX;;OAEG;IACH,KAAK;IAML;;OAEG;IACH,OAAO;CAOR"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppState } from
|
|
1
|
+
import { AppState } from "react-native";
|
|
2
2
|
import { retryWithBackoff } from "./utils/retry";
|
|
3
3
|
import { error, log, setDebugLogging, warn } from "./utils/logger";
|
|
4
4
|
import { IdentityManager } from "./IdentityManager";
|
|
@@ -29,20 +29,20 @@ export class MetaRouterAnalyticsClient {
|
|
|
29
29
|
* @param nextState - The new app state.
|
|
30
30
|
*/
|
|
31
31
|
this.handleAppStateChange = (nextState) => {
|
|
32
|
-
if (this.appState ===
|
|
32
|
+
if (this.appState === "active" && nextState.match(/inactive|background/)) {
|
|
33
33
|
this.flush();
|
|
34
34
|
}
|
|
35
35
|
this.appState = nextState;
|
|
36
36
|
};
|
|
37
|
-
log(
|
|
38
|
-
const { writeKey,
|
|
39
|
-
this.
|
|
37
|
+
log("Initializing analytics client", options);
|
|
38
|
+
const { writeKey, ingestionHost, flushInterval } = options;
|
|
39
|
+
this.ingestionHost = ingestionHost;
|
|
40
40
|
this.writeKey = writeKey;
|
|
41
41
|
this.flushIntervalMs = flushInterval !== null && flushInterval !== void 0 ? flushInterval : 10000;
|
|
42
42
|
setDebugLogging((_a = options.debug) !== null && _a !== void 0 ? _a : false);
|
|
43
43
|
this.identityManager = new IdentityManager();
|
|
44
44
|
this.initPromise = this.init();
|
|
45
|
-
log(
|
|
45
|
+
log("Analytics client constructor completed, initialization in progress...");
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Initializes the analytics client.
|
|
@@ -50,23 +50,23 @@ export class MetaRouterAnalyticsClient {
|
|
|
50
50
|
*/
|
|
51
51
|
async init() {
|
|
52
52
|
if (this.initialized) {
|
|
53
|
-
log(
|
|
53
|
+
log("Analytics client already initialized");
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
|
-
log(
|
|
56
|
+
log("Starting analytics client initialization...");
|
|
57
57
|
try {
|
|
58
58
|
await this.identityManager.init();
|
|
59
|
-
log(
|
|
59
|
+
log("IdentityManager initialized successfully");
|
|
60
60
|
this.startFlushLoop();
|
|
61
|
-
log(
|
|
61
|
+
log("Flush loop started with interval:", this.flushIntervalMs, "ms");
|
|
62
62
|
this.setupAppStateListener();
|
|
63
|
-
log(
|
|
63
|
+
log("App state listener setup completed");
|
|
64
64
|
this.context = await getContextInfo();
|
|
65
65
|
this.initialized = true;
|
|
66
|
-
log(
|
|
66
|
+
log("Analytics client initialization completed successfully");
|
|
67
67
|
}
|
|
68
68
|
catch (error) {
|
|
69
|
-
warn(
|
|
69
|
+
warn("Analytics client initialization failed:", error);
|
|
70
70
|
// Still mark as initialized to prevent infinite retries
|
|
71
71
|
this.initialized = true;
|
|
72
72
|
throw error;
|
|
@@ -97,16 +97,20 @@ export class MetaRouterAnalyticsClient {
|
|
|
97
97
|
* @param event - The event to enqueue.
|
|
98
98
|
*/
|
|
99
99
|
enqueue(event) {
|
|
100
|
+
if (this.queue.length >= MetaRouterAnalyticsClient.MAX_QUEUE_SIZE) {
|
|
101
|
+
log(`Event queue reached max size (${MetaRouterAnalyticsClient.MAX_QUEUE_SIZE}). Attempting to flush queued events before adding new.`);
|
|
102
|
+
this.flush();
|
|
103
|
+
}
|
|
100
104
|
const eventWithIdentity = this.identityManager.addIdentityInfo(event);
|
|
101
105
|
const enrichedEvent = enrichEvent(eventWithIdentity, this.writeKey, this.context);
|
|
102
|
-
log(
|
|
106
|
+
log("Enqueuing event", enrichedEvent);
|
|
103
107
|
this.queue.push(enrichedEvent);
|
|
104
108
|
}
|
|
105
109
|
/**
|
|
106
110
|
* Sets up the app state listener.
|
|
107
111
|
*/
|
|
108
112
|
setupAppStateListener() {
|
|
109
|
-
this.appStateSubscription = AppState.addEventListener(
|
|
113
|
+
this.appStateSubscription = AppState.addEventListener("change", this.handleAppStateChange);
|
|
110
114
|
}
|
|
111
115
|
/**
|
|
112
116
|
* Tracks an event.
|
|
@@ -114,9 +118,9 @@ export class MetaRouterAnalyticsClient {
|
|
|
114
118
|
* @param properties - The properties to track.
|
|
115
119
|
*/
|
|
116
120
|
track(event, properties) {
|
|
117
|
-
log(
|
|
118
|
-
this.enqueue({ type:
|
|
119
|
-
log(
|
|
121
|
+
log("Tracking event:", event, "with properties:", properties);
|
|
122
|
+
this.enqueue({ type: "track", event, properties, timestamp: this.now() });
|
|
123
|
+
log("Event enqueued, queue length:", this.queue.length);
|
|
120
124
|
}
|
|
121
125
|
/**
|
|
122
126
|
* Identifies a user.
|
|
@@ -124,10 +128,10 @@ export class MetaRouterAnalyticsClient {
|
|
|
124
128
|
* @param traits - The traits to identify the user with.
|
|
125
129
|
*/
|
|
126
130
|
identify(userId, traits) {
|
|
127
|
-
log(
|
|
131
|
+
log("Identifying user:", userId, "with traits:", traits);
|
|
128
132
|
this.identityManager.identify(userId);
|
|
129
|
-
this.enqueue({ type:
|
|
130
|
-
log(
|
|
133
|
+
this.enqueue({ type: "identify", userId, traits, timestamp: this.now() });
|
|
134
|
+
log("Identify event enqueued, queue length:", this.queue.length);
|
|
131
135
|
}
|
|
132
136
|
/**
|
|
133
137
|
* Tracks a page view.
|
|
@@ -135,9 +139,14 @@ export class MetaRouterAnalyticsClient {
|
|
|
135
139
|
* @param properties - The properties to track.
|
|
136
140
|
*/
|
|
137
141
|
page(name, properties) {
|
|
138
|
-
log(
|
|
139
|
-
this.enqueue({
|
|
140
|
-
|
|
142
|
+
log("Tracking page:", name, "with properties:", properties);
|
|
143
|
+
this.enqueue({
|
|
144
|
+
type: "page",
|
|
145
|
+
event: name,
|
|
146
|
+
properties,
|
|
147
|
+
timestamp: this.now(),
|
|
148
|
+
});
|
|
149
|
+
log("Page event enqueued, queue length:", this.queue.length);
|
|
141
150
|
}
|
|
142
151
|
/**
|
|
143
152
|
* Groups a user.
|
|
@@ -145,10 +154,10 @@ export class MetaRouterAnalyticsClient {
|
|
|
145
154
|
* @param traits - The traits to group the user with.
|
|
146
155
|
*/
|
|
147
156
|
group(groupId, traits) {
|
|
148
|
-
log(
|
|
157
|
+
log("Grouping user:", groupId, "with traits:", traits);
|
|
149
158
|
this.identityManager.group(groupId);
|
|
150
|
-
this.enqueue({ type:
|
|
151
|
-
log(
|
|
159
|
+
this.enqueue({ type: "group", groupId, traits, timestamp: this.now() });
|
|
160
|
+
log("Group event enqueued, queue length:", this.queue.length);
|
|
152
161
|
}
|
|
153
162
|
/**
|
|
154
163
|
* Tracks a screen view.
|
|
@@ -156,27 +165,32 @@ export class MetaRouterAnalyticsClient {
|
|
|
156
165
|
* @param properties - The properties to track.
|
|
157
166
|
*/
|
|
158
167
|
screen(name, properties) {
|
|
159
|
-
log(
|
|
160
|
-
this.enqueue({
|
|
161
|
-
|
|
168
|
+
log("Tracking screen:", name, "with properties:", properties);
|
|
169
|
+
this.enqueue({
|
|
170
|
+
type: "screen",
|
|
171
|
+
event: name,
|
|
172
|
+
properties,
|
|
173
|
+
timestamp: this.now(),
|
|
174
|
+
});
|
|
175
|
+
log("Screen event enqueued, queue length:", this.queue.length);
|
|
162
176
|
}
|
|
163
177
|
/**
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
178
|
+
* Alias an anonymous user to a known user ID.
|
|
179
|
+
* This updates internal identity state and enqueues an alias event.
|
|
180
|
+
* @param newUserId - The new user ID to alias to.
|
|
181
|
+
*/
|
|
168
182
|
alias(newUserId) {
|
|
169
|
-
log(
|
|
183
|
+
log("Aliasing user to:", newUserId);
|
|
170
184
|
this.identityManager.identify(newUserId);
|
|
171
|
-
this.enqueue({ type:
|
|
172
|
-
log(
|
|
185
|
+
this.enqueue({ type: "alias", userId: newUserId, timestamp: this.now() });
|
|
186
|
+
log("Alias event enqueued, queue length:", this.queue.length);
|
|
173
187
|
}
|
|
174
188
|
/**
|
|
175
189
|
* Enable debug logging for troubleshooting
|
|
176
190
|
*/
|
|
177
191
|
enableDebugLogging() {
|
|
178
192
|
setDebugLogging(true);
|
|
179
|
-
log(
|
|
193
|
+
log("Debug logging enabled");
|
|
180
194
|
}
|
|
181
195
|
/**
|
|
182
196
|
* Get current state for debugging
|
|
@@ -185,8 +199,8 @@ export class MetaRouterAnalyticsClient {
|
|
|
185
199
|
return {
|
|
186
200
|
initialized: this.initialized,
|
|
187
201
|
queueLength: this.queue.length,
|
|
188
|
-
|
|
189
|
-
writeKey: this.writeKey ?
|
|
202
|
+
ingestionHost: this.ingestionHost,
|
|
203
|
+
writeKey: this.writeKey ? "***" + this.writeKey.slice(-4) : undefined,
|
|
190
204
|
flushIntervalMs: this.flushIntervalMs,
|
|
191
205
|
anonymousId: this.identityManager.getAnonymousId(),
|
|
192
206
|
userId: this.identityManager.getUserId(),
|
|
@@ -199,12 +213,12 @@ export class MetaRouterAnalyticsClient {
|
|
|
199
213
|
async flush() {
|
|
200
214
|
const anonId = this.identityManager.getAnonymousId();
|
|
201
215
|
if (!anonId) {
|
|
202
|
-
warn(
|
|
216
|
+
warn("Anonymous ID not yet ready, delaying flush");
|
|
203
217
|
// Anon ID not yet ready, delay flushing
|
|
204
218
|
return;
|
|
205
219
|
}
|
|
206
220
|
if (this.queue.length === 0) {
|
|
207
|
-
log(
|
|
221
|
+
log("No events to flush");
|
|
208
222
|
return;
|
|
209
223
|
}
|
|
210
224
|
const batch = this.queue.map((event) => ({
|
|
@@ -212,29 +226,29 @@ export class MetaRouterAnalyticsClient {
|
|
|
212
226
|
sentAt: new Date().toISOString(),
|
|
213
227
|
}));
|
|
214
228
|
this.queue = [];
|
|
215
|
-
log(`Flushing ${batch.length} events to ${this.
|
|
229
|
+
log(`Flushing ${batch.length} events to ${this.ingestionHost}/v1/batch`);
|
|
216
230
|
try {
|
|
217
231
|
await retryWithBackoff(async () => {
|
|
218
|
-
log(
|
|
219
|
-
const response = await fetch(this.
|
|
220
|
-
method:
|
|
232
|
+
log("Making API call to:", this.ingestionHost + "/v1/batch");
|
|
233
|
+
const response = await fetch(this.ingestionHost + "/v1/batch", {
|
|
234
|
+
method: "POST",
|
|
221
235
|
headers: {
|
|
222
|
-
|
|
236
|
+
"Content-Type": "application/json",
|
|
223
237
|
},
|
|
224
238
|
body: JSON.stringify({ batch }),
|
|
225
239
|
});
|
|
226
240
|
if (!response.ok) {
|
|
227
|
-
error(
|
|
241
|
+
error("HTTP error", response);
|
|
228
242
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
229
243
|
}
|
|
230
|
-
log(
|
|
244
|
+
log("API call successful, status:", response.status);
|
|
231
245
|
return response;
|
|
232
246
|
});
|
|
233
|
-
log(
|
|
247
|
+
log("Flush completed successfully");
|
|
234
248
|
return batch;
|
|
235
249
|
}
|
|
236
250
|
catch (err) {
|
|
237
|
-
warn(
|
|
251
|
+
warn("Flush failed, re-queueing events", err);
|
|
238
252
|
this.queue.unshift(...batch);
|
|
239
253
|
}
|
|
240
254
|
}
|
|
@@ -244,14 +258,14 @@ export class MetaRouterAnalyticsClient {
|
|
|
244
258
|
reset() {
|
|
245
259
|
this.identityManager.reset();
|
|
246
260
|
this.queue = [];
|
|
247
|
-
log(
|
|
261
|
+
log("Analytics client reset");
|
|
248
262
|
}
|
|
249
263
|
/**
|
|
250
264
|
* Cleans up the analytics client.
|
|
251
265
|
*/
|
|
252
266
|
cleanup() {
|
|
253
267
|
var _a, _b;
|
|
254
|
-
log(
|
|
268
|
+
log("Cleaning up analytics client");
|
|
255
269
|
if (this.flushTimer)
|
|
256
270
|
clearInterval(this.flushTimer);
|
|
257
271
|
this.queue = [];
|
|
@@ -259,3 +273,4 @@ export class MetaRouterAnalyticsClient {
|
|
|
259
273
|
(_b = (_a = this.appStateSubscription) === null || _a === void 0 ? void 0 : _a.remove) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
260
274
|
}
|
|
261
275
|
}
|
|
276
|
+
MetaRouterAnalyticsClient.MAX_QUEUE_SIZE = 20;
|
package/lib/analytics/init.js
CHANGED
|
@@ -2,7 +2,7 @@ import { MetaRouterAnalyticsClient } from './MetaRouterAnalyticsClient';
|
|
|
2
2
|
import { proxyClient, setRealClient } from './proxy/proxyClient';
|
|
3
3
|
let initialized = false;
|
|
4
4
|
let client = null;
|
|
5
|
-
let
|
|
5
|
+
let analyticsClient = null;
|
|
6
6
|
/**
|
|
7
7
|
* Initializes the analytics client singleton.
|
|
8
8
|
* Waits for async setup (e.g., identity loading) before returning the interface.
|
|
@@ -12,11 +12,11 @@ let analyticsInterface = null;
|
|
|
12
12
|
*/
|
|
13
13
|
export async function initAnalytics(options) {
|
|
14
14
|
if (initialized)
|
|
15
|
-
return
|
|
15
|
+
return analyticsClient;
|
|
16
16
|
client = new MetaRouterAnalyticsClient(options);
|
|
17
17
|
// Wait for the client to fully initialize (including anonymous ID loading)
|
|
18
18
|
await client.waitForInitialization();
|
|
19
|
-
|
|
19
|
+
analyticsClient = {
|
|
20
20
|
track: (event, props) => client.track(event, props),
|
|
21
21
|
identify: (userId, traits) => client.identify(userId, traits),
|
|
22
22
|
group: (groupId, traits) => client.group(groupId, traits),
|
|
@@ -28,8 +28,8 @@ export async function initAnalytics(options) {
|
|
|
28
28
|
getDebugInfo: () => client.getDebugInfo(),
|
|
29
29
|
};
|
|
30
30
|
initialized = true;
|
|
31
|
-
setRealClient(
|
|
32
|
-
return
|
|
31
|
+
setRealClient(analyticsClient);
|
|
32
|
+
return analyticsClient;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Retrieves the analytics client singleton.
|
|
@@ -37,7 +37,7 @@ export async function initAnalytics(options) {
|
|
|
37
37
|
* @returns The analytics interface.
|
|
38
38
|
*/
|
|
39
39
|
export function getAnalyticsClient() {
|
|
40
|
-
return initialized ?
|
|
40
|
+
return initialized ? analyticsClient : proxyClient;
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Resets the analytics client singleton.
|
|
@@ -47,7 +47,7 @@ export function getAnalyticsClient() {
|
|
|
47
47
|
export async function resetAnalytics() {
|
|
48
48
|
await (client === null || client === void 0 ? void 0 : client.cleanup());
|
|
49
49
|
client = null;
|
|
50
|
-
|
|
50
|
+
analyticsClient = null;
|
|
51
51
|
initialized = false;
|
|
52
52
|
setRealClient(null);
|
|
53
53
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxyClient.d.ts","sourceRoot":"","sources":["../../../src/analytics/proxy/proxyClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"proxyClient.d.ts","sourceRoot":"","sources":["../../../src/analytics/proxy/proxyClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AA2BnD;;;;;;;;;;;;GAYG;AAEH,eAAO,MAAM,WAAW,EAAE,kBAWzB,CAAC;AAEF,wBAAgB,aAAa,CAAC,MAAM,EAAE,kBAAkB,GAAG,IAAI,QAO9D"}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
const pendingCalls = [];
|
|
2
|
+
const MAX_PENDING_CALLS = 20;
|
|
2
3
|
let realClient = null;
|
|
3
4
|
function handleMethodCall(methodName, ...args) {
|
|
4
5
|
if (realClient) {
|
|
5
6
|
return realClient[methodName](...args);
|
|
6
7
|
}
|
|
7
|
-
if (pendingCalls.length
|
|
8
|
-
|
|
8
|
+
if (pendingCalls.length >= MAX_PENDING_CALLS) {
|
|
9
|
+
// Drop the oldest call
|
|
10
|
+
pendingCalls.shift();
|
|
11
|
+
console.warn(`[MetaRouter] Proxy queue reached max size (${MAX_PENDING_CALLS}). Oldest call dropped.`);
|
|
9
12
|
}
|
|
10
13
|
pendingCalls.push(() => {
|
|
11
14
|
if (realClient) {
|
package/lib/analytics/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type EventType =
|
|
1
|
+
export type EventType = "track" | "identify" | "group" | "screen" | "alias" | "page";
|
|
2
2
|
export interface EventPayload {
|
|
3
3
|
type: EventType;
|
|
4
4
|
event?: string;
|
|
@@ -19,7 +19,7 @@ export interface EnrichedEventPayload extends EventWithIdentity {
|
|
|
19
19
|
}
|
|
20
20
|
export interface InitOptions {
|
|
21
21
|
writeKey: string;
|
|
22
|
-
|
|
22
|
+
ingestionHost: string;
|
|
23
23
|
flushInterval?: number;
|
|
24
24
|
debug?: boolean;
|
|
25
25
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/analytics/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/analytics/types.ts"],"names":[],"mappings":"AAAA,MAA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|
+
export declare const USER_ID_KEY = "metarouter:user_id";
|
|
3
|
+
export declare const GROUP_ID_KEY = "metarouter:group_id";
|
|
4
|
+
export declare function getIdentityField(key: string): Promise<string | null>;
|
|
5
|
+
export declare function setIdentityField(key: string, value: string): Promise<void>;
|
|
6
|
+
export declare function removeIdentityField(key: string): Promise<void>;
|
|
7
|
+
//# sourceMappingURL=identityStorage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identityStorage.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/identityStorage.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,gBAAgB,4BAA4B,CAAC;AAC1D,eAAO,MAAM,WAAW,uBAAuB,CAAC;AAChD,eAAO,MAAM,YAAY,wBAAwB,CAAC;AAElD,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAM1E;AAED,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAMhF;AAED,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAMpE"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
2
|
+
export const ANONYMOUS_ID_KEY = 'metarouter:anonymous_id';
|
|
3
|
+
export const USER_ID_KEY = 'metarouter:user_id';
|
|
4
|
+
export const GROUP_ID_KEY = 'metarouter:group_id';
|
|
5
|
+
export async function getIdentityField(key) {
|
|
6
|
+
try {
|
|
7
|
+
return await AsyncStorage.getItem(key);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export async function setIdentityField(key, value) {
|
|
14
|
+
try {
|
|
15
|
+
await AsyncStorage.setItem(key, value);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// Optionally log error
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export async function removeIdentityField(key) {
|
|
22
|
+
try {
|
|
23
|
+
await AsyncStorage.removeItem(key);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
// Optionally log error
|
|
27
|
+
}
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retrieves or generates a persistent anonymous ID for the user/device.
|
|
3
|
-
* - Attempts to load from AsyncStorage.
|
|
4
|
-
* - If not found or storage fails, generates a new UUIDv4.
|
|
5
|
-
* - Always returns a valid string.
|
|
6
|
-
*/
|
|
7
|
-
export declare function getAnonymousId(): Promise<string>;
|
|
8
|
-
//# sourceMappingURL=anonymousId.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"anonymousId.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/anonymousId.ts"],"names":[],"mappings":"AAMA;;;;;GAKG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAsBtD"}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
2
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
-
import { log, warn } from './logger';
|
|
4
|
-
const STORAGE_KEY = 'metarouter:anonymous_id';
|
|
5
|
-
/**
|
|
6
|
-
* Retrieves or generates a persistent anonymous ID for the user/device.
|
|
7
|
-
* - Attempts to load from AsyncStorage.
|
|
8
|
-
* - If not found or storage fails, generates a new UUIDv4.
|
|
9
|
-
* - Always returns a valid string.
|
|
10
|
-
*/
|
|
11
|
-
export async function getAnonymousId() {
|
|
12
|
-
try {
|
|
13
|
-
const existing = await AsyncStorage.getItem(STORAGE_KEY);
|
|
14
|
-
if (existing) {
|
|
15
|
-
log('Retrieved existing anonymous ID from storage');
|
|
16
|
-
return existing;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
catch (error) {
|
|
20
|
-
warn('Failed to read anonymous ID from storage, generating new one', error);
|
|
21
|
-
}
|
|
22
|
-
const anonId = uuidv4();
|
|
23
|
-
log('Generated new anonymous ID:', anonId);
|
|
24
|
-
try {
|
|
25
|
-
await AsyncStorage.setItem(STORAGE_KEY, anonId);
|
|
26
|
-
log('Stored anonymous ID in storage');
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
warn('Failed to store anonymous ID in storage, but continuing with generated ID', error);
|
|
30
|
-
}
|
|
31
|
-
return anonId;
|
|
32
|
-
}
|