@metarouter/react-native-sdk 0.1.23 → 0.1.25
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/MetaRouter.d.ts +11 -0
- package/lib/MetaRouter.d.ts.map +1 -1
- package/lib/MetaRouter.js +11 -0
- package/lib/analytics/IdentityManager.d.ts +40 -0
- package/lib/analytics/IdentityManager.d.ts.map +1 -1
- package/lib/analytics/IdentityManager.js +40 -0
- package/lib/analytics/MetaRouterAnalyticsClient.d.ts +77 -5
- package/lib/analytics/MetaRouterAnalyticsClient.d.ts.map +1 -1
- package/lib/analytics/MetaRouterAnalyticsClient.js +136 -63
- package/lib/analytics/init.d.ts +17 -0
- package/lib/analytics/init.d.ts.map +1 -1
- package/lib/analytics/init.js +17 -0
- package/lib/analytics/proxy/proxyClient.d.ts +13 -0
- package/lib/analytics/proxy/proxyClient.d.ts.map +1 -1
- package/lib/analytics/proxy/proxyClient.js +13 -0
- package/lib/analytics/types.d.ts +2 -2
- package/lib/analytics/types.d.ts.map +1 -1
- package/lib/analytics/utils/anonymousId.d.ts +6 -0
- package/lib/analytics/utils/anonymousId.d.ts.map +1 -1
- package/lib/analytics/utils/anonymousId.js +6 -0
- package/lib/analytics/utils/contextInfo.d.ts +12 -1
- package/lib/analytics/utils/contextInfo.d.ts.map +1 -1
- package/lib/analytics/utils/contextInfo.js +10 -0
- package/lib/analytics/utils/enrichEvent.d.ts +15 -0
- package/lib/analytics/utils/enrichEvent.d.ts.map +1 -0
- package/lib/analytics/utils/enrichEvent.js +22 -0
- package/lib/analytics/utils/logger.d.ts +23 -0
- package/lib/analytics/utils/logger.d.ts.map +1 -1
- package/lib/analytics/utils/logger.js +23 -2
- package/lib/analytics/utils/retry.d.ts +14 -0
- package/lib/analytics/utils/retry.d.ts.map +1 -1
- package/lib/analytics/utils/retry.js +14 -0
- package/lib/analytics/utils/timezone.d.ts +5 -0
- package/lib/analytics/utils/timezone.d.ts.map +1 -1
- package/lib/analytics/utils/timezone.js +5 -0
- package/lib/react/context.d.ts +9 -0
- package/lib/react/context.d.ts.map +1 -1
- package/lib/react/context.js +9 -0
- package/lib/react/useMetaRouter.d.ts +4 -0
- package/lib/react/useMetaRouter.d.ts.map +1 -1
- package/lib/react/useMetaRouter.js +4 -0
- package/package.json +2 -2
- package/lib/analytics/enrichEvent.d.ts +0 -3
- package/lib/analytics/enrichEvent.d.ts.map +0 -1
- package/lib/analytics/enrichEvent.js +0 -10
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
|
|
package/lib/MetaRouter.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import type { MetaRouterInterface } from './types';
|
|
2
2
|
import 'react-native-get-random-values';
|
|
3
|
+
/**
|
|
4
|
+
* Top-level MetaRouter SDK interface.
|
|
5
|
+
*
|
|
6
|
+
* Provides access to analytics methods for initialization, client retrieval, and reset.
|
|
7
|
+
* Designed for use in React Native applications.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import MetaRouter from '@metarouter/react-native-sdk';
|
|
11
|
+
* await MetaRouter.analytics.init({ ... });
|
|
12
|
+
* MetaRouter.analytics.track(...);
|
|
13
|
+
*/
|
|
3
14
|
export declare const MetaRouter: MetaRouterInterface;
|
|
4
15
|
//# sourceMappingURL=MetaRouter.d.ts.map
|
package/lib/MetaRouter.d.ts.map
CHANGED
|
@@ -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;AACnD,OAAO,gCAAgC,CAAC;AAExC,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;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,EAAE,mBAMxB,CAAC"}
|
package/lib/MetaRouter.js
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
import { initAnalytics, getAnalyticsClient, resetAnalytics } from './analytics/init';
|
|
2
2
|
import 'react-native-get-random-values';
|
|
3
|
+
/**
|
|
4
|
+
* Top-level MetaRouter SDK interface.
|
|
5
|
+
*
|
|
6
|
+
* Provides access to analytics methods for initialization, client retrieval, and reset.
|
|
7
|
+
* Designed for use in React Native applications.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import MetaRouter from '@metarouter/react-native-sdk';
|
|
11
|
+
* await MetaRouter.analytics.init({ ... });
|
|
12
|
+
* MetaRouter.analytics.track(...);
|
|
13
|
+
*/
|
|
3
14
|
export const MetaRouter = {
|
|
4
15
|
analytics: {
|
|
5
16
|
init: (opts) => initAnalytics(opts),
|
|
@@ -1,15 +1,55 @@
|
|
|
1
1
|
import { EventPayload, EventWithIdentity } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Manages user, group, and anonymous identity for analytics events.
|
|
4
|
+
* - Loads or generates a persistent anonymous ID.
|
|
5
|
+
* - Tracks current user and group IDs.
|
|
6
|
+
* - Can enrich events with identity information.
|
|
7
|
+
* - Supports reset for logout/testing scenarios.
|
|
8
|
+
*/
|
|
2
9
|
export declare class IdentityManager {
|
|
3
10
|
private anonymousId;
|
|
4
11
|
private userId?;
|
|
5
12
|
private groupId?;
|
|
13
|
+
/**
|
|
14
|
+
* Initializes the manager by loading or generating an anonymous ID.
|
|
15
|
+
* Should be called before using other methods.
|
|
16
|
+
*/
|
|
6
17
|
init(): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves the current anonymous ID.
|
|
20
|
+
* @returns The anonymous ID or null if not initialized.
|
|
21
|
+
*/
|
|
7
22
|
getAnonymousId(): string | null;
|
|
23
|
+
/**
|
|
24
|
+
* Sets the user ID for the current session.
|
|
25
|
+
* @param userId - The user ID to set.
|
|
26
|
+
*/
|
|
8
27
|
identify(userId: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Sets the group ID for the current session.
|
|
30
|
+
* @param groupId - The group ID to set.
|
|
31
|
+
*/
|
|
9
32
|
group(groupId: string): void;
|
|
33
|
+
/**
|
|
34
|
+
* Retrieves the current user ID.
|
|
35
|
+
* @returns The user ID or undefined if not set.
|
|
36
|
+
*/
|
|
10
37
|
getUserId(): string | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves the current group ID.
|
|
40
|
+
* @returns The group ID or undefined if not set.
|
|
41
|
+
*/
|
|
11
42
|
getGroupId(): string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Adds identity information to an event payload.
|
|
45
|
+
* @param event - The event payload to enrich.
|
|
46
|
+
* @returns The enriched event payload with identity information.
|
|
47
|
+
*/
|
|
12
48
|
addIdentityInfo<T extends EventPayload>(event: T): EventWithIdentity;
|
|
49
|
+
/**
|
|
50
|
+
* Resets the identity manager to its initial state.
|
|
51
|
+
* Clears all user and group IDs, and sets the anonymous ID to null.
|
|
52
|
+
*/
|
|
13
53
|
reset(): void;
|
|
14
54
|
}
|
|
15
55
|
//# sourceMappingURL=IdentityManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdentityManager.d.ts","sourceRoot":"","sources":["../../src/analytics/IdentityManager.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE1D,qBAAa,eAAe;IACxB,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,OAAO,CAAC,CAAS;
|
|
1
|
+
{"version":3,"file":"IdentityManager.d.ts","sourceRoot":"","sources":["../../src/analytics/IdentityManager.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE1D;;;;;;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;IAY3B;;;OAGG;IACH,cAAc,IAAI,MAAM,GAAG,IAAI;IAI/B;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM;IAKvB;;;OAGG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM;IAKrB;;;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;IACH,KAAK;CAMN"}
|
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { getAnonymousId } from "./utils/anonymousId";
|
|
2
2
|
import { log, warn } from "./utils/logger";
|
|
3
|
+
/**
|
|
4
|
+
* Manages user, group, and anonymous identity for analytics events.
|
|
5
|
+
* - Loads or generates a persistent anonymous ID.
|
|
6
|
+
* - Tracks current user and group IDs.
|
|
7
|
+
* - Can enrich events with identity information.
|
|
8
|
+
* - Supports reset for logout/testing scenarios.
|
|
9
|
+
*/
|
|
3
10
|
export class IdentityManager {
|
|
4
11
|
constructor() {
|
|
5
12
|
this.anonymousId = null;
|
|
6
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Initializes the manager by loading or generating an anonymous ID.
|
|
16
|
+
* Should be called before using other methods.
|
|
17
|
+
*/
|
|
7
18
|
async init() {
|
|
8
19
|
try {
|
|
9
20
|
this.anonymousId = await getAnonymousId(); // load or generate
|
|
@@ -16,23 +27,48 @@ export class IdentityManager {
|
|
|
16
27
|
log('Using fallback anonymous ID:', this.anonymousId);
|
|
17
28
|
}
|
|
18
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Retrieves the current anonymous ID.
|
|
32
|
+
* @returns The anonymous ID or null if not initialized.
|
|
33
|
+
*/
|
|
19
34
|
getAnonymousId() {
|
|
20
35
|
return this.anonymousId;
|
|
21
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Sets the user ID for the current session.
|
|
39
|
+
* @param userId - The user ID to set.
|
|
40
|
+
*/
|
|
22
41
|
identify(userId) {
|
|
23
42
|
this.userId = userId;
|
|
24
43
|
log('User identified:', userId);
|
|
25
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Sets the group ID for the current session.
|
|
47
|
+
* @param groupId - The group ID to set.
|
|
48
|
+
*/
|
|
26
49
|
group(groupId) {
|
|
27
50
|
this.groupId = groupId;
|
|
28
51
|
log('User grouped:', groupId);
|
|
29
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves the current user ID.
|
|
55
|
+
* @returns The user ID or undefined if not set.
|
|
56
|
+
*/
|
|
30
57
|
getUserId() {
|
|
31
58
|
return this.userId;
|
|
32
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves the current group ID.
|
|
62
|
+
* @returns The group ID or undefined if not set.
|
|
63
|
+
*/
|
|
33
64
|
getGroupId() {
|
|
34
65
|
return this.groupId;
|
|
35
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Adds identity information to an event payload.
|
|
69
|
+
* @param event - The event payload to enrich.
|
|
70
|
+
* @returns The enriched event payload with identity information.
|
|
71
|
+
*/
|
|
36
72
|
addIdentityInfo(event) {
|
|
37
73
|
var _a, _b, _c;
|
|
38
74
|
return {
|
|
@@ -42,6 +78,10 @@ export class IdentityManager {
|
|
|
42
78
|
groupId: (_c = event.groupId) !== null && _c !== void 0 ? _c : this.groupId,
|
|
43
79
|
};
|
|
44
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Resets the identity manager to its initial state.
|
|
83
|
+
* Clears all user and group IDs, and sets the anonymous ID to null.
|
|
84
|
+
*/
|
|
45
85
|
reset() {
|
|
46
86
|
this.anonymousId = null;
|
|
47
87
|
this.userId = undefined;
|
|
@@ -1,33 +1,96 @@
|
|
|
1
1
|
import { InitOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Analytics client for MetaRouter.
|
|
4
|
+
* - Handles event queueing, batching, and delivery with retries.
|
|
5
|
+
* - Manages user, group, and anonymous identity.
|
|
6
|
+
* - Supports periodic and on-demand flushing.
|
|
7
|
+
* - Provides debug and cleanup utilities.
|
|
8
|
+
*/
|
|
2
9
|
export declare class MetaRouterAnalyticsClient {
|
|
3
10
|
private initialized;
|
|
4
11
|
private initPromise;
|
|
5
12
|
private queue;
|
|
6
13
|
private flushIntervalMs;
|
|
7
14
|
private flushTimer;
|
|
8
|
-
private
|
|
15
|
+
private ingestionHost;
|
|
9
16
|
private writeKey;
|
|
10
17
|
private context;
|
|
11
18
|
private appState;
|
|
12
19
|
private appStateSubscription;
|
|
13
20
|
private identityManager;
|
|
21
|
+
/**
|
|
22
|
+
* Initializes the analytics client with the provided options.
|
|
23
|
+
* @param options - The initialization options.
|
|
24
|
+
*/
|
|
14
25
|
constructor(options: InitOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Initializes the analytics client.
|
|
28
|
+
* @returns A promise that resolves when the client is initialized.
|
|
29
|
+
*/
|
|
15
30
|
private init;
|
|
31
|
+
/**
|
|
32
|
+
* Waits for the analytics client to be initialized.
|
|
33
|
+
* @returns A promise that resolves when the client is initialized.
|
|
34
|
+
*/
|
|
16
35
|
waitForInitialization(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Starts the flush loop.
|
|
38
|
+
*/
|
|
17
39
|
private startFlushLoop;
|
|
40
|
+
/**
|
|
41
|
+
* Returns the current timestamp in ISO format.
|
|
42
|
+
* @returns The current timestamp in ISO format.
|
|
43
|
+
*/
|
|
18
44
|
private now;
|
|
45
|
+
/**
|
|
46
|
+
* Enqueues an event for processing.
|
|
47
|
+
* @param event - The event to enqueue.
|
|
48
|
+
*/
|
|
19
49
|
private enqueue;
|
|
50
|
+
/**
|
|
51
|
+
* Sets up the app state listener.
|
|
52
|
+
*/
|
|
20
53
|
private setupAppStateListener;
|
|
54
|
+
/**
|
|
55
|
+
* Handles the app state change event.
|
|
56
|
+
* @param nextState - The new app state.
|
|
57
|
+
*/
|
|
21
58
|
private handleAppStateChange;
|
|
59
|
+
/**
|
|
60
|
+
* Tracks an event.
|
|
61
|
+
* @param event - The event to track.
|
|
62
|
+
* @param properties - The properties to track.
|
|
63
|
+
*/
|
|
22
64
|
track(event: string, properties?: Record<string, any>): void;
|
|
65
|
+
/**
|
|
66
|
+
* Identifies a user.
|
|
67
|
+
* @param userId - The user ID to identify.
|
|
68
|
+
* @param traits - The traits to identify the user with.
|
|
69
|
+
*/
|
|
23
70
|
identify(userId: string, traits?: Record<string, any>): void;
|
|
71
|
+
/**
|
|
72
|
+
* Tracks a page view.
|
|
73
|
+
* @param name - The name of the page.
|
|
74
|
+
* @param properties - The properties to track.
|
|
75
|
+
*/
|
|
24
76
|
page(name: string, properties?: Record<string, any>): void;
|
|
77
|
+
/**
|
|
78
|
+
* Groups a user.
|
|
79
|
+
* @param groupId - The group ID to group.
|
|
80
|
+
* @param traits - The traits to group the user with.
|
|
81
|
+
*/
|
|
25
82
|
group(groupId: string, traits?: Record<string, any>): void;
|
|
83
|
+
/**
|
|
84
|
+
* Tracks a screen view.
|
|
85
|
+
* @param name - The name of the screen.
|
|
86
|
+
* @param properties - The properties to track.
|
|
87
|
+
*/
|
|
26
88
|
screen(name: string, properties?: Record<string, any>): void;
|
|
27
89
|
/**
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
90
|
+
* Alias an anonymous user to a known user ID.
|
|
91
|
+
* This updates internal identity state and enqueues an alias event.
|
|
92
|
+
* @param newUserId - The new user ID to alias to.
|
|
93
|
+
*/
|
|
31
94
|
alias(newUserId: string): void;
|
|
32
95
|
/**
|
|
33
96
|
* Enable debug logging for troubleshooting
|
|
@@ -39,13 +102,16 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
39
102
|
getDebugInfo(): {
|
|
40
103
|
initialized: boolean;
|
|
41
104
|
queueLength: number;
|
|
42
|
-
|
|
105
|
+
ingestionHost: string;
|
|
43
106
|
writeKey: string | undefined;
|
|
44
107
|
flushIntervalMs: number;
|
|
45
108
|
anonymousId: string | null;
|
|
46
109
|
userId: string | undefined;
|
|
47
110
|
groupId: string | undefined;
|
|
48
111
|
};
|
|
112
|
+
/**
|
|
113
|
+
* Flushes the event queue to the ingestion endpoint.
|
|
114
|
+
*/
|
|
49
115
|
flush(): Promise<{
|
|
50
116
|
sentAt: string;
|
|
51
117
|
type: import("./types").EventType;
|
|
@@ -56,7 +122,13 @@ export declare class MetaRouterAnalyticsClient {
|
|
|
56
122
|
properties?: Record<string, any>;
|
|
57
123
|
timestamp?: string;
|
|
58
124
|
}[] | undefined>;
|
|
125
|
+
/**
|
|
126
|
+
* Resets the analytics client.
|
|
127
|
+
*/
|
|
59
128
|
reset(): void;
|
|
129
|
+
/**
|
|
130
|
+
* Cleans up the analytics client.
|
|
131
|
+
*/
|
|
60
132
|
cleanup(): void;
|
|
61
133
|
}
|
|
62
134
|
//# sourceMappingURL=MetaRouterAnalyticsClient.d.ts.map
|
|
@@ -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;IAEzC;;;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;IAWf;;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,10 +1,21 @@
|
|
|
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";
|
|
5
|
-
import { enrichEvent } from "./enrichEvent";
|
|
5
|
+
import { enrichEvent } from "./utils/enrichEvent";
|
|
6
6
|
import { getContextInfo } from "./utils/contextInfo";
|
|
7
|
+
/**
|
|
8
|
+
* Analytics client for MetaRouter.
|
|
9
|
+
* - Handles event queueing, batching, and delivery with retries.
|
|
10
|
+
* - Manages user, group, and anonymous identity.
|
|
11
|
+
* - Supports periodic and on-demand flushing.
|
|
12
|
+
* - Provides debug and cleanup utilities.
|
|
13
|
+
*/
|
|
7
14
|
export class MetaRouterAnalyticsClient {
|
|
15
|
+
/**
|
|
16
|
+
* Initializes the analytics client with the provided options.
|
|
17
|
+
* @param options - The initialization options.
|
|
18
|
+
*/
|
|
8
19
|
constructor(options) {
|
|
9
20
|
var _a;
|
|
10
21
|
this.initialized = false;
|
|
@@ -13,107 +24,169 @@ export class MetaRouterAnalyticsClient {
|
|
|
13
24
|
this.flushTimer = null;
|
|
14
25
|
this.appState = AppState.currentState;
|
|
15
26
|
this.appStateSubscription = null;
|
|
27
|
+
/**
|
|
28
|
+
* Handles the app state change event.
|
|
29
|
+
* @param nextState - The new app state.
|
|
30
|
+
*/
|
|
16
31
|
this.handleAppStateChange = (nextState) => {
|
|
17
|
-
if (this.appState ===
|
|
32
|
+
if (this.appState === "active" && nextState.match(/inactive|background/)) {
|
|
18
33
|
this.flush();
|
|
19
34
|
}
|
|
20
35
|
this.appState = nextState;
|
|
21
36
|
};
|
|
22
|
-
log(
|
|
23
|
-
const { writeKey,
|
|
24
|
-
this.
|
|
37
|
+
log("Initializing analytics client", options);
|
|
38
|
+
const { writeKey, ingestionHost, flushInterval } = options;
|
|
39
|
+
this.ingestionHost = ingestionHost;
|
|
25
40
|
this.writeKey = writeKey;
|
|
26
41
|
this.flushIntervalMs = flushInterval !== null && flushInterval !== void 0 ? flushInterval : 10000;
|
|
27
42
|
setDebugLogging((_a = options.debug) !== null && _a !== void 0 ? _a : false);
|
|
28
43
|
this.identityManager = new IdentityManager();
|
|
29
44
|
this.initPromise = this.init();
|
|
30
|
-
log(
|
|
45
|
+
log("Analytics client constructor completed, initialization in progress...");
|
|
31
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Initializes the analytics client.
|
|
49
|
+
* @returns A promise that resolves when the client is initialized.
|
|
50
|
+
*/
|
|
32
51
|
async init() {
|
|
33
52
|
if (this.initialized) {
|
|
34
|
-
log(
|
|
53
|
+
log("Analytics client already initialized");
|
|
35
54
|
return;
|
|
36
55
|
}
|
|
37
|
-
log(
|
|
56
|
+
log("Starting analytics client initialization...");
|
|
38
57
|
try {
|
|
39
58
|
await this.identityManager.init();
|
|
40
|
-
log(
|
|
59
|
+
log("IdentityManager initialized successfully");
|
|
41
60
|
this.startFlushLoop();
|
|
42
|
-
log(
|
|
61
|
+
log("Flush loop started with interval:", this.flushIntervalMs, "ms");
|
|
43
62
|
this.setupAppStateListener();
|
|
44
|
-
log(
|
|
63
|
+
log("App state listener setup completed");
|
|
45
64
|
this.context = await getContextInfo();
|
|
46
65
|
this.initialized = true;
|
|
47
|
-
log(
|
|
66
|
+
log("Analytics client initialization completed successfully");
|
|
48
67
|
}
|
|
49
68
|
catch (error) {
|
|
50
|
-
warn(
|
|
69
|
+
warn("Analytics client initialization failed:", error);
|
|
51
70
|
// Still mark as initialized to prevent infinite retries
|
|
52
71
|
this.initialized = true;
|
|
53
72
|
throw error;
|
|
54
73
|
}
|
|
55
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Waits for the analytics client to be initialized.
|
|
77
|
+
* @returns A promise that resolves when the client is initialized.
|
|
78
|
+
*/
|
|
56
79
|
async waitForInitialization() {
|
|
57
80
|
await this.initPromise;
|
|
58
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Starts the flush loop.
|
|
84
|
+
*/
|
|
59
85
|
startFlushLoop() {
|
|
60
86
|
this.flushTimer = setInterval(() => this.flush(), this.flushIntervalMs);
|
|
61
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns the current timestamp in ISO format.
|
|
90
|
+
* @returns The current timestamp in ISO format.
|
|
91
|
+
*/
|
|
62
92
|
now() {
|
|
63
93
|
return new Date().toISOString();
|
|
64
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Enqueues an event for processing.
|
|
97
|
+
* @param event - The event to enqueue.
|
|
98
|
+
*/
|
|
65
99
|
enqueue(event) {
|
|
66
100
|
const eventWithIdentity = this.identityManager.addIdentityInfo(event);
|
|
67
101
|
const enrichedEvent = enrichEvent(eventWithIdentity, this.writeKey, this.context);
|
|
68
|
-
log(
|
|
102
|
+
log("Enqueuing event", enrichedEvent);
|
|
69
103
|
this.queue.push(enrichedEvent);
|
|
70
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Sets up the app state listener.
|
|
107
|
+
*/
|
|
71
108
|
setupAppStateListener() {
|
|
72
|
-
this.appStateSubscription = AppState.addEventListener(
|
|
109
|
+
this.appStateSubscription = AppState.addEventListener("change", this.handleAppStateChange);
|
|
73
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Tracks an event.
|
|
113
|
+
* @param event - The event to track.
|
|
114
|
+
* @param properties - The properties to track.
|
|
115
|
+
*/
|
|
74
116
|
track(event, properties) {
|
|
75
|
-
log(
|
|
76
|
-
this.enqueue({ type:
|
|
77
|
-
log(
|
|
117
|
+
log("Tracking event:", event, "with properties:", properties);
|
|
118
|
+
this.enqueue({ type: "track", event, properties, timestamp: this.now() });
|
|
119
|
+
log("Event enqueued, queue length:", this.queue.length);
|
|
78
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Identifies a user.
|
|
123
|
+
* @param userId - The user ID to identify.
|
|
124
|
+
* @param traits - The traits to identify the user with.
|
|
125
|
+
*/
|
|
79
126
|
identify(userId, traits) {
|
|
80
|
-
log(
|
|
127
|
+
log("Identifying user:", userId, "with traits:", traits);
|
|
81
128
|
this.identityManager.identify(userId);
|
|
82
|
-
this.enqueue({ type:
|
|
83
|
-
log(
|
|
129
|
+
this.enqueue({ type: "identify", userId, traits, timestamp: this.now() });
|
|
130
|
+
log("Identify event enqueued, queue length:", this.queue.length);
|
|
84
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Tracks a page view.
|
|
134
|
+
* @param name - The name of the page.
|
|
135
|
+
* @param properties - The properties to track.
|
|
136
|
+
*/
|
|
85
137
|
page(name, properties) {
|
|
86
|
-
log(
|
|
87
|
-
this.enqueue({
|
|
88
|
-
|
|
138
|
+
log("Tracking page:", name, "with properties:", properties);
|
|
139
|
+
this.enqueue({
|
|
140
|
+
type: "page",
|
|
141
|
+
event: name,
|
|
142
|
+
properties,
|
|
143
|
+
timestamp: this.now(),
|
|
144
|
+
});
|
|
145
|
+
log("Page event enqueued, queue length:", this.queue.length);
|
|
89
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Groups a user.
|
|
149
|
+
* @param groupId - The group ID to group.
|
|
150
|
+
* @param traits - The traits to group the user with.
|
|
151
|
+
*/
|
|
90
152
|
group(groupId, traits) {
|
|
91
|
-
log(
|
|
153
|
+
log("Grouping user:", groupId, "with traits:", traits);
|
|
92
154
|
this.identityManager.group(groupId);
|
|
93
|
-
this.enqueue({ type:
|
|
94
|
-
log(
|
|
155
|
+
this.enqueue({ type: "group", groupId, traits, timestamp: this.now() });
|
|
156
|
+
log("Group event enqueued, queue length:", this.queue.length);
|
|
95
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Tracks a screen view.
|
|
160
|
+
* @param name - The name of the screen.
|
|
161
|
+
* @param properties - The properties to track.
|
|
162
|
+
*/
|
|
96
163
|
screen(name, properties) {
|
|
97
|
-
log(
|
|
98
|
-
this.enqueue({
|
|
99
|
-
|
|
164
|
+
log("Tracking screen:", name, "with properties:", properties);
|
|
165
|
+
this.enqueue({
|
|
166
|
+
type: "screen",
|
|
167
|
+
event: name,
|
|
168
|
+
properties,
|
|
169
|
+
timestamp: this.now(),
|
|
170
|
+
});
|
|
171
|
+
log("Screen event enqueued, queue length:", this.queue.length);
|
|
100
172
|
}
|
|
101
173
|
/**
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
174
|
+
* Alias an anonymous user to a known user ID.
|
|
175
|
+
* This updates internal identity state and enqueues an alias event.
|
|
176
|
+
* @param newUserId - The new user ID to alias to.
|
|
177
|
+
*/
|
|
105
178
|
alias(newUserId) {
|
|
106
|
-
log(
|
|
179
|
+
log("Aliasing user to:", newUserId);
|
|
107
180
|
this.identityManager.identify(newUserId);
|
|
108
|
-
this.enqueue({ type:
|
|
109
|
-
log(
|
|
181
|
+
this.enqueue({ type: "alias", userId: newUserId, timestamp: this.now() });
|
|
182
|
+
log("Alias event enqueued, queue length:", this.queue.length);
|
|
110
183
|
}
|
|
111
184
|
/**
|
|
112
185
|
* Enable debug logging for troubleshooting
|
|
113
186
|
*/
|
|
114
187
|
enableDebugLogging() {
|
|
115
188
|
setDebugLogging(true);
|
|
116
|
-
log(
|
|
189
|
+
log("Debug logging enabled");
|
|
117
190
|
}
|
|
118
191
|
/**
|
|
119
192
|
* Get current state for debugging
|
|
@@ -122,23 +195,26 @@ export class MetaRouterAnalyticsClient {
|
|
|
122
195
|
return {
|
|
123
196
|
initialized: this.initialized,
|
|
124
197
|
queueLength: this.queue.length,
|
|
125
|
-
|
|
126
|
-
writeKey: this.writeKey ?
|
|
198
|
+
ingestionHost: this.ingestionHost,
|
|
199
|
+
writeKey: this.writeKey ? "***" + this.writeKey.slice(-4) : undefined,
|
|
127
200
|
flushIntervalMs: this.flushIntervalMs,
|
|
128
201
|
anonymousId: this.identityManager.getAnonymousId(),
|
|
129
202
|
userId: this.identityManager.getUserId(),
|
|
130
203
|
groupId: this.identityManager.getGroupId(),
|
|
131
204
|
};
|
|
132
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Flushes the event queue to the ingestion endpoint.
|
|
208
|
+
*/
|
|
133
209
|
async flush() {
|
|
134
210
|
const anonId = this.identityManager.getAnonymousId();
|
|
135
211
|
if (!anonId) {
|
|
136
|
-
warn(
|
|
212
|
+
warn("Anonymous ID not yet ready, delaying flush");
|
|
137
213
|
// Anon ID not yet ready, delay flushing
|
|
138
214
|
return;
|
|
139
215
|
}
|
|
140
216
|
if (this.queue.length === 0) {
|
|
141
|
-
log(
|
|
217
|
+
log("No events to flush");
|
|
142
218
|
return;
|
|
143
219
|
}
|
|
144
220
|
const batch = this.queue.map((event) => ({
|
|
@@ -146,49 +222,46 @@ export class MetaRouterAnalyticsClient {
|
|
|
146
222
|
sentAt: new Date().toISOString(),
|
|
147
223
|
}));
|
|
148
224
|
this.queue = [];
|
|
149
|
-
log(`Flushing ${batch.length} events to ${this.
|
|
225
|
+
log(`Flushing ${batch.length} events to ${this.ingestionHost}/v1/batch`);
|
|
150
226
|
try {
|
|
151
227
|
await retryWithBackoff(async () => {
|
|
152
|
-
log(
|
|
153
|
-
const response = await fetch(this.
|
|
154
|
-
method:
|
|
228
|
+
log("Making API call to:", this.ingestionHost + "/v1/batch");
|
|
229
|
+
const response = await fetch(this.ingestionHost + "/v1/batch", {
|
|
230
|
+
method: "POST",
|
|
155
231
|
headers: {
|
|
156
|
-
|
|
232
|
+
"Content-Type": "application/json",
|
|
157
233
|
},
|
|
158
234
|
body: JSON.stringify({ batch }),
|
|
159
235
|
});
|
|
160
236
|
if (!response.ok) {
|
|
161
|
-
error(
|
|
237
|
+
error("HTTP error", response);
|
|
162
238
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
163
239
|
}
|
|
164
|
-
log(
|
|
240
|
+
log("API call successful, status:", response.status);
|
|
165
241
|
return response;
|
|
166
242
|
});
|
|
167
|
-
log(
|
|
243
|
+
log("Flush completed successfully");
|
|
168
244
|
return batch;
|
|
169
245
|
}
|
|
170
246
|
catch (err) {
|
|
171
|
-
warn(
|
|
172
|
-
|
|
173
|
-
this.queue.unshift(...this.queue.splice(0, 0, ...batch.map(event => ({
|
|
174
|
-
type: event.type,
|
|
175
|
-
event: event.event,
|
|
176
|
-
properties: event.properties,
|
|
177
|
-
userId: event.userId,
|
|
178
|
-
traits: event.traits,
|
|
179
|
-
groupId: event.groupId,
|
|
180
|
-
timestamp: event.timestamp,
|
|
181
|
-
}))));
|
|
247
|
+
warn("Flush failed, re-queueing events", err);
|
|
248
|
+
this.queue.unshift(...batch);
|
|
182
249
|
}
|
|
183
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Resets the analytics client.
|
|
253
|
+
*/
|
|
184
254
|
reset() {
|
|
185
255
|
this.identityManager.reset();
|
|
186
256
|
this.queue = [];
|
|
187
|
-
log(
|
|
257
|
+
log("Analytics client reset");
|
|
188
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Cleans up the analytics client.
|
|
261
|
+
*/
|
|
189
262
|
cleanup() {
|
|
190
263
|
var _a, _b;
|
|
191
|
-
log(
|
|
264
|
+
log("Cleaning up analytics client");
|
|
192
265
|
if (this.flushTimer)
|
|
193
266
|
clearInterval(this.flushTimer);
|
|
194
267
|
this.queue = [];
|
package/lib/analytics/init.d.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
import type { InitOptions, AnalyticsInterface } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Initializes the analytics client singleton.
|
|
4
|
+
* Waits for async setup (e.g., identity loading) before returning the interface.
|
|
5
|
+
* Returns the same instance on subsequent calls.
|
|
6
|
+
* @param options Analytics initialization options.
|
|
7
|
+
* @returns The analytics interface.
|
|
8
|
+
*/
|
|
2
9
|
export declare function initAnalytics(options: InitOptions): Promise<AnalyticsInterface>;
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves the analytics client singleton.
|
|
12
|
+
* Returns the proxy client before initialization, and the real client after.
|
|
13
|
+
* @returns The analytics interface.
|
|
14
|
+
*/
|
|
3
15
|
export declare function getAnalyticsClient(): AnalyticsInterface;
|
|
16
|
+
/**
|
|
17
|
+
* Resets the analytics client singleton.
|
|
18
|
+
* Cleans up the client and sets it to null.
|
|
19
|
+
* @returns A promise that resolves when the client is reset.
|
|
20
|
+
*/
|
|
4
21
|
export declare function resetAnalytics(): Promise<void>;
|
|
5
22
|
//# sourceMappingURL=init.d.ts.map
|
|
@@ -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,CAuBrF;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;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAuBrF;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,kBAAkB,CAEvD;AAED;;;;GAIG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAMpD"}
|
package/lib/analytics/init.js
CHANGED
|
@@ -3,6 +3,13 @@ import { proxyClient, setRealClient } from './proxy/proxyClient';
|
|
|
3
3
|
let initialized = false;
|
|
4
4
|
let client = null;
|
|
5
5
|
let analyticsInterface = null;
|
|
6
|
+
/**
|
|
7
|
+
* Initializes the analytics client singleton.
|
|
8
|
+
* Waits for async setup (e.g., identity loading) before returning the interface.
|
|
9
|
+
* Returns the same instance on subsequent calls.
|
|
10
|
+
* @param options Analytics initialization options.
|
|
11
|
+
* @returns The analytics interface.
|
|
12
|
+
*/
|
|
6
13
|
export async function initAnalytics(options) {
|
|
7
14
|
if (initialized)
|
|
8
15
|
return analyticsInterface;
|
|
@@ -24,9 +31,19 @@ export async function initAnalytics(options) {
|
|
|
24
31
|
setRealClient(analyticsInterface);
|
|
25
32
|
return analyticsInterface;
|
|
26
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Retrieves the analytics client singleton.
|
|
36
|
+
* Returns the proxy client before initialization, and the real client after.
|
|
37
|
+
* @returns The analytics interface.
|
|
38
|
+
*/
|
|
27
39
|
export function getAnalyticsClient() {
|
|
28
40
|
return initialized ? analyticsInterface : proxyClient;
|
|
29
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Resets the analytics client singleton.
|
|
44
|
+
* Cleans up the client and sets it to null.
|
|
45
|
+
* @returns A promise that resolves when the client is reset.
|
|
46
|
+
*/
|
|
30
47
|
export async function resetAnalytics() {
|
|
31
48
|
await (client === null || client === void 0 ? void 0 : client.cleanup());
|
|
32
49
|
client = null;
|
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import type { AnalyticsInterface } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* proxyClient implements a proxy pattern for the AnalyticsInterface.
|
|
4
|
+
*
|
|
5
|
+
* It allows analytics method calls (track, identify, etc.) to be made before the real analytics client
|
|
6
|
+
* is fully initialized. Calls are queued and replayed once the real client is set via setRealClient.
|
|
7
|
+
* This ensures that analytics events are not lost if called early in the app lifecycle.
|
|
8
|
+
*
|
|
9
|
+
* The proxy also provides a fallback for getDebugInfo, returning proxy state if the real client is not ready.
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* - Use proxyClient as the default analytics client before initialization.
|
|
13
|
+
* - Call setRealClient(realClientInstance) once the real client is ready.
|
|
14
|
+
*/
|
|
2
15
|
export declare const proxyClient: AnalyticsInterface;
|
|
3
16
|
export declare function setRealClient(client: AnalyticsInterface | null): void;
|
|
4
17
|
//# sourceMappingURL=proxyClient.d.ts.map
|
|
@@ -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;AAwBnD,eAAO,MAAM,WAAW,EAAE,kBAWzB,CAAC;AAEF,wBAAgB,aAAa,CAAC,MAAM,EAAE,kBAAkB,GAAG,IAAI,QAO9D"}
|
|
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;AAwBnD;;;;;;;;;;;;GAYG;AAEH,eAAO,MAAM,WAAW,EAAE,kBAWzB,CAAC;AAEF,wBAAgB,aAAa,CAAC,MAAM,EAAE,kBAAkB,GAAG,IAAI,QAO9D"}
|
|
@@ -13,6 +13,19 @@ function handleMethodCall(methodName, ...args) {
|
|
|
13
13
|
}
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* proxyClient implements a proxy pattern for the AnalyticsInterface.
|
|
18
|
+
*
|
|
19
|
+
* It allows analytics method calls (track, identify, etc.) to be made before the real analytics client
|
|
20
|
+
* is fully initialized. Calls are queued and replayed once the real client is set via setRealClient.
|
|
21
|
+
* This ensures that analytics events are not lost if called early in the app lifecycle.
|
|
22
|
+
*
|
|
23
|
+
* The proxy also provides a fallback for getDebugInfo, returning proxy state if the real client is not ready.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* - Use proxyClient as the default analytics client before initialization.
|
|
27
|
+
* - Call setRealClient(realClientInstance) once the real client is ready.
|
|
28
|
+
*/
|
|
16
29
|
export const proxyClient = {
|
|
17
30
|
track: (event, props) => handleMethodCall('track', event, props),
|
|
18
31
|
identify: (userId, traits) => handleMethodCall('identify', userId, traits),
|
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,MAAM,MAAM,SAAS,GACjB,OAAO,GACP,UAAU,GACV,OAAO,GACP,QAAQ,GACR,OAAO,GACP,MAAM,CAAC;AAEX,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,iBAAkB,SAAQ,YAAY;IACrD,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,oBAAqB,SAAQ,iBAAiB;IAC7D,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,YAAY,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC;IAC5D,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC;IACjE,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC;IAC/D,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC;IAC5D,KAAK,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,kBAAkB,CAAC,EAAE,MAAM,IAAI,CAAC;IAChC,YAAY,CAAC,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC1C;AAED,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE;QACH,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,MAAM,EAAE;QACN,YAAY,EAAE,MAAM,CAAC;QACrB,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IACF,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,EAAE,EAAE;QACF,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,MAAM,EAAE;QACN,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,OAAO,EAAE;QACP,IAAI,EAAE,OAAO,CAAC;KACf,CAAC;IACF,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB"}
|
|
@@ -1,2 +1,8 @@
|
|
|
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
|
+
*/
|
|
1
7
|
export declare function getAnonymousId(): Promise<string>;
|
|
2
8
|
//# sourceMappingURL=anonymousId.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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
|
+
{"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"}
|
|
@@ -2,6 +2,12 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
|
2
2
|
import { v4 as uuidv4 } from 'uuid';
|
|
3
3
|
import { log, warn } from './logger';
|
|
4
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
|
+
*/
|
|
5
11
|
export async function getAnonymousId() {
|
|
6
12
|
try {
|
|
7
13
|
const existing = await AsyncStorage.getItem(STORAGE_KEY);
|
|
@@ -1,2 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import { EventContext } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Gathers and caches device, app, and environment context information for analytics events.
|
|
4
|
+
*
|
|
5
|
+
* - Collects details such as app name/version, device model/type, OS, screen size, locale, timezone, and network status.
|
|
6
|
+
* - Uses `react-native-device-info` and the current environment to populate fields.
|
|
7
|
+
* - Caches the result for the lifetime of the app to avoid redundant async calls.
|
|
8
|
+
* - Returns a context object suitable for event enrichment.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Promise<EventContext>} A promise that resolves to the context information object.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getContextInfo(): Promise<EventContext>;
|
|
2
13
|
//# sourceMappingURL=contextInfo.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contextInfo.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/contextInfo.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"contextInfo.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/contextInfo.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAWxC;;;;;;;;;GASG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,YAAY,CAAC,CA2C5D"}
|
|
@@ -8,6 +8,16 @@ try {
|
|
|
8
8
|
catch {
|
|
9
9
|
DeviceInfo = null;
|
|
10
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Gathers and caches device, app, and environment context information for analytics events.
|
|
13
|
+
*
|
|
14
|
+
* - Collects details such as app name/version, device model/type, OS, screen size, locale, timezone, and network status.
|
|
15
|
+
* - Uses `react-native-device-info` and the current environment to populate fields.
|
|
16
|
+
* - Caches the result for the lifetime of the app to avoid redundant async calls.
|
|
17
|
+
* - Returns a context object suitable for event enrichment.
|
|
18
|
+
*
|
|
19
|
+
* @returns {Promise<EventContext>} A promise that resolves to the context information object.
|
|
20
|
+
*/
|
|
11
21
|
export async function getContextInfo() {
|
|
12
22
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5;
|
|
13
23
|
if (cachedContext)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EnrichedEventPayload, EventContext, EventWithIdentity } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Enriches an analytics event with additional metadata required for ingestion.
|
|
4
|
+
*
|
|
5
|
+
* - Adds a unique messageId and the current sentAt timestamp.
|
|
6
|
+
* - Injects the provided writeKey and context information into the event.
|
|
7
|
+
* - Returns a new EnrichedEventPayload object, suitable for sending to the ingestion endpoint.
|
|
8
|
+
*
|
|
9
|
+
* @param event The base event with identity information.
|
|
10
|
+
* @param writeKey The write key for the analytics project.
|
|
11
|
+
* @param context The context information (device, app, environment, etc.).
|
|
12
|
+
* @returns The enriched event payload.
|
|
13
|
+
*/
|
|
14
|
+
export declare function enrichEvent(event: EventWithIdentity, writeKey: string, context: EventContext): EnrichedEventPayload;
|
|
15
|
+
//# sourceMappingURL=enrichEvent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enrichEvent.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/enrichEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAI,oBAAoB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAEnF;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,iBAAiB,EACxB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,YAAY,GACpB,oBAAoB,CAUtB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enriches an analytics event with additional metadata required for ingestion.
|
|
3
|
+
*
|
|
4
|
+
* - Adds a unique messageId and the current sentAt timestamp.
|
|
5
|
+
* - Injects the provided writeKey and context information into the event.
|
|
6
|
+
* - Returns a new EnrichedEventPayload object, suitable for sending to the ingestion endpoint.
|
|
7
|
+
*
|
|
8
|
+
* @param event The base event with identity information.
|
|
9
|
+
* @param writeKey The write key for the analytics project.
|
|
10
|
+
* @param context The context information (device, app, environment, etc.).
|
|
11
|
+
* @returns The enriched event payload.
|
|
12
|
+
*/
|
|
13
|
+
export function enrichEvent(event, writeKey, context) {
|
|
14
|
+
const enriched = {
|
|
15
|
+
...event,
|
|
16
|
+
writeKey,
|
|
17
|
+
messageId: `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`,
|
|
18
|
+
sentAt: new Date().toISOString(),
|
|
19
|
+
context,
|
|
20
|
+
};
|
|
21
|
+
return enriched;
|
|
22
|
+
}
|
|
@@ -1,5 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple logger utility for MetaRouter SDK.
|
|
3
|
+
*
|
|
4
|
+
* - Allows toggling debug logging for development and troubleshooting.
|
|
5
|
+
* - Always logs warnings and errors, regardless of debug setting.
|
|
6
|
+
* - Prepends all logs with a [MetaRouter] tag for easy identification.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Enables or disables debug logging.
|
|
10
|
+
* @param value If true, enables debug logging; if false, disables it.
|
|
11
|
+
*/
|
|
1
12
|
export declare function setDebugLogging(value: boolean): void;
|
|
13
|
+
/**
|
|
14
|
+
* Logs a message to the console if debug logging is enabled.
|
|
15
|
+
* @param args Arguments to log.
|
|
16
|
+
*/
|
|
2
17
|
export declare function log(...args: any[]): void;
|
|
18
|
+
/**
|
|
19
|
+
* Logs a warning to the console (always, regardless of debug setting).
|
|
20
|
+
* @param args Arguments to log as a warning.
|
|
21
|
+
*/
|
|
3
22
|
export declare function warn(...args: any[]): void;
|
|
23
|
+
/**
|
|
24
|
+
* Logs an error to the console (always, regardless of debug setting).
|
|
25
|
+
* @param args Arguments to log as an error.
|
|
26
|
+
*/
|
|
4
27
|
export declare function error(...args: any[]): void;
|
|
5
28
|
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/logger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/logger.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,QAE7C;AAED;;;GAGG;AACH,wBAAgB,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAEjC;AAED;;;GAGG;AACH,wBAAgB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAElC;AAED;;;GAGG;AACH,wBAAgB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,QAEnC"}
|
|
@@ -1,16 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simple logger utility for MetaRouter SDK.
|
|
3
|
+
*
|
|
4
|
+
* - Allows toggling debug logging for development and troubleshooting.
|
|
5
|
+
* - Always logs warnings and errors, regardless of debug setting.
|
|
6
|
+
* - Prepends all logs with a [MetaRouter] tag for easy identification.
|
|
7
|
+
*/
|
|
1
8
|
let enabled = false;
|
|
9
|
+
/**
|
|
10
|
+
* Enables or disables debug logging.
|
|
11
|
+
* @param value If true, enables debug logging; if false, disables it.
|
|
12
|
+
*/
|
|
2
13
|
export function setDebugLogging(value) {
|
|
3
14
|
enabled = value;
|
|
4
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Logs a message to the console if debug logging is enabled.
|
|
18
|
+
* @param args Arguments to log.
|
|
19
|
+
*/
|
|
5
20
|
export function log(...args) {
|
|
6
21
|
if (enabled)
|
|
7
22
|
console.log('[MetaRouter]', ...args);
|
|
8
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Logs a warning to the console (always, regardless of debug setting).
|
|
26
|
+
* @param args Arguments to log as a warning.
|
|
27
|
+
*/
|
|
9
28
|
export function warn(...args) {
|
|
10
|
-
// Always log warnings, even when debug is disabled
|
|
11
29
|
console.warn('[MetaRouter]', ...args);
|
|
12
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Logs an error to the console (always, regardless of debug setting).
|
|
33
|
+
* @param args Arguments to log as an error.
|
|
34
|
+
*/
|
|
13
35
|
export function error(...args) {
|
|
14
|
-
// Always log errors, even when debug is disabled
|
|
15
36
|
console.error('[MetaRouter]', ...args);
|
|
16
37
|
}
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retries an asynchronous operation with exponential backoff and optional jitter.
|
|
3
|
+
*
|
|
4
|
+
* - Attempts to execute the provided async function up to `retries` times.
|
|
5
|
+
* - Waits an exponentially increasing delay (with random jitter) between attempts.
|
|
6
|
+
* - If all attempts fail, the last error is thrown.
|
|
7
|
+
*
|
|
8
|
+
* @template T The return type of the async function.
|
|
9
|
+
* @param fn The asynchronous function to retry.
|
|
10
|
+
* @param retries The maximum number of attempts (default: 5).
|
|
11
|
+
* @param baseDelayMs The base delay in milliseconds for backoff (default: 1000).
|
|
12
|
+
* @param sleep Optional custom sleep function (default: setTimeout-based Promise).
|
|
13
|
+
* @returns A promise that resolves with the result of `fn`, or rejects after all retries fail.
|
|
14
|
+
*/
|
|
1
15
|
export declare function retryWithBackoff<T>(fn: () => Promise<T>, retries?: number, baseDelayMs?: number, sleep?: (ms: number) => Promise<unknown>): Promise<T>;
|
|
2
16
|
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/retry.ts"],"names":[],"mappings":"AAAA,wBAAsB,gBAAgB,CAAC,CAAC,EACpC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,SAAI,EACX,WAAW,SAAO,EAClB,KAAK,IAAI,IAAI,MAAM,qBAA8C,GAChE,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/retry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,EACpC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,SAAI,EACX,WAAW,SAAO,EAClB,KAAK,IAAI,IAAI,MAAM,qBAA8C,GAChE,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retries an asynchronous operation with exponential backoff and optional jitter.
|
|
3
|
+
*
|
|
4
|
+
* - Attempts to execute the provided async function up to `retries` times.
|
|
5
|
+
* - Waits an exponentially increasing delay (with random jitter) between attempts.
|
|
6
|
+
* - If all attempts fail, the last error is thrown.
|
|
7
|
+
*
|
|
8
|
+
* @template T The return type of the async function.
|
|
9
|
+
* @param fn The asynchronous function to retry.
|
|
10
|
+
* @param retries The maximum number of attempts (default: 5).
|
|
11
|
+
* @param baseDelayMs The base delay in milliseconds for backoff (default: 1000).
|
|
12
|
+
* @param sleep Optional custom sleep function (default: setTimeout-based Promise).
|
|
13
|
+
* @returns A promise that resolves with the result of `fn`, or rejects after all retries fail.
|
|
14
|
+
*/
|
|
1
15
|
export async function retryWithBackoff(fn, retries = 5, baseDelayMs = 1000, sleep = (ms) => new Promise((res) => setTimeout(res, ms))) {
|
|
2
16
|
let attempt = 0;
|
|
3
17
|
while (attempt < retries) {
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the current IANA time zone string for the environment.
|
|
3
|
+
* Falls back to 'UTC' if unavailable.
|
|
4
|
+
* @returns {string} The IANA time zone (e.g., 'America/New_York') or 'UTC'.
|
|
5
|
+
*/
|
|
1
6
|
export declare function getTimeZone(): string;
|
|
2
7
|
//# sourceMappingURL=timezone.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timezone.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/timezone.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"timezone.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/timezone.ts"],"names":[],"mappings":"AAAG;;;;GAIG;AACN,wBAAgB,WAAW,IAAI,MAAM,CAMlC"}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the current IANA time zone string for the environment.
|
|
3
|
+
* Falls back to 'UTC' if unavailable.
|
|
4
|
+
* @returns {string} The IANA time zone (e.g., 'America/New_York') or 'UTC'.
|
|
5
|
+
*/
|
|
1
6
|
export function getTimeZone() {
|
|
2
7
|
try {
|
|
3
8
|
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
package/lib/react/context.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import React, { ReactNode } from "react";
|
|
2
2
|
import type { MetaRouterInterface } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* Context for the MetaRouter analytics client.
|
|
5
|
+
* @param instance - The analytics client instance.
|
|
6
|
+
*/
|
|
3
7
|
export declare const MetaRouterContext: React.Context<MetaRouterInterface | null>;
|
|
8
|
+
/**
|
|
9
|
+
* Provider for the MetaRouter analytics client.
|
|
10
|
+
* @param children - The children to render.
|
|
11
|
+
* @param instance - The analytics client instance.
|
|
12
|
+
*/
|
|
4
13
|
export declare const MetaRouterProvider: ({ children, instance, }: {
|
|
5
14
|
children: ReactNode;
|
|
6
15
|
instance: MetaRouterInterface;
|
|
@@ -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"}
|
|
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;;;GAGG;AACH,eAAO,MAAM,iBAAiB,2CAE7B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAAI,yBAGhC;IACD,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,EAAE,mBAAmB,CAAC;CAC/B,sBAMA,CAAC"}
|
package/lib/react/context.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import React, { createContext } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Context for the MetaRouter analytics client.
|
|
4
|
+
* @param instance - The analytics client instance.
|
|
5
|
+
*/
|
|
2
6
|
export const MetaRouterContext = createContext(null);
|
|
7
|
+
/**
|
|
8
|
+
* Provider for the MetaRouter analytics client.
|
|
9
|
+
* @param children - The children to render.
|
|
10
|
+
* @param instance - The analytics client instance.
|
|
11
|
+
*/
|
|
3
12
|
export const MetaRouterProvider = ({ children, instance, }) => {
|
|
4
13
|
return (React.createElement(MetaRouterContext.Provider, { value: instance }, children));
|
|
5
14
|
};
|
|
@@ -1 +1 @@
|
|
|
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"}
|
|
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;;;GAGG;AACH,eAAO,MAAM,aAAa,QAAO;IAAE,SAAS,EAAE,kBAAkB,CAAA;CAQ7D,CAAC"}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { useContext } from "react";
|
|
2
2
|
import { MetaRouterContext } from "./context";
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook to access the MetaRouter analytics client.
|
|
5
|
+
* @returns The analytics client instance.
|
|
6
|
+
*/
|
|
3
7
|
export const useMetaRouter = () => {
|
|
4
8
|
const ctx = useContext(MetaRouterContext);
|
|
5
9
|
if (!ctx)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metarouter/react-native-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.25",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"clean": "rm -rf lib",
|
|
19
19
|
"build": "tsc --project tsconfig.build.json",
|
|
20
20
|
"prepare": "npm run build",
|
|
21
|
-
"
|
|
21
|
+
"prepublishOnly": "npm run clean && npm run build",
|
|
22
22
|
"test": "jest"
|
|
23
23
|
},
|
|
24
24
|
"publishConfig": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"enrichEvent.d.ts","sourceRoot":"","sources":["../../src/analytics/enrichEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAI,oBAAoB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAGlF,wBAAgB,WAAW,CACzB,KAAK,EAAE,iBAAiB,EACxB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,YAAY,GACpB,oBAAoB,CAUtB"}
|