@explorins/pers-sdk-react-native 2.3.7 → 2.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1266 -1244
  3. package/dist/__tests__/PersSDKProvider.test.js +33 -0
  4. package/dist/hooks/useBusiness.d.ts +2 -2
  5. package/dist/hooks/useBusiness.d.ts.map +1 -1
  6. package/dist/hooks/useBusiness.js +2 -2
  7. package/dist/hooks/useIPFSUrl.d.ts +3 -3
  8. package/dist/hooks/useIPFSUrl.d.ts.map +1 -1
  9. package/dist/hooks/useIPFSUrl.js +7 -7
  10. package/dist/hooks/useWeb3.js +4 -4
  11. package/dist/index.js +2400 -487
  12. package/dist/index.js.map +1 -1
  13. package/dist/providers/PersSDKProvider.d.ts +4 -0
  14. package/dist/providers/PersSDKProvider.d.ts.map +1 -1
  15. package/dist/providers/PersSDKProvider.js +14 -0
  16. package/dist/providers/react-native-http-client.d.ts +2 -0
  17. package/dist/providers/react-native-http-client.d.ts.map +1 -1
  18. package/dist/providers/react-native-http-client.js +5 -2
  19. package/dist/storage/rn-secure-storage.d.ts.map +1 -1
  20. package/dist/storage/rn-secure-storage.js +21 -8
  21. package/package.json +3 -3
  22. package/src/__tests__/PersSDKProvider.test.tsx +484 -420
  23. package/src/__tests__/setup.ts +40 -40
  24. package/src/hooks/index.ts +60 -60
  25. package/src/hooks/useAnalytics.ts +327 -327
  26. package/src/hooks/useAuth.ts +265 -265
  27. package/src/hooks/useBookings.ts +191 -191
  28. package/src/hooks/useBusiness.ts +251 -251
  29. package/src/hooks/useCampaigns.ts +500 -500
  30. package/src/hooks/useDonations.ts +66 -66
  31. package/src/hooks/useEvents.ts +372 -372
  32. package/src/hooks/useFiles.ts +155 -155
  33. package/src/hooks/useIPFSUrl.ts +192 -192
  34. package/src/hooks/usePurchases.ts +65 -65
  35. package/src/hooks/useRedemptions.ts +467 -467
  36. package/src/hooks/useTenants.ts +72 -72
  37. package/src/hooks/useTokenBalances.ts +385 -385
  38. package/src/hooks/useTokens.ts +313 -313
  39. package/src/hooks/useTransactionSigner.ts +611 -611
  40. package/src/hooks/useTransactions.ts +299 -299
  41. package/src/hooks/useTriggerSources.ts +301 -301
  42. package/src/hooks/useUserStatus.ts +61 -61
  43. package/src/hooks/useUsers.ts +167 -167
  44. package/src/hooks/useWeb3.ts +355 -355
  45. package/src/index.ts +777 -777
  46. package/src/polyfills/index.ts +160 -160
  47. package/src/polyfills/web3-polyfills.ts +97 -97
  48. package/src/providers/PersSDKProvider.tsx +393 -374
  49. package/src/providers/react-native-auth-provider.ts +110 -110
  50. package/src/providers/react-native-http-client.ts +150 -145
  51. package/src/providers/rn-dpop-provider.ts +190 -190
  52. package/src/storage/async-storage-token-storage.ts +185 -185
  53. package/src/storage/rn-secure-storage.ts +182 -170
  54. package/src/types/react-native-globals.d.ts +45 -45
  55. package/src/utils/index.ts +8 -8
  56. package/src/utils/platform-detection.ts +86 -86
  57. package/dist/index-CzLxqePZ.js +0 -7717
  58. package/dist/index-CzLxqePZ.js.map +0 -1
@@ -1,328 +1,328 @@
1
- import { useCallback } from 'react';
2
- import { usePersSDK } from '../providers/PersSDKProvider';
3
- import {
4
- TransactionAnalyticsRequestDTO,
5
- TransactionAnalyticsResponseDTO,
6
- CampaignClaimAnalyticsRequestDTO,
7
- CampaignClaimAnalyticsResponseDTO,
8
- UserAnalyticsRequestDTO,
9
- UserAnalyticsResponseDTO,
10
- UserRankingAnalyticsRequestDTO,
11
- UserRankingAnalyticsResponseDTO,
12
- BusinessRankingAnalyticsRequestDTO,
13
- BusinessRankingAnalyticsResponseDTO,
14
- RetentionAnalyticsRequestDTO,
15
- RetentionAnalyticsResponseDTO
16
- } from '@explorins/pers-sdk';
17
-
18
- /**
19
- * React hook for analytics operations in the PERS SDK
20
- *
21
- * Provides comprehensive analytics and business intelligence capabilities:
22
- * - **Transaction Analytics**: Volume, trends, business performance
23
- * - **Campaign Claim Analytics**: Campaign performance and claim patterns
24
- * - **User Analytics**: Engagement metrics with per-active-user averages
25
- * - **User Ranking**: Leaderboards with full user details
26
- * - **Business Ranking**: Business performance rankings
27
- * - **Retention Analytics**: Monthly retention metrics
28
- *
29
- * @returns Analytics hook with methods for data analysis
30
- *
31
- * @example Basic Transaction Analytics
32
- * ```typescript
33
- * function AnalyticsComponent() {
34
- * const { getTransactionAnalytics } = useAnalytics();
35
- *
36
- * const loadAnalytics = async () => {
37
- * const analytics = await getTransactionAnalytics({
38
- * startDate: '2024-01-01',
39
- * endDate: '2024-01-31',
40
- * groupBy: 'day'
41
- * });
42
- * console.log('Transaction analytics:', analytics);
43
- * };
44
- * }
45
- * ```
46
- *
47
- * @example User Leaderboard
48
- * ```typescript
49
- * function LeaderboardScreen() {
50
- * const { getUserRanking } = useAnalytics();
51
- *
52
- * const loadLeaderboard = async () => {
53
- * const ranking = await getUserRanking({
54
- * sortBy: 'totalTransactions',
55
- * sortOrder: 'DESC',
56
- * limit: 50
57
- * });
58
- * ranking.results.forEach((user, i) => {
59
- * console.log(`#${i + 1}: ${user.email} - ${user.totalTransactions} txns`);
60
- * });
61
- * };
62
- * }
63
- * ```
64
- */
65
- export const useAnalytics = () => {
66
- const { sdk, isInitialized } = usePersSDK();
67
-
68
- /**
69
- * Retrieves transaction analytics data based on request parameters
70
- *
71
- * @param request - Analytics request parameters (time range, grouping, filters, etc.)
72
- * @returns Promise resolving to analytics data with results and metadata
73
- * @throws Error if SDK is not initialized
74
- *
75
- * @example
76
- * ```typescript
77
- * const analytics = await getTransactionAnalytics({
78
- * startDate: '2024-01-01',
79
- * endDate: '2024-01-31',
80
- * groupBy: 'day',
81
- * metrics: ['count', 'sum']
82
- * });
83
- * ```
84
- */
85
- const getTransactionAnalytics = useCallback(async (
86
- request: TransactionAnalyticsRequestDTO
87
- ): Promise<TransactionAnalyticsResponseDTO> => {
88
- if (!isInitialized || !sdk) {
89
- throw new Error('SDK not initialized. Call initialize() first.');
90
- }
91
-
92
- try {
93
- const result = await sdk.analytics.getTransactionAnalytics(request);
94
- return result;
95
- } catch (error) {
96
- console.error('Failed to fetch transaction analytics:', error);
97
- throw error;
98
- }
99
- }, [sdk, isInitialized]);
100
-
101
- /**
102
- * Retrieves campaign claim analytics with aggregation
103
- *
104
- * Provides insights into campaign performance, claim patterns, and user engagement.
105
- *
106
- * @param request - Analytics request with filters, groupBy, and metrics
107
- * @returns Promise resolving to campaign claim analytics data
108
- *
109
- * @example Claims per campaign
110
- * ```typescript
111
- * const analytics = await getCampaignClaimAnalytics({
112
- * filters: { status: 'COMPLETED' },
113
- * groupBy: ['campaignId'],
114
- * metrics: ['count'],
115
- * sortBy: 'count',
116
- * sortOrder: 'DESC',
117
- * limit: 10
118
- * });
119
- * console.log('Top campaigns:', analytics.results);
120
- * ```
121
- */
122
- const getCampaignClaimAnalytics = useCallback(async (
123
- request: CampaignClaimAnalyticsRequestDTO
124
- ): Promise<CampaignClaimAnalyticsResponseDTO> => {
125
- if (!isInitialized || !sdk) {
126
- throw new Error('SDK not initialized. Call initialize() first.');
127
- }
128
-
129
- try {
130
- const result = await sdk.analytics.getCampaignClaimAnalytics(request);
131
- return result;
132
- } catch (error) {
133
- console.error('Failed to fetch campaign claim analytics:', error);
134
- throw error;
135
- }
136
- }, [sdk, isInitialized]);
137
-
138
- /**
139
- * Retrieves user analytics with engagement metrics
140
- *
141
- * Includes both per-user and per-active-user metrics for accurate engagement insights.
142
- * Per-active-user metrics show concentrated engagement among active users.
143
- *
144
- * @param request - Analytics request with optional filters and date range
145
- * @returns Promise resolving to user analytics data
146
- *
147
- * @example Compare per-user vs per-active-user metrics
148
- * ```typescript
149
- * const analytics = await getUserAnalytics({
150
- * startDate: new Date('2026-02-01'),
151
- * endDate: new Date('2026-02-28')
152
- * });
153
- *
154
- * console.log(`Active users: ${analytics.activeUsers} / ${analytics.totalUsers}`);
155
- * console.log(`Engagement rate: ${analytics.engagementRate.toFixed(1)}%`);
156
- *
157
- * // Per-active-user metrics (more meaningful)
158
- * console.log(`Avg transactions per active user: ${analytics.averageTransactionsPerActiveUser}`);
159
- * ```
160
- *
161
- * @example Business-specific analytics
162
- * ```typescript
163
- * const analytics = await getUserAnalytics({
164
- * filters: { businessId: 'business-123' },
165
- * startDate: new Date('2026-01-01'),
166
- * endDate: new Date('2026-12-31')
167
- * });
168
- * ```
169
- */
170
- const getUserAnalytics = useCallback(async (
171
- request: UserAnalyticsRequestDTO = {}
172
- ): Promise<UserAnalyticsResponseDTO> => {
173
- if (!isInitialized || !sdk) {
174
- throw new Error('SDK not initialized. Call initialize() first.');
175
- }
176
-
177
- try {
178
- const result = await sdk.analytics.getUserAnalytics(request);
179
- return result;
180
- } catch (error) {
181
- console.error('Failed to fetch user analytics:', error);
182
- throw error;
183
- }
184
- }, [sdk, isInitialized]);
185
-
186
- /**
187
- * Retrieves user transaction ranking with enriched user data
188
- *
189
- * Returns ranked list of users with full user details (email, externalUserId)
190
- * and transaction metrics. Ideal for leaderboards, engagement analysis,
191
- * and identifying power users.
192
- *
193
- * @param request - Ranking request with filters, sorting, and limit
194
- * @returns Promise resolving to ranked user list with transaction metrics
195
- *
196
- * @example Top 50 users by transaction count
197
- * ```typescript
198
- * const ranking = await getUserRanking({
199
- * sortBy: 'totalTransactions',
200
- * sortOrder: 'DESC',
201
- * limit: 50
202
- * });
203
- *
204
- * ranking.results.forEach((user, index) => {
205
- * console.log(`#${index + 1}: ${user.email || user.externalUserId}`);
206
- * console.log(` Transactions: ${user.totalTransactions}`);
207
- * console.log(` Token spent: ${user.tokenSpent}`);
208
- * });
209
- * ```
210
- *
211
- * @example Top STAMP spenders
212
- * ```typescript
213
- * const ranking = await getUserRanking({
214
- * filters: { tokenType: 'STAMP' },
215
- * sortBy: 'tokenSpent',
216
- * sortOrder: 'DESC',
217
- * limit: 20
218
- * });
219
- * ```
220
- */
221
- const getUserRanking = useCallback(async (
222
- request: UserRankingAnalyticsRequestDTO = {}
223
- ): Promise<UserRankingAnalyticsResponseDTO> => {
224
- if (!isInitialized || !sdk) {
225
- throw new Error('SDK not initialized. Call initialize() first.');
226
- }
227
-
228
- try {
229
- const result = await sdk.analytics.getUserRanking(request);
230
- return result;
231
- } catch (error) {
232
- console.error('Failed to fetch user ranking:', error);
233
- throw error;
234
- }
235
- }, [sdk, isInitialized]);
236
-
237
- /**
238
- * Retrieves business transaction ranking with enriched business data
239
- *
240
- * Returns ranked list of businesses with transaction metrics for
241
- * partner analytics and performance dashboards.
242
- *
243
- * @param request - Ranking request with filters, sorting, and limit
244
- * @returns Promise resolving to ranked business list
245
- *
246
- * @example Top businesses by transaction count
247
- * ```typescript
248
- * const ranking = await getBusinessRanking({
249
- * sortBy: 'totalTransactions',
250
- * sortOrder: 'DESC',
251
- * limit: 20
252
- * });
253
- *
254
- * ranking.results.forEach((business, index) => {
255
- * console.log(`#${index + 1}: ${business.businessId}`);
256
- * console.log(` Transactions: ${business.totalTransactions}`);
257
- * console.log(` Token spent: ${business.tokenSpent}`);
258
- * });
259
- * ```
260
- */
261
- const getBusinessRanking = useCallback(async (
262
- request: BusinessRankingAnalyticsRequestDTO = {}
263
- ): Promise<BusinessRankingAnalyticsResponseDTO> => {
264
- if (!isInitialized || !sdk) {
265
- throw new Error('SDK not initialized. Call initialize() first.');
266
- }
267
-
268
- try {
269
- const result = await sdk.analytics.getBusinessRanking(request);
270
- return result;
271
- } catch (error) {
272
- console.error('Failed to fetch business ranking:', error);
273
- throw error;
274
- }
275
- }, [sdk, isInitialized]);
276
-
277
- /**
278
- * Retrieves monthly user retention analytics
279
- *
280
- * Returns monthly retention data with active, new, and returning users
281
- * along with retention rates. Useful for churn analysis and engagement trends.
282
- *
283
- * @param request - Retention request with monthsBack and filters
284
- * @returns Promise resolving to monthly retention data
285
- *
286
- * @example Get 12 months of retention data
287
- * ```typescript
288
- * const retention = await getRetentionAnalytics({
289
- * monthsBack: 12
290
- * });
291
- *
292
- * retention.results.forEach(month => {
293
- * console.log(`${month.month}:`);
294
- * console.log(` Active: ${month.activeUsers}`);
295
- * console.log(` New: ${month.newUsers}`);
296
- * console.log(` Returning: ${month.returningUsers}`);
297
- * console.log(` Retention Rate: ${month.retentionRate.toFixed(1)}%`);
298
- * });
299
- * ```
300
- */
301
- const getRetentionAnalytics = useCallback(async (
302
- request: RetentionAnalyticsRequestDTO = {}
303
- ): Promise<RetentionAnalyticsResponseDTO> => {
304
- if (!isInitialized || !sdk) {
305
- throw new Error('SDK not initialized. Call initialize() first.');
306
- }
307
-
308
- try {
309
- const result = await sdk.analytics.getRetentionAnalytics(request);
310
- return result;
311
- } catch (error) {
312
- console.error('Failed to fetch retention analytics:', error);
313
- throw error;
314
- }
315
- }, [sdk, isInitialized]);
316
-
317
- return {
318
- getTransactionAnalytics,
319
- getCampaignClaimAnalytics,
320
- getUserAnalytics,
321
- getUserRanking,
322
- getBusinessRanking,
323
- getRetentionAnalytics,
324
- isAvailable: isInitialized && !!sdk?.analytics,
325
- };
326
- };
327
-
1
+ import { useCallback } from 'react';
2
+ import { usePersSDK } from '../providers/PersSDKProvider';
3
+ import {
4
+ TransactionAnalyticsRequestDTO,
5
+ TransactionAnalyticsResponseDTO,
6
+ CampaignClaimAnalyticsRequestDTO,
7
+ CampaignClaimAnalyticsResponseDTO,
8
+ UserAnalyticsRequestDTO,
9
+ UserAnalyticsResponseDTO,
10
+ UserRankingAnalyticsRequestDTO,
11
+ UserRankingAnalyticsResponseDTO,
12
+ BusinessRankingAnalyticsRequestDTO,
13
+ BusinessRankingAnalyticsResponseDTO,
14
+ RetentionAnalyticsRequestDTO,
15
+ RetentionAnalyticsResponseDTO
16
+ } from '@explorins/pers-sdk';
17
+
18
+ /**
19
+ * React hook for analytics operations in the PERS SDK
20
+ *
21
+ * Provides comprehensive analytics and business intelligence capabilities:
22
+ * - **Transaction Analytics**: Volume, trends, business performance
23
+ * - **Campaign Claim Analytics**: Campaign performance and claim patterns
24
+ * - **User Analytics**: Engagement metrics with per-active-user averages
25
+ * - **User Ranking**: Leaderboards with full user details
26
+ * - **Business Ranking**: Business performance rankings
27
+ * - **Retention Analytics**: Monthly retention metrics
28
+ *
29
+ * @returns Analytics hook with methods for data analysis
30
+ *
31
+ * @example Basic Transaction Analytics
32
+ * ```typescript
33
+ * function AnalyticsComponent() {
34
+ * const { getTransactionAnalytics } = useAnalytics();
35
+ *
36
+ * const loadAnalytics = async () => {
37
+ * const analytics = await getTransactionAnalytics({
38
+ * startDate: '2024-01-01',
39
+ * endDate: '2024-01-31',
40
+ * groupBy: 'day'
41
+ * });
42
+ * console.log('Transaction analytics:', analytics);
43
+ * };
44
+ * }
45
+ * ```
46
+ *
47
+ * @example User Leaderboard
48
+ * ```typescript
49
+ * function LeaderboardScreen() {
50
+ * const { getUserRanking } = useAnalytics();
51
+ *
52
+ * const loadLeaderboard = async () => {
53
+ * const ranking = await getUserRanking({
54
+ * sortBy: 'totalTransactions',
55
+ * sortOrder: 'DESC',
56
+ * limit: 50
57
+ * });
58
+ * ranking.results.forEach((user, i) => {
59
+ * console.log(`#${i + 1}: ${user.email} - ${user.totalTransactions} txns`);
60
+ * });
61
+ * };
62
+ * }
63
+ * ```
64
+ */
65
+ export const useAnalytics = () => {
66
+ const { sdk, isInitialized } = usePersSDK();
67
+
68
+ /**
69
+ * Retrieves transaction analytics data based on request parameters
70
+ *
71
+ * @param request - Analytics request parameters (time range, grouping, filters, etc.)
72
+ * @returns Promise resolving to analytics data with results and metadata
73
+ * @throws Error if SDK is not initialized
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * const analytics = await getTransactionAnalytics({
78
+ * startDate: '2024-01-01',
79
+ * endDate: '2024-01-31',
80
+ * groupBy: 'day',
81
+ * metrics: ['count', 'sum']
82
+ * });
83
+ * ```
84
+ */
85
+ const getTransactionAnalytics = useCallback(async (
86
+ request: TransactionAnalyticsRequestDTO
87
+ ): Promise<TransactionAnalyticsResponseDTO> => {
88
+ if (!isInitialized || !sdk) {
89
+ throw new Error('SDK not initialized. Call initialize() first.');
90
+ }
91
+
92
+ try {
93
+ const result = await sdk.analytics.getTransactionAnalytics(request);
94
+ return result;
95
+ } catch (error) {
96
+ console.error('Failed to fetch transaction analytics:', error);
97
+ throw error;
98
+ }
99
+ }, [sdk, isInitialized]);
100
+
101
+ /**
102
+ * Retrieves campaign claim analytics with aggregation
103
+ *
104
+ * Provides insights into campaign performance, claim patterns, and user engagement.
105
+ *
106
+ * @param request - Analytics request with filters, groupBy, and metrics
107
+ * @returns Promise resolving to campaign claim analytics data
108
+ *
109
+ * @example Claims per campaign
110
+ * ```typescript
111
+ * const analytics = await getCampaignClaimAnalytics({
112
+ * filters: { status: 'COMPLETED' },
113
+ * groupBy: ['campaignId'],
114
+ * metrics: ['count'],
115
+ * sortBy: 'count',
116
+ * sortOrder: 'DESC',
117
+ * limit: 10
118
+ * });
119
+ * console.log('Top campaigns:', analytics.results);
120
+ * ```
121
+ */
122
+ const getCampaignClaimAnalytics = useCallback(async (
123
+ request: CampaignClaimAnalyticsRequestDTO
124
+ ): Promise<CampaignClaimAnalyticsResponseDTO> => {
125
+ if (!isInitialized || !sdk) {
126
+ throw new Error('SDK not initialized. Call initialize() first.');
127
+ }
128
+
129
+ try {
130
+ const result = await sdk.analytics.getCampaignClaimAnalytics(request);
131
+ return result;
132
+ } catch (error) {
133
+ console.error('Failed to fetch campaign claim analytics:', error);
134
+ throw error;
135
+ }
136
+ }, [sdk, isInitialized]);
137
+
138
+ /**
139
+ * Retrieves user analytics with engagement metrics
140
+ *
141
+ * Includes both per-user and per-active-user metrics for accurate engagement insights.
142
+ * Per-active-user metrics show concentrated engagement among active users.
143
+ *
144
+ * @param request - Analytics request with optional filters and date range
145
+ * @returns Promise resolving to user analytics data
146
+ *
147
+ * @example Compare per-user vs per-active-user metrics
148
+ * ```typescript
149
+ * const analytics = await getUserAnalytics({
150
+ * startDate: new Date('2026-02-01'),
151
+ * endDate: new Date('2026-02-28')
152
+ * });
153
+ *
154
+ * console.log(`Active users: ${analytics.activeUsers} / ${analytics.totalUsers}`);
155
+ * console.log(`Engagement rate: ${analytics.engagementRate.toFixed(1)}%`);
156
+ *
157
+ * // Per-active-user metrics (more meaningful)
158
+ * console.log(`Avg transactions per active user: ${analytics.averageTransactionsPerActiveUser}`);
159
+ * ```
160
+ *
161
+ * @example Business-specific analytics
162
+ * ```typescript
163
+ * const analytics = await getUserAnalytics({
164
+ * filters: { businessId: 'business-123' },
165
+ * startDate: new Date('2026-01-01'),
166
+ * endDate: new Date('2026-12-31')
167
+ * });
168
+ * ```
169
+ */
170
+ const getUserAnalytics = useCallback(async (
171
+ request: UserAnalyticsRequestDTO = {}
172
+ ): Promise<UserAnalyticsResponseDTO> => {
173
+ if (!isInitialized || !sdk) {
174
+ throw new Error('SDK not initialized. Call initialize() first.');
175
+ }
176
+
177
+ try {
178
+ const result = await sdk.analytics.getUserAnalytics(request);
179
+ return result;
180
+ } catch (error) {
181
+ console.error('Failed to fetch user analytics:', error);
182
+ throw error;
183
+ }
184
+ }, [sdk, isInitialized]);
185
+
186
+ /**
187
+ * Retrieves user transaction ranking with enriched user data
188
+ *
189
+ * Returns ranked list of users with full user details (email, externalUserId)
190
+ * and transaction metrics. Ideal for leaderboards, engagement analysis,
191
+ * and identifying power users.
192
+ *
193
+ * @param request - Ranking request with filters, sorting, and limit
194
+ * @returns Promise resolving to ranked user list with transaction metrics
195
+ *
196
+ * @example Top 50 users by transaction count
197
+ * ```typescript
198
+ * const ranking = await getUserRanking({
199
+ * sortBy: 'totalTransactions',
200
+ * sortOrder: 'DESC',
201
+ * limit: 50
202
+ * });
203
+ *
204
+ * ranking.results.forEach((user, index) => {
205
+ * console.log(`#${index + 1}: ${user.email || user.externalUserId}`);
206
+ * console.log(` Transactions: ${user.totalTransactions}`);
207
+ * console.log(` Token spent: ${user.tokenSpent}`);
208
+ * });
209
+ * ```
210
+ *
211
+ * @example Top STAMP spenders
212
+ * ```typescript
213
+ * const ranking = await getUserRanking({
214
+ * filters: { tokenType: 'STAMP' },
215
+ * sortBy: 'tokenSpent',
216
+ * sortOrder: 'DESC',
217
+ * limit: 20
218
+ * });
219
+ * ```
220
+ */
221
+ const getUserRanking = useCallback(async (
222
+ request: UserRankingAnalyticsRequestDTO = {}
223
+ ): Promise<UserRankingAnalyticsResponseDTO> => {
224
+ if (!isInitialized || !sdk) {
225
+ throw new Error('SDK not initialized. Call initialize() first.');
226
+ }
227
+
228
+ try {
229
+ const result = await sdk.analytics.getUserRanking(request);
230
+ return result;
231
+ } catch (error) {
232
+ console.error('Failed to fetch user ranking:', error);
233
+ throw error;
234
+ }
235
+ }, [sdk, isInitialized]);
236
+
237
+ /**
238
+ * Retrieves business transaction ranking with enriched business data
239
+ *
240
+ * Returns ranked list of businesses with transaction metrics for
241
+ * partner analytics and performance dashboards.
242
+ *
243
+ * @param request - Ranking request with filters, sorting, and limit
244
+ * @returns Promise resolving to ranked business list
245
+ *
246
+ * @example Top businesses by transaction count
247
+ * ```typescript
248
+ * const ranking = await getBusinessRanking({
249
+ * sortBy: 'totalTransactions',
250
+ * sortOrder: 'DESC',
251
+ * limit: 20
252
+ * });
253
+ *
254
+ * ranking.results.forEach((business, index) => {
255
+ * console.log(`#${index + 1}: ${business.businessId}`);
256
+ * console.log(` Transactions: ${business.totalTransactions}`);
257
+ * console.log(` Token spent: ${business.tokenSpent}`);
258
+ * });
259
+ * ```
260
+ */
261
+ const getBusinessRanking = useCallback(async (
262
+ request: BusinessRankingAnalyticsRequestDTO = {}
263
+ ): Promise<BusinessRankingAnalyticsResponseDTO> => {
264
+ if (!isInitialized || !sdk) {
265
+ throw new Error('SDK not initialized. Call initialize() first.');
266
+ }
267
+
268
+ try {
269
+ const result = await sdk.analytics.getBusinessRanking(request);
270
+ return result;
271
+ } catch (error) {
272
+ console.error('Failed to fetch business ranking:', error);
273
+ throw error;
274
+ }
275
+ }, [sdk, isInitialized]);
276
+
277
+ /**
278
+ * Retrieves monthly user retention analytics
279
+ *
280
+ * Returns monthly retention data with active, new, and returning users
281
+ * along with retention rates. Useful for churn analysis and engagement trends.
282
+ *
283
+ * @param request - Retention request with monthsBack and filters
284
+ * @returns Promise resolving to monthly retention data
285
+ *
286
+ * @example Get 12 months of retention data
287
+ * ```typescript
288
+ * const retention = await getRetentionAnalytics({
289
+ * monthsBack: 12
290
+ * });
291
+ *
292
+ * retention.results.forEach(month => {
293
+ * console.log(`${month.month}:`);
294
+ * console.log(` Active: ${month.activeUsers}`);
295
+ * console.log(` New: ${month.newUsers}`);
296
+ * console.log(` Returning: ${month.returningUsers}`);
297
+ * console.log(` Retention Rate: ${month.retentionRate.toFixed(1)}%`);
298
+ * });
299
+ * ```
300
+ */
301
+ const getRetentionAnalytics = useCallback(async (
302
+ request: RetentionAnalyticsRequestDTO = {}
303
+ ): Promise<RetentionAnalyticsResponseDTO> => {
304
+ if (!isInitialized || !sdk) {
305
+ throw new Error('SDK not initialized. Call initialize() first.');
306
+ }
307
+
308
+ try {
309
+ const result = await sdk.analytics.getRetentionAnalytics(request);
310
+ return result;
311
+ } catch (error) {
312
+ console.error('Failed to fetch retention analytics:', error);
313
+ throw error;
314
+ }
315
+ }, [sdk, isInitialized]);
316
+
317
+ return {
318
+ getTransactionAnalytics,
319
+ getCampaignClaimAnalytics,
320
+ getUserAnalytics,
321
+ getUserRanking,
322
+ getBusinessRanking,
323
+ getRetentionAnalytics,
324
+ isAvailable: isInitialized && !!sdk?.analytics,
325
+ };
326
+ };
327
+
328
328
  export type AnalyticsHook = ReturnType<typeof useAnalytics>;