@datalyr/react-native 1.1.1 → 1.2.0

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 (42) hide show
  1. package/CHANGELOG.md +24 -141
  2. package/LICENSE +21 -0
  3. package/README.md +405 -217
  4. package/datalyr-react-native.podspec +31 -0
  5. package/ios/DatalyrNative.m +70 -0
  6. package/ios/DatalyrNative.swift +275 -0
  7. package/ios/DatalyrSKAdNetwork.m +26 -0
  8. package/lib/datalyr-sdk.d.ts +64 -3
  9. package/lib/datalyr-sdk.js +322 -3
  10. package/lib/index.d.ts +1 -0
  11. package/lib/index.js +4 -2
  12. package/lib/integrations/index.d.ts +6 -0
  13. package/lib/integrations/index.js +6 -0
  14. package/lib/integrations/meta-integration.d.ts +76 -0
  15. package/lib/integrations/meta-integration.js +218 -0
  16. package/lib/integrations/tiktok-integration.d.ts +82 -0
  17. package/lib/integrations/tiktok-integration.js +356 -0
  18. package/lib/native/DatalyrNativeBridge.d.ts +31 -0
  19. package/lib/native/DatalyrNativeBridge.js +168 -0
  20. package/lib/native/index.d.ts +5 -0
  21. package/lib/native/index.js +5 -0
  22. package/lib/types.d.ts +29 -0
  23. package/package.json +10 -5
  24. package/src/datalyr-sdk-expo.ts +957 -0
  25. package/src/datalyr-sdk.ts +419 -19
  26. package/src/expo.ts +38 -18
  27. package/src/index.ts +5 -2
  28. package/src/integrations/index.ts +7 -0
  29. package/src/integrations/meta-integration.ts +238 -0
  30. package/src/integrations/tiktok-integration.ts +360 -0
  31. package/src/native/DatalyrNativeBridge.ts +271 -0
  32. package/src/native/index.ts +11 -0
  33. package/src/types.ts +39 -0
  34. package/src/utils-expo.ts +25 -3
  35. package/src/utils-interface.ts +38 -0
  36. package/EXPO_INSTALL.md +0 -297
  37. package/INSTALL.md +0 -402
  38. package/examples/attribution-example.tsx +0 -377
  39. package/examples/auto-events-example.tsx +0 -403
  40. package/examples/example.tsx +0 -250
  41. package/examples/skadnetwork-example.tsx +0 -380
  42. package/examples/test-implementation.tsx +0 -163
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Meta (Facebook) SDK Integration
3
+ * Uses bundled native iOS SDK for deferred deep linking, event forwarding, and Advanced Matching
4
+ */
5
+
6
+ import { Platform } from 'react-native';
7
+ import { MetaConfig, DeferredDeepLinkResult } from '../types';
8
+ import { MetaNativeBridge, isNativeModuleAvailable } from '../native/DatalyrNativeBridge';
9
+
10
+ /**
11
+ * Meta Integration class for handling Facebook SDK operations
12
+ * Uses native iOS SDK bundled via CocoaPods (no additional npm packages required)
13
+ */
14
+ export class MetaIntegration {
15
+ private config: MetaConfig | null = null;
16
+ private initialized: boolean = false;
17
+ private available: boolean = false;
18
+ private debug: boolean = false;
19
+ private deferredDeepLinkData: DeferredDeepLinkResult | null = null;
20
+
21
+ /**
22
+ * Initialize Meta SDK with configuration
23
+ */
24
+ async initialize(config: MetaConfig, debug: boolean = false): Promise<void> {
25
+ this.debug = debug;
26
+ this.config = config;
27
+
28
+ // Only available on iOS via native module
29
+ if (Platform.OS !== 'ios') {
30
+ this.log('Meta SDK only available on iOS');
31
+ return;
32
+ }
33
+
34
+ this.available = isNativeModuleAvailable();
35
+
36
+ if (!this.available) {
37
+ this.log('Meta native module not available');
38
+ return;
39
+ }
40
+
41
+ try {
42
+ const success = await MetaNativeBridge.initialize(
43
+ config.appId,
44
+ config.clientToken,
45
+ config.advertiserTrackingEnabled ?? false
46
+ );
47
+
48
+ if (success) {
49
+ this.initialized = true;
50
+ this.log(`Meta SDK initialized with App ID: ${config.appId}`);
51
+ }
52
+ } catch (error) {
53
+ this.logError('Failed to initialize Meta SDK:', error);
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Update tracking authorization status (call after ATT prompt)
59
+ */
60
+ async updateTrackingAuthorization(enabled: boolean): Promise<void> {
61
+ if (!this.available || !this.initialized) return;
62
+
63
+ try {
64
+ await MetaNativeBridge.updateTrackingAuthorization(enabled);
65
+ this.log(`Meta ATT status updated: ${enabled ? 'authorized' : 'not authorized'}`);
66
+ } catch (error) {
67
+ this.logError('Failed to update Meta tracking authorization:', error);
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Fetch deferred deep link from Meta SDK
73
+ * This captures fbclid for installs that went through App Store
74
+ */
75
+ async fetchDeferredDeepLink(): Promise<DeferredDeepLinkResult | null> {
76
+ if (!this.available || !this.initialized) {
77
+ return null;
78
+ }
79
+
80
+ if (this.config?.enableDeferredDeepLink === false) {
81
+ return null;
82
+ }
83
+
84
+ try {
85
+ const url = await MetaNativeBridge.fetchDeferredAppLink();
86
+
87
+ if (!url) {
88
+ this.log('No deferred deep link available from Meta');
89
+ return null;
90
+ }
91
+
92
+ // Parse the URL for attribution parameters
93
+ const result = this.parseDeepLinkUrl(url);
94
+ this.deferredDeepLinkData = result;
95
+
96
+ this.log(`Meta deferred deep link fetched: ${url}`);
97
+ return result;
98
+ } catch (error) {
99
+ // This is expected to fail in some scenarios - log but don't treat as error
100
+ this.log('Could not fetch Meta deferred deep link (may not be available)');
101
+ return null;
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Parse deep link URL to extract attribution parameters
107
+ */
108
+ private parseDeepLinkUrl(urlString: string): DeferredDeepLinkResult {
109
+ const result: DeferredDeepLinkResult = {
110
+ url: urlString,
111
+ source: 'meta',
112
+ };
113
+
114
+ try {
115
+ const url = new URL(urlString);
116
+ const params = url.searchParams;
117
+
118
+ // Extract known parameters
119
+ if (params.get('fbclid')) result.fbclid = params.get('fbclid')!;
120
+ if (params.get('utm_source')) result.utmSource = params.get('utm_source')!;
121
+ if (params.get('utm_medium')) result.utmMedium = params.get('utm_medium')!;
122
+ if (params.get('utm_campaign')) result.utmCampaign = params.get('utm_campaign')!;
123
+ if (params.get('utm_content')) result.utmContent = params.get('utm_content')!;
124
+ if (params.get('utm_term')) result.utmTerm = params.get('utm_term')!;
125
+ if (params.get('campaign_id')) result.campaignId = params.get('campaign_id')!;
126
+ if (params.get('adset_id')) result.adsetId = params.get('adset_id')!;
127
+ if (params.get('ad_id')) result.adId = params.get('ad_id')!;
128
+ } catch (error) {
129
+ this.logError('Failed to parse deep link URL:', error);
130
+ }
131
+
132
+ return result;
133
+ }
134
+
135
+ /**
136
+ * Log purchase event to Meta
137
+ */
138
+ async logPurchase(value: number, currency: string, parameters?: Record<string, any>): Promise<void> {
139
+ if (!this.available || !this.initialized) return;
140
+ if (this.config?.enableAppEvents === false) return;
141
+
142
+ try {
143
+ await MetaNativeBridge.logPurchase(value, currency, parameters);
144
+ this.log(`Meta purchase event logged: ${value} ${currency}`);
145
+ } catch (error) {
146
+ this.logError('Failed to log Meta purchase:', error);
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Log custom event to Meta
152
+ */
153
+ async logEvent(eventName: string, valueToSum?: number, parameters?: Record<string, any>): Promise<void> {
154
+ if (!this.available || !this.initialized) return;
155
+ if (this.config?.enableAppEvents === false) return;
156
+
157
+ try {
158
+ await MetaNativeBridge.logEvent(eventName, valueToSum, parameters);
159
+ this.log(`Meta event logged: ${eventName}`);
160
+ } catch (error) {
161
+ this.logError('Failed to log Meta event:', error);
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Set user data for Advanced Matching (improves conversion attribution)
167
+ * Note: Meta's Advanced Matching uses these specific fields - externalId is not supported
168
+ */
169
+ async setUserData(userData: {
170
+ email?: string;
171
+ firstName?: string;
172
+ lastName?: string;
173
+ phone?: string;
174
+ dateOfBirth?: string;
175
+ gender?: string;
176
+ city?: string;
177
+ state?: string;
178
+ zip?: string;
179
+ country?: string;
180
+ }): Promise<void> {
181
+ if (!this.available || !this.initialized) return;
182
+
183
+ try {
184
+ await MetaNativeBridge.setUserData(userData);
185
+ this.log('Meta user data set for Advanced Matching');
186
+ } catch (error) {
187
+ this.logError('Failed to set Meta user data:', error);
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Clear user data (call on logout)
193
+ */
194
+ async clearUserData(): Promise<void> {
195
+ if (!this.available || !this.initialized) return;
196
+
197
+ try {
198
+ await MetaNativeBridge.clearUserData();
199
+ this.log('Meta user data cleared');
200
+ } catch (error) {
201
+ this.logError('Failed to clear Meta user data:', error);
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Get cached deferred deep link data
207
+ */
208
+ getDeferredDeepLinkData(): DeferredDeepLinkResult | null {
209
+ return this.deferredDeepLinkData;
210
+ }
211
+
212
+ /**
213
+ * Check if Meta SDK is available and initialized
214
+ */
215
+ isAvailable(): boolean {
216
+ return this.available && this.initialized;
217
+ }
218
+
219
+ /**
220
+ * Check if Meta SDK native module is installed
221
+ */
222
+ isInstalled(): boolean {
223
+ return this.available;
224
+ }
225
+
226
+ private log(message: string, data?: any): void {
227
+ if (this.debug) {
228
+ console.log(`[Datalyr/Meta] ${message}`, data || '');
229
+ }
230
+ }
231
+
232
+ private logError(message: string, error: any): void {
233
+ console.error(`[Datalyr/Meta] ${message}`, error);
234
+ }
235
+ }
236
+
237
+ // Export singleton instance
238
+ export const metaIntegration = new MetaIntegration();
@@ -0,0 +1,360 @@
1
+ /**
2
+ * TikTok Business SDK Integration
3
+ * Uses bundled native iOS SDK for event forwarding and user identification
4
+ */
5
+
6
+ import { Platform } from 'react-native';
7
+ import { TikTokConfig } from '../types';
8
+ import { TikTokNativeBridge, isNativeModuleAvailable } from '../native/DatalyrNativeBridge';
9
+
10
+ /**
11
+ * TikTok standard event names (current as of 2025)
12
+ * Note: CompletePayment and SubmitForm are legacy, use Purchase and Lead instead
13
+ */
14
+ const TikTokEvents = {
15
+ // Commerce events
16
+ PURCHASE: 'Purchase',
17
+ ADD_TO_CART: 'AddToCart',
18
+ ADD_TO_WISHLIST: 'AddToWishlist',
19
+ INITIATE_CHECKOUT: 'InitiateCheckout',
20
+ ADD_PAYMENT_INFO: 'AddPaymentInfo',
21
+ VIEW_CONTENT: 'ViewContent',
22
+ SEARCH: 'Search',
23
+
24
+ // User events
25
+ COMPLETE_REGISTRATION: 'CompleteRegistration',
26
+ SUBSCRIBE: 'Subscribe',
27
+ START_TRIAL: 'StartTrial',
28
+ LEAD: 'Lead',
29
+ CONTACT: 'Contact',
30
+
31
+ // App events
32
+ DOWNLOAD: 'Download',
33
+ SCHEDULE: 'Schedule',
34
+ SUBMIT_APPLICATION: 'SubmitApplication',
35
+ } as const;
36
+
37
+ /**
38
+ * TikTok Integration class for handling TikTok Business SDK operations
39
+ * Uses native iOS SDK bundled via CocoaPods (no additional npm packages required)
40
+ */
41
+ export class TikTokIntegration {
42
+ private config: TikTokConfig | null = null;
43
+ private initialized: boolean = false;
44
+ private available: boolean = false;
45
+ private debug: boolean = false;
46
+
47
+ /**
48
+ * Initialize TikTok SDK with configuration
49
+ */
50
+ async initialize(config: TikTokConfig, debug: boolean = false): Promise<void> {
51
+ this.debug = debug;
52
+ this.config = config;
53
+
54
+ // Only available on iOS via native module
55
+ if (Platform.OS !== 'ios') {
56
+ this.log('TikTok SDK only available on iOS');
57
+ return;
58
+ }
59
+
60
+ this.available = isNativeModuleAvailable();
61
+
62
+ if (!this.available) {
63
+ this.log('TikTok native module not available');
64
+ return;
65
+ }
66
+
67
+ try {
68
+ const success = await TikTokNativeBridge.initialize(
69
+ config.appId,
70
+ config.tiktokAppId,
71
+ config.accessToken,
72
+ debug
73
+ );
74
+
75
+ if (success) {
76
+ this.initialized = true;
77
+ this.log(`TikTok SDK initialized with App ID: ${config.tiktokAppId}`);
78
+ }
79
+ } catch (error) {
80
+ this.logError('Failed to initialize TikTok SDK:', error);
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Update tracking authorization status (call after ATT prompt)
86
+ */
87
+ async updateTrackingAuthorization(enabled: boolean): Promise<void> {
88
+ if (!this.available || !this.initialized) return;
89
+
90
+ try {
91
+ // TikTok SDK auto-handles ATT, but we track the event
92
+ if (enabled) {
93
+ await this.trackEvent('ATTAuthorized');
94
+ }
95
+ await TikTokNativeBridge.updateTrackingAuthorization(enabled);
96
+ this.log(`TikTok ATT status: ${enabled ? 'authorized' : 'not authorized'}`);
97
+ } catch (error) {
98
+ this.logError('Failed to update TikTok tracking authorization:', error);
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Log purchase event to TikTok (uses Purchase - TikTok's current standard event)
104
+ */
105
+ async logPurchase(
106
+ value: number,
107
+ currency: string,
108
+ contentId?: string,
109
+ contentType?: string,
110
+ parameters?: Record<string, any>
111
+ ): Promise<void> {
112
+ if (!this.available || !this.initialized) return;
113
+ if (this.config?.enableAppEvents === false) return;
114
+
115
+ try {
116
+ const properties: Record<string, any> = {
117
+ value,
118
+ currency,
119
+ ...parameters,
120
+ };
121
+
122
+ if (contentId) properties.content_id = contentId;
123
+ if (contentType) properties.content_type = contentType;
124
+
125
+ await TikTokNativeBridge.trackEvent(TikTokEvents.PURCHASE, undefined, properties);
126
+ this.log(`TikTok Purchase event logged: ${value} ${currency}`);
127
+ } catch (error) {
128
+ this.logError('Failed to log TikTok purchase:', error);
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Log subscription event to TikTok
134
+ */
135
+ async logSubscription(value: number, currency: string, plan?: string): Promise<void> {
136
+ if (!this.available || !this.initialized) return;
137
+ if (this.config?.enableAppEvents === false) return;
138
+
139
+ try {
140
+ const properties: Record<string, any> = {
141
+ value,
142
+ currency,
143
+ };
144
+
145
+ if (plan) {
146
+ properties.content_id = plan;
147
+ properties.content_type = 'subscription';
148
+ }
149
+
150
+ await TikTokNativeBridge.trackEvent(TikTokEvents.SUBSCRIBE, undefined, properties);
151
+ this.log(`TikTok subscription event logged: ${value} ${currency}`);
152
+ } catch (error) {
153
+ this.logError('Failed to log TikTok subscription:', error);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Log add to cart event
159
+ */
160
+ async logAddToCart(value: number, currency: string, contentId?: string, contentType?: string): Promise<void> {
161
+ if (!this.available || !this.initialized) return;
162
+ if (this.config?.enableAppEvents === false) return;
163
+
164
+ try {
165
+ const properties: Record<string, any> = {
166
+ value,
167
+ currency,
168
+ };
169
+
170
+ if (contentId) properties.content_id = contentId;
171
+ if (contentType) properties.content_type = contentType;
172
+
173
+ await TikTokNativeBridge.trackEvent(TikTokEvents.ADD_TO_CART, undefined, properties);
174
+ this.log('TikTok add to cart event logged');
175
+ } catch (error) {
176
+ this.logError('Failed to log TikTok add to cart:', error);
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Log view content event
182
+ */
183
+ async logViewContent(contentId?: string, contentName?: string, contentType?: string): Promise<void> {
184
+ if (!this.available || !this.initialized) return;
185
+ if (this.config?.enableAppEvents === false) return;
186
+
187
+ try {
188
+ const properties: Record<string, any> = {};
189
+ if (contentId) properties.content_id = contentId;
190
+ if (contentName) properties.content_name = contentName;
191
+ if (contentType) properties.content_type = contentType;
192
+
193
+ await TikTokNativeBridge.trackEvent(TikTokEvents.VIEW_CONTENT, undefined, properties);
194
+ this.log('TikTok view content event logged');
195
+ } catch (error) {
196
+ this.logError('Failed to log TikTok view content:', error);
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Log initiate checkout event
202
+ */
203
+ async logInitiateCheckout(value: number, currency: string, numItems?: number): Promise<void> {
204
+ if (!this.available || !this.initialized) return;
205
+ if (this.config?.enableAppEvents === false) return;
206
+
207
+ try {
208
+ const properties: Record<string, any> = {
209
+ value,
210
+ currency,
211
+ };
212
+ if (numItems) properties.quantity = numItems;
213
+
214
+ await TikTokNativeBridge.trackEvent(TikTokEvents.INITIATE_CHECKOUT, undefined, properties);
215
+ this.log('TikTok initiate checkout event logged');
216
+ } catch (error) {
217
+ this.logError('Failed to log TikTok initiate checkout:', error);
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Log complete registration event
223
+ */
224
+ async logCompleteRegistration(method?: string): Promise<void> {
225
+ if (!this.available || !this.initialized) return;
226
+ if (this.config?.enableAppEvents === false) return;
227
+
228
+ try {
229
+ const properties: Record<string, any> = {};
230
+ if (method) properties.registration_method = method;
231
+
232
+ await TikTokNativeBridge.trackEvent(TikTokEvents.COMPLETE_REGISTRATION, undefined, properties);
233
+ this.log('TikTok complete registration event logged');
234
+ } catch (error) {
235
+ this.logError('Failed to log TikTok complete registration:', error);
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Log search event
241
+ */
242
+ async logSearch(query: string): Promise<void> {
243
+ if (!this.available || !this.initialized) return;
244
+ if (this.config?.enableAppEvents === false) return;
245
+
246
+ try {
247
+ await TikTokNativeBridge.trackEvent(TikTokEvents.SEARCH, undefined, { query });
248
+ this.log('TikTok search event logged');
249
+ } catch (error) {
250
+ this.logError('Failed to log TikTok search:', error);
251
+ }
252
+ }
253
+
254
+ /**
255
+ * Log lead event (uses Lead - current standard, SubmitForm is legacy)
256
+ */
257
+ async logLead(value?: number, currency?: string): Promise<void> {
258
+ if (!this.available || !this.initialized) return;
259
+ if (this.config?.enableAppEvents === false) return;
260
+
261
+ try {
262
+ const properties: Record<string, any> = {};
263
+ if (value !== undefined) properties.value = value;
264
+ if (currency) properties.currency = currency;
265
+
266
+ await TikTokNativeBridge.trackEvent(TikTokEvents.LEAD, undefined, properties);
267
+ this.log('TikTok lead event logged');
268
+ } catch (error) {
269
+ this.logError('Failed to log TikTok lead:', error);
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Log add payment info event
275
+ */
276
+ async logAddPaymentInfo(success: boolean): Promise<void> {
277
+ if (!this.available || !this.initialized) return;
278
+ if (this.config?.enableAppEvents === false) return;
279
+
280
+ try {
281
+ await TikTokNativeBridge.trackEvent(TikTokEvents.ADD_PAYMENT_INFO, undefined, { success });
282
+ this.log('TikTok add payment info event logged');
283
+ } catch (error) {
284
+ this.logError('Failed to log TikTok add payment info:', error);
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Log custom event to TikTok
290
+ */
291
+ async trackEvent(eventName: string, properties?: Record<string, any>): Promise<void> {
292
+ if (!this.available || !this.initialized) return;
293
+ if (this.config?.enableAppEvents === false) return;
294
+
295
+ try {
296
+ await TikTokNativeBridge.trackEvent(eventName, undefined, properties || {});
297
+ this.log(`TikTok event logged: ${eventName}`);
298
+ } catch (error) {
299
+ this.logError('Failed to log TikTok event:', error);
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Identify user for improved attribution matching
305
+ */
306
+ async identify(email?: string, phone?: string, externalId?: string): Promise<void> {
307
+ if (!this.available || !this.initialized) return;
308
+
309
+ // Only call identify if we have at least one value
310
+ if (!email && !phone && !externalId) return;
311
+
312
+ try {
313
+ await TikTokNativeBridge.identify(externalId, email, phone);
314
+ this.log('TikTok user identification set');
315
+ } catch (error) {
316
+ this.logError('Failed to identify TikTok user:', error);
317
+ }
318
+ }
319
+
320
+ /**
321
+ * Clear user session (call on logout)
322
+ */
323
+ async logout(): Promise<void> {
324
+ if (!this.available || !this.initialized) return;
325
+
326
+ try {
327
+ await TikTokNativeBridge.logout();
328
+ this.log('TikTok user logged out');
329
+ } catch (error) {
330
+ this.logError('Failed to logout TikTok user:', error);
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Check if TikTok SDK is available and initialized
336
+ */
337
+ isAvailable(): boolean {
338
+ return this.available && this.initialized;
339
+ }
340
+
341
+ /**
342
+ * Check if TikTok SDK native module is installed
343
+ */
344
+ isInstalled(): boolean {
345
+ return this.available;
346
+ }
347
+
348
+ private log(message: string, data?: any): void {
349
+ if (this.debug) {
350
+ console.log(`[Datalyr/TikTok] ${message}`, data || '');
351
+ }
352
+ }
353
+
354
+ private logError(message: string, error: any): void {
355
+ console.error(`[Datalyr/TikTok] ${message}`, error);
356
+ }
357
+ }
358
+
359
+ // Export singleton instance
360
+ export const tiktokIntegration = new TikTokIntegration();