@datalyr/react-native 1.2.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +145 -9
- package/android/build.gradle +54 -0
- package/android/src/main/AndroidManifest.xml +14 -0
- package/android/src/main/java/com/datalyr/reactnative/DatalyrNativeModule.java +423 -0
- package/android/src/main/java/com/datalyr/reactnative/DatalyrPackage.java +30 -0
- package/android/src/main/java/com/datalyr/reactnative/DatalyrPlayInstallReferrerModule.java +229 -0
- package/datalyr-react-native.podspec +2 -2
- package/ios/DatalyrSKAdNetwork.m +400 -1
- package/ios/PrivacyInfo.xcprivacy +48 -0
- package/lib/ConversionValueEncoder.d.ts +13 -1
- package/lib/ConversionValueEncoder.js +57 -23
- package/lib/datalyr-sdk.d.ts +31 -2
- package/lib/datalyr-sdk.js +138 -30
- package/lib/index.d.ts +5 -1
- package/lib/index.js +4 -1
- package/lib/integrations/index.d.ts +3 -1
- package/lib/integrations/index.js +2 -1
- package/lib/integrations/meta-integration.d.ts +1 -0
- package/lib/integrations/meta-integration.js +4 -3
- package/lib/integrations/play-install-referrer.d.ts +78 -0
- package/lib/integrations/play-install-referrer.js +166 -0
- package/lib/integrations/tiktok-integration.d.ts +1 -0
- package/lib/integrations/tiktok-integration.js +4 -3
- package/lib/journey.d.ts +106 -0
- package/lib/journey.js +258 -0
- package/lib/native/DatalyrNativeBridge.d.ts +42 -3
- package/lib/native/DatalyrNativeBridge.js +63 -9
- package/lib/native/SKAdNetworkBridge.d.ts +142 -0
- package/lib/native/SKAdNetworkBridge.js +328 -0
- package/lib/network-status.d.ts +84 -0
- package/lib/network-status.js +281 -0
- package/lib/types.d.ts +51 -0
- package/lib/utils.d.ts +6 -1
- package/lib/utils.js +52 -2
- package/package.json +13 -4
- package/src/ConversionValueEncoder.ts +67 -26
- package/src/datalyr-sdk-expo.ts +55 -6
- package/src/datalyr-sdk.ts +161 -38
- package/src/expo.ts +4 -0
- package/src/index.ts +7 -1
- package/src/integrations/index.ts +3 -1
- package/src/integrations/meta-integration.ts +4 -3
- package/src/integrations/play-install-referrer.ts +218 -0
- package/src/integrations/tiktok-integration.ts +4 -3
- package/src/journey.ts +338 -0
- package/src/native/DatalyrNativeBridge.ts +99 -13
- package/src/native/SKAdNetworkBridge.ts +481 -2
- package/src/network-status.ts +312 -0
- package/src/types.ts +74 -6
- package/src/utils.ts +62 -6
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google Play Install Referrer Integration
|
|
3
|
+
*
|
|
4
|
+
* Provides Android install attribution via the Google Play Install Referrer API.
|
|
5
|
+
* This captures UTM parameters and click IDs passed through Google Play Store.
|
|
6
|
+
*
|
|
7
|
+
* How it works:
|
|
8
|
+
* 1. User clicks ad/link with UTM parameters
|
|
9
|
+
* 2. Google Play Store stores the referrer URL
|
|
10
|
+
* 3. On first app launch, SDK retrieves the referrer
|
|
11
|
+
* 4. Attribution data (utm_source, utm_medium, gclid, etc.) is extracted
|
|
12
|
+
*
|
|
13
|
+
* Requirements:
|
|
14
|
+
* - Android only (returns null on iOS)
|
|
15
|
+
* - Requires Google Play Install Referrer Library in build.gradle:
|
|
16
|
+
* implementation 'com.android.installreferrer:installreferrer:2.2'
|
|
17
|
+
*
|
|
18
|
+
* Attribution data captured:
|
|
19
|
+
* - referrer_url: Full referrer URL from Play Store
|
|
20
|
+
* - referrer_click_timestamp: When the referrer link was clicked
|
|
21
|
+
* - install_begin_timestamp: When the install began
|
|
22
|
+
* - gclid: Google Ads click ID (standard)
|
|
23
|
+
* - gbraid: Google Ads privacy-safe click ID (iOS App campaigns)
|
|
24
|
+
* - wbraid: Google Ads privacy-safe click ID (Web-to-App campaigns)
|
|
25
|
+
* - utm_source, utm_medium, utm_campaign, etc.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { Platform, NativeModules } from 'react-native';
|
|
29
|
+
import { debugLog, errorLog } from '../utils';
|
|
30
|
+
|
|
31
|
+
export interface PlayInstallReferrer {
|
|
32
|
+
// Raw referrer URL from Play Store
|
|
33
|
+
referrerUrl: string;
|
|
34
|
+
// Timestamp when the referrer link was clicked (ms)
|
|
35
|
+
referrerClickTimestamp: number;
|
|
36
|
+
// Timestamp when the install began (ms)
|
|
37
|
+
installBeginTimestamp: number;
|
|
38
|
+
// Timestamp when install was completed (ms)
|
|
39
|
+
installCompleteTimestamp?: number;
|
|
40
|
+
// Google Ads click ID
|
|
41
|
+
gclid?: string;
|
|
42
|
+
// Google Ads privacy-safe click IDs (iOS App campaigns)
|
|
43
|
+
gbraid?: string;
|
|
44
|
+
// Google Ads privacy-safe click IDs (Web-to-App campaigns)
|
|
45
|
+
wbraid?: string;
|
|
46
|
+
// UTM Parameters
|
|
47
|
+
utmSource?: string;
|
|
48
|
+
utmMedium?: string;
|
|
49
|
+
utmCampaign?: string;
|
|
50
|
+
utmTerm?: string;
|
|
51
|
+
utmContent?: string;
|
|
52
|
+
// Additional parameters
|
|
53
|
+
[key: string]: any;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface PlayInstallReferrerModule {
|
|
57
|
+
getInstallReferrer(): Promise<PlayInstallReferrer | null>;
|
|
58
|
+
isAvailable(): Promise<boolean>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const { DatalyrPlayInstallReferrer } = NativeModules as {
|
|
62
|
+
DatalyrPlayInstallReferrer?: PlayInstallReferrerModule;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Google Play Install Referrer Integration
|
|
67
|
+
*
|
|
68
|
+
* Retrieves install attribution data from Google Play Store.
|
|
69
|
+
* Only available on Android.
|
|
70
|
+
*/
|
|
71
|
+
class PlayInstallReferrerIntegration {
|
|
72
|
+
private referrerData: PlayInstallReferrer | null = null;
|
|
73
|
+
private initialized = false;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Check if Play Install Referrer is available
|
|
77
|
+
*/
|
|
78
|
+
isAvailable(): boolean {
|
|
79
|
+
return Platform.OS === 'android' && !!DatalyrPlayInstallReferrer;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Initialize and fetch install referrer data
|
|
84
|
+
* Should be called once on first app launch
|
|
85
|
+
*/
|
|
86
|
+
async initialize(): Promise<void> {
|
|
87
|
+
if (this.initialized) return;
|
|
88
|
+
|
|
89
|
+
if (!this.isAvailable()) {
|
|
90
|
+
debugLog('[PlayInstallReferrer] Not available (iOS or native module missing)');
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
this.referrerData = await this.fetchInstallReferrer();
|
|
96
|
+
this.initialized = true;
|
|
97
|
+
|
|
98
|
+
if (this.referrerData) {
|
|
99
|
+
debugLog('[PlayInstallReferrer] Install referrer fetched:', {
|
|
100
|
+
utmSource: this.referrerData.utmSource,
|
|
101
|
+
utmMedium: this.referrerData.utmMedium,
|
|
102
|
+
hasGclid: !!this.referrerData.gclid,
|
|
103
|
+
hasGbraid: !!this.referrerData.gbraid,
|
|
104
|
+
hasWbraid: !!this.referrerData.wbraid,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
} catch (error) {
|
|
108
|
+
errorLog('[PlayInstallReferrer] Failed to initialize:', error as Error);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Fetch install referrer from Play Store
|
|
114
|
+
*/
|
|
115
|
+
async fetchInstallReferrer(): Promise<PlayInstallReferrer | null> {
|
|
116
|
+
if (!this.isAvailable()) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const referrer = await DatalyrPlayInstallReferrer!.getInstallReferrer();
|
|
122
|
+
|
|
123
|
+
if (!referrer) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Parse UTM parameters from referrer URL
|
|
128
|
+
const parsed = this.parseReferrerUrl(referrer.referrerUrl);
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
...referrer,
|
|
132
|
+
...parsed,
|
|
133
|
+
};
|
|
134
|
+
} catch (error) {
|
|
135
|
+
errorLog('[PlayInstallReferrer] Error fetching referrer:', error as Error);
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Parse referrer URL to extract UTM parameters and click IDs
|
|
142
|
+
*/
|
|
143
|
+
private parseReferrerUrl(referrerUrl: string): Partial<PlayInstallReferrer> {
|
|
144
|
+
const params: Partial<PlayInstallReferrer> = {};
|
|
145
|
+
|
|
146
|
+
if (!referrerUrl) return params;
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
// Referrer URL is URL-encoded, decode it first
|
|
150
|
+
const decoded = decodeURIComponent(referrerUrl);
|
|
151
|
+
const searchParams = new URLSearchParams(decoded);
|
|
152
|
+
|
|
153
|
+
// Extract UTM parameters
|
|
154
|
+
params.utmSource = searchParams.get('utm_source') || undefined;
|
|
155
|
+
params.utmMedium = searchParams.get('utm_medium') || undefined;
|
|
156
|
+
params.utmCampaign = searchParams.get('utm_campaign') || undefined;
|
|
157
|
+
params.utmTerm = searchParams.get('utm_term') || undefined;
|
|
158
|
+
params.utmContent = searchParams.get('utm_content') || undefined;
|
|
159
|
+
|
|
160
|
+
// Extract click IDs (gclid, gbraid, wbraid)
|
|
161
|
+
params.gclid = searchParams.get('gclid') || undefined;
|
|
162
|
+
params.gbraid = searchParams.get('gbraid') || undefined;
|
|
163
|
+
params.wbraid = searchParams.get('wbraid') || undefined;
|
|
164
|
+
|
|
165
|
+
// Store any additional parameters
|
|
166
|
+
const knownParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gclid', 'gbraid', 'wbraid'];
|
|
167
|
+
searchParams.forEach((value, key) => {
|
|
168
|
+
if (!knownParams.includes(key) && !key.startsWith('utm_')) {
|
|
169
|
+
params[key] = value;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
debugLog('[PlayInstallReferrer] Parsed referrer URL:', params);
|
|
174
|
+
} catch (error) {
|
|
175
|
+
errorLog('[PlayInstallReferrer] Error parsing referrer URL:', error as Error);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return params;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Get cached install referrer data
|
|
183
|
+
*/
|
|
184
|
+
getReferrerData(): PlayInstallReferrer | null {
|
|
185
|
+
return this.referrerData;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Get attribution data in standard format
|
|
190
|
+
*/
|
|
191
|
+
getAttributionData(): Record<string, any> {
|
|
192
|
+
if (!this.referrerData) return {};
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
// Install referrer specific
|
|
196
|
+
install_referrer_url: this.referrerData.referrerUrl,
|
|
197
|
+
referrer_click_timestamp: this.referrerData.referrerClickTimestamp,
|
|
198
|
+
install_begin_timestamp: this.referrerData.installBeginTimestamp,
|
|
199
|
+
|
|
200
|
+
// Google Ads click IDs (gclid is standard, gbraid/wbraid are privacy-safe alternatives)
|
|
201
|
+
gclid: this.referrerData.gclid,
|
|
202
|
+
gbraid: this.referrerData.gbraid,
|
|
203
|
+
wbraid: this.referrerData.wbraid,
|
|
204
|
+
|
|
205
|
+
// UTM parameters
|
|
206
|
+
utm_source: this.referrerData.utmSource,
|
|
207
|
+
utm_medium: this.referrerData.utmMedium,
|
|
208
|
+
utm_campaign: this.referrerData.utmCampaign,
|
|
209
|
+
utm_term: this.referrerData.utmTerm,
|
|
210
|
+
utm_content: this.referrerData.utmContent,
|
|
211
|
+
|
|
212
|
+
// Source indicators
|
|
213
|
+
attribution_source: 'play_install_referrer',
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export const playInstallReferrerIntegration = new PlayInstallReferrerIntegration();
|
|
@@ -46,14 +46,15 @@ export class TikTokIntegration {
|
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Initialize TikTok SDK with configuration
|
|
49
|
+
* Supported on both iOS and Android via native modules
|
|
49
50
|
*/
|
|
50
51
|
async initialize(config: TikTokConfig, debug: boolean = false): Promise<void> {
|
|
51
52
|
this.debug = debug;
|
|
52
53
|
this.config = config;
|
|
53
54
|
|
|
54
|
-
// Only available on iOS via native
|
|
55
|
-
if (Platform.OS !== 'ios') {
|
|
56
|
-
this.log('TikTok SDK only available on iOS');
|
|
55
|
+
// Only available on iOS and Android via native modules
|
|
56
|
+
if (Platform.OS !== 'ios' && Platform.OS !== 'android') {
|
|
57
|
+
this.log('TikTok SDK only available on iOS and Android');
|
|
57
58
|
return;
|
|
58
59
|
}
|
|
59
60
|
|
package/src/journey.ts
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Journey Tracking Module for React Native
|
|
3
|
+
* Mirrors the Web SDK's journey tracking capabilities:
|
|
4
|
+
* - First-touch attribution with 90-day expiration
|
|
5
|
+
* - Last-touch attribution with 90-day expiration
|
|
6
|
+
* - Up to 30 touchpoints stored
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Storage, debugLog, errorLog } from './utils';
|
|
10
|
+
|
|
11
|
+
// Storage keys for journey data
|
|
12
|
+
const JOURNEY_STORAGE_KEYS = {
|
|
13
|
+
FIRST_TOUCH: '@datalyr/first_touch',
|
|
14
|
+
LAST_TOUCH: '@datalyr/last_touch',
|
|
15
|
+
JOURNEY: '@datalyr/journey',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// 90-day attribution window (matching web SDK)
|
|
19
|
+
const ATTRIBUTION_WINDOW_MS = 90 * 24 * 60 * 60 * 1000;
|
|
20
|
+
|
|
21
|
+
// Maximum touchpoints to store
|
|
22
|
+
const MAX_TOUCHPOINTS = 30;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Attribution data for a touch
|
|
26
|
+
*/
|
|
27
|
+
export interface TouchAttribution {
|
|
28
|
+
timestamp: number;
|
|
29
|
+
expires_at: number;
|
|
30
|
+
captured_at: number;
|
|
31
|
+
|
|
32
|
+
// Source attribution
|
|
33
|
+
source?: string;
|
|
34
|
+
medium?: string;
|
|
35
|
+
campaign?: string;
|
|
36
|
+
term?: string;
|
|
37
|
+
content?: string;
|
|
38
|
+
|
|
39
|
+
// Click IDs
|
|
40
|
+
clickId?: string;
|
|
41
|
+
clickIdType?: string;
|
|
42
|
+
fbclid?: string;
|
|
43
|
+
gclid?: string;
|
|
44
|
+
ttclid?: string;
|
|
45
|
+
gbraid?: string;
|
|
46
|
+
wbraid?: string;
|
|
47
|
+
|
|
48
|
+
// LYR tag
|
|
49
|
+
lyr?: string;
|
|
50
|
+
|
|
51
|
+
// Context
|
|
52
|
+
landingPage?: string;
|
|
53
|
+
referrer?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A single touchpoint in the customer journey
|
|
58
|
+
*/
|
|
59
|
+
export interface TouchPoint {
|
|
60
|
+
timestamp: number;
|
|
61
|
+
sessionId: string;
|
|
62
|
+
source?: string;
|
|
63
|
+
medium?: string;
|
|
64
|
+
campaign?: string;
|
|
65
|
+
clickIdType?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Journey manager for tracking customer touchpoints
|
|
70
|
+
*/
|
|
71
|
+
export class JourneyManager {
|
|
72
|
+
private firstTouch: TouchAttribution | null = null;
|
|
73
|
+
private lastTouch: TouchAttribution | null = null;
|
|
74
|
+
private journey: TouchPoint[] = [];
|
|
75
|
+
private initialized = false;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Initialize journey tracking by loading persisted data
|
|
79
|
+
*/
|
|
80
|
+
async initialize(): Promise<void> {
|
|
81
|
+
if (this.initialized) return;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
debugLog('Initializing journey manager...');
|
|
85
|
+
|
|
86
|
+
// Load first touch
|
|
87
|
+
const savedFirstTouch = await Storage.getItem<TouchAttribution>(JOURNEY_STORAGE_KEYS.FIRST_TOUCH);
|
|
88
|
+
if (savedFirstTouch && !this.isExpired(savedFirstTouch)) {
|
|
89
|
+
this.firstTouch = savedFirstTouch;
|
|
90
|
+
} else if (savedFirstTouch) {
|
|
91
|
+
// Expired, clear it
|
|
92
|
+
await Storage.removeItem(JOURNEY_STORAGE_KEYS.FIRST_TOUCH);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Load last touch
|
|
96
|
+
const savedLastTouch = await Storage.getItem<TouchAttribution>(JOURNEY_STORAGE_KEYS.LAST_TOUCH);
|
|
97
|
+
if (savedLastTouch && !this.isExpired(savedLastTouch)) {
|
|
98
|
+
this.lastTouch = savedLastTouch;
|
|
99
|
+
} else if (savedLastTouch) {
|
|
100
|
+
await Storage.removeItem(JOURNEY_STORAGE_KEYS.LAST_TOUCH);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Load journey
|
|
104
|
+
const savedJourney = await Storage.getItem<TouchPoint[]>(JOURNEY_STORAGE_KEYS.JOURNEY);
|
|
105
|
+
if (savedJourney) {
|
|
106
|
+
this.journey = savedJourney;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this.initialized = true;
|
|
110
|
+
debugLog('Journey manager initialized', {
|
|
111
|
+
hasFirstTouch: !!this.firstTouch,
|
|
112
|
+
hasLastTouch: !!this.lastTouch,
|
|
113
|
+
touchpointCount: this.journey.length,
|
|
114
|
+
});
|
|
115
|
+
} catch (error) {
|
|
116
|
+
errorLog('Failed to initialize journey manager:', error as Error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Check if attribution has expired
|
|
122
|
+
*/
|
|
123
|
+
private isExpired(attribution: TouchAttribution): boolean {
|
|
124
|
+
return Date.now() >= attribution.expires_at;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Store first touch attribution (only if not already set or expired)
|
|
129
|
+
*/
|
|
130
|
+
async storeFirstTouch(attribution: Partial<TouchAttribution>): Promise<void> {
|
|
131
|
+
try {
|
|
132
|
+
// Only store if no valid first touch exists
|
|
133
|
+
if (this.firstTouch && !this.isExpired(this.firstTouch)) {
|
|
134
|
+
debugLog('First touch already exists, not overwriting');
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const now = Date.now();
|
|
139
|
+
this.firstTouch = {
|
|
140
|
+
...attribution,
|
|
141
|
+
timestamp: attribution.timestamp || now,
|
|
142
|
+
captured_at: now,
|
|
143
|
+
expires_at: now + ATTRIBUTION_WINDOW_MS,
|
|
144
|
+
} as TouchAttribution;
|
|
145
|
+
|
|
146
|
+
await Storage.setItem(JOURNEY_STORAGE_KEYS.FIRST_TOUCH, this.firstTouch);
|
|
147
|
+
debugLog('First touch stored:', this.firstTouch);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
errorLog('Failed to store first touch:', error as Error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Get first touch attribution (null if expired)
|
|
155
|
+
*/
|
|
156
|
+
getFirstTouch(): TouchAttribution | null {
|
|
157
|
+
if (this.firstTouch && this.isExpired(this.firstTouch)) {
|
|
158
|
+
this.firstTouch = null;
|
|
159
|
+
Storage.removeItem(JOURNEY_STORAGE_KEYS.FIRST_TOUCH).catch(() => {});
|
|
160
|
+
}
|
|
161
|
+
return this.firstTouch;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Store last touch attribution (always updates)
|
|
166
|
+
*/
|
|
167
|
+
async storeLastTouch(attribution: Partial<TouchAttribution>): Promise<void> {
|
|
168
|
+
try {
|
|
169
|
+
const now = Date.now();
|
|
170
|
+
this.lastTouch = {
|
|
171
|
+
...attribution,
|
|
172
|
+
timestamp: attribution.timestamp || now,
|
|
173
|
+
captured_at: now,
|
|
174
|
+
expires_at: now + ATTRIBUTION_WINDOW_MS,
|
|
175
|
+
} as TouchAttribution;
|
|
176
|
+
|
|
177
|
+
await Storage.setItem(JOURNEY_STORAGE_KEYS.LAST_TOUCH, this.lastTouch);
|
|
178
|
+
debugLog('Last touch stored:', this.lastTouch);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
errorLog('Failed to store last touch:', error as Error);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Get last touch attribution (null if expired)
|
|
186
|
+
*/
|
|
187
|
+
getLastTouch(): TouchAttribution | null {
|
|
188
|
+
if (this.lastTouch && this.isExpired(this.lastTouch)) {
|
|
189
|
+
this.lastTouch = null;
|
|
190
|
+
Storage.removeItem(JOURNEY_STORAGE_KEYS.LAST_TOUCH).catch(() => {});
|
|
191
|
+
}
|
|
192
|
+
return this.lastTouch;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Add a touchpoint to the customer journey
|
|
197
|
+
*/
|
|
198
|
+
async addTouchpoint(sessionId: string, attribution: Partial<TouchAttribution>): Promise<void> {
|
|
199
|
+
try {
|
|
200
|
+
const touchpoint: TouchPoint = {
|
|
201
|
+
timestamp: Date.now(),
|
|
202
|
+
sessionId,
|
|
203
|
+
source: attribution.source,
|
|
204
|
+
medium: attribution.medium,
|
|
205
|
+
campaign: attribution.campaign,
|
|
206
|
+
clickIdType: attribution.clickIdType,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
this.journey.push(touchpoint);
|
|
210
|
+
|
|
211
|
+
// Keep only last MAX_TOUCHPOINTS
|
|
212
|
+
if (this.journey.length > MAX_TOUCHPOINTS) {
|
|
213
|
+
this.journey = this.journey.slice(-MAX_TOUCHPOINTS);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
await Storage.setItem(JOURNEY_STORAGE_KEYS.JOURNEY, this.journey);
|
|
217
|
+
debugLog('Touchpoint added, total:', this.journey.length);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
errorLog('Failed to add touchpoint:', error as Error);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Get customer journey (all touchpoints)
|
|
225
|
+
*/
|
|
226
|
+
getJourney(): TouchPoint[] {
|
|
227
|
+
return [...this.journey];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Record attribution from a deep link or install
|
|
232
|
+
* Updates first-touch (if not set), last-touch, and adds touchpoint
|
|
233
|
+
*/
|
|
234
|
+
async recordAttribution(sessionId: string, attribution: Partial<TouchAttribution>): Promise<void> {
|
|
235
|
+
// Only process if we have meaningful attribution data
|
|
236
|
+
const hasAttribution = attribution.source || attribution.clickId || attribution.campaign || attribution.lyr;
|
|
237
|
+
|
|
238
|
+
if (!hasAttribution) {
|
|
239
|
+
debugLog('No attribution data to record');
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Store first touch if not set
|
|
244
|
+
if (!this.getFirstTouch()) {
|
|
245
|
+
await this.storeFirstTouch(attribution);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Always update last touch
|
|
249
|
+
await this.storeLastTouch(attribution);
|
|
250
|
+
|
|
251
|
+
// Add touchpoint
|
|
252
|
+
await this.addTouchpoint(sessionId, attribution);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Get attribution data for events (mirrors Web SDK format)
|
|
257
|
+
*/
|
|
258
|
+
getAttributionData(): Record<string, any> {
|
|
259
|
+
const firstTouch = this.getFirstTouch();
|
|
260
|
+
const lastTouch = this.getLastTouch();
|
|
261
|
+
const journey = this.getJourney();
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
// First touch (with snake_case and camelCase aliases)
|
|
265
|
+
first_touch_source: firstTouch?.source,
|
|
266
|
+
first_touch_medium: firstTouch?.medium,
|
|
267
|
+
first_touch_campaign: firstTouch?.campaign,
|
|
268
|
+
first_touch_timestamp: firstTouch?.timestamp,
|
|
269
|
+
firstTouchSource: firstTouch?.source,
|
|
270
|
+
firstTouchMedium: firstTouch?.medium,
|
|
271
|
+
firstTouchCampaign: firstTouch?.campaign,
|
|
272
|
+
|
|
273
|
+
// Last touch
|
|
274
|
+
last_touch_source: lastTouch?.source,
|
|
275
|
+
last_touch_medium: lastTouch?.medium,
|
|
276
|
+
last_touch_campaign: lastTouch?.campaign,
|
|
277
|
+
last_touch_timestamp: lastTouch?.timestamp,
|
|
278
|
+
lastTouchSource: lastTouch?.source,
|
|
279
|
+
lastTouchMedium: lastTouch?.medium,
|
|
280
|
+
lastTouchCampaign: lastTouch?.campaign,
|
|
281
|
+
|
|
282
|
+
// Journey metrics
|
|
283
|
+
touchpoint_count: journey.length,
|
|
284
|
+
touchpointCount: journey.length,
|
|
285
|
+
days_since_first_touch: firstTouch?.timestamp
|
|
286
|
+
? Math.floor((Date.now() - firstTouch.timestamp) / 86400000)
|
|
287
|
+
: 0,
|
|
288
|
+
daysSinceFirstTouch: firstTouch?.timestamp
|
|
289
|
+
? Math.floor((Date.now() - firstTouch.timestamp) / 86400000)
|
|
290
|
+
: 0,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Clear all journey data (for testing/reset)
|
|
296
|
+
*/
|
|
297
|
+
async clearJourney(): Promise<void> {
|
|
298
|
+
this.firstTouch = null;
|
|
299
|
+
this.lastTouch = null;
|
|
300
|
+
this.journey = [];
|
|
301
|
+
|
|
302
|
+
await Promise.all([
|
|
303
|
+
Storage.removeItem(JOURNEY_STORAGE_KEYS.FIRST_TOUCH),
|
|
304
|
+
Storage.removeItem(JOURNEY_STORAGE_KEYS.LAST_TOUCH),
|
|
305
|
+
Storage.removeItem(JOURNEY_STORAGE_KEYS.JOURNEY),
|
|
306
|
+
]);
|
|
307
|
+
|
|
308
|
+
debugLog('Journey data cleared');
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Get journey summary for debugging
|
|
313
|
+
*/
|
|
314
|
+
getJourneySummary(): {
|
|
315
|
+
hasFirstTouch: boolean;
|
|
316
|
+
hasLastTouch: boolean;
|
|
317
|
+
touchpointCount: number;
|
|
318
|
+
daysSinceFirstTouch: number;
|
|
319
|
+
sources: string[];
|
|
320
|
+
} {
|
|
321
|
+
const firstTouch = this.getFirstTouch();
|
|
322
|
+
const journey = this.getJourney();
|
|
323
|
+
const sources = [...new Set(journey.map(t => t.source).filter(Boolean))] as string[];
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
hasFirstTouch: !!firstTouch,
|
|
327
|
+
hasLastTouch: !!this.getLastTouch(),
|
|
328
|
+
touchpointCount: journey.length,
|
|
329
|
+
daysSinceFirstTouch: firstTouch?.timestamp
|
|
330
|
+
? Math.floor((Date.now() - firstTouch.timestamp) / 86400000)
|
|
331
|
+
: 0,
|
|
332
|
+
sources,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Export singleton instance
|
|
338
|
+
export const journeyManager = new JourneyManager();
|