|
1 | 1 | import type { RequiredConfig } from '../types'; |
2 | | -import { getProbabilisticFingerprint } from '../utils/fingerprint'; |
| 2 | +import { |
| 3 | + getDeterministicFingerprint, |
| 4 | + getProbabilisticFingerprint, |
| 5 | + type DeterministicFingerprint, |
| 6 | + type ProbabilisticFingerprint, |
| 7 | +} from '../utils/fingerprint'; |
| 8 | +import * as Application from 'expo-application'; |
3 | 9 |
|
4 | 10 | const API_URL = 'https://detour-poc.vercel.app/api/link/match-link'; |
5 | 11 |
|
| 12 | +const sendFingerprint = async ({ |
| 13 | + API_KEY, |
| 14 | + appID, |
| 15 | + requestBody, |
| 16 | +}: { |
| 17 | + API_KEY: string; |
| 18 | + appID: string; |
| 19 | + requestBody: ProbabilisticFingerprint | DeterministicFingerprint; |
| 20 | +}): Promise<Response> => { |
| 21 | + const response = await fetch(API_URL, { |
| 22 | + method: 'POST', |
| 23 | + headers: { |
| 24 | + 'Content-Type': 'application/json', |
| 25 | + 'Authorization': `Bearer ${API_KEY}`, |
| 26 | + 'X-App-ID': appID, |
| 27 | + }, |
| 28 | + body: JSON.stringify(requestBody), |
| 29 | + }); |
| 30 | + |
| 31 | + return response; |
| 32 | +}; |
| 33 | + |
6 | 34 | export const getDeferredLink = async ({ |
7 | 35 | API_KEY, |
8 | 36 | appID, |
9 | 37 | shouldUseClipboard, |
10 | 38 | }: RequiredConfig) => { |
11 | | - const probabilisticFingerprint = |
12 | | - await getProbabilisticFingerprint(shouldUseClipboard); |
| 39 | + const referrer = await Application.getInstallReferrerAsync(); |
| 40 | + const matchClickId = referrer.match(/(?:^|&)click_id=([^&]+)/); |
| 41 | + const referrerClickId = matchClickId ? matchClickId[1] : null; |
13 | 42 |
|
14 | | - try { |
15 | | - const response = await fetch(API_URL, { |
16 | | - method: 'POST', |
17 | | - headers: { |
18 | | - 'Content-Type': 'application/json', |
19 | | - 'Authorization': `Bearer ${API_KEY}`, |
20 | | - 'X-App-ID': appID, |
21 | | - }, |
22 | | - body: JSON.stringify(probabilisticFingerprint), |
| 43 | + let response; |
| 44 | + if (referrerClickId?.length) { |
| 45 | + response = await sendFingerprint({ |
| 46 | + API_KEY, |
| 47 | + appID, |
| 48 | + requestBody: getDeterministicFingerprint(referrerClickId), |
23 | 49 | }); |
| 50 | + } else { |
| 51 | + const probabilisticFingerprint = |
| 52 | + await getProbabilisticFingerprint(shouldUseClipboard); |
24 | 53 |
|
| 54 | + response = await sendFingerprint({ |
| 55 | + API_KEY, |
| 56 | + appID, |
| 57 | + requestBody: probabilisticFingerprint, |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + try { |
25 | 62 | if (!response.ok) { |
26 | 63 | throw new Error('Failed to fetch deferred link'); |
27 | 64 | } |
28 | 65 |
|
29 | 66 | const data = await response.json(); |
30 | | - |
31 | 67 | return data.link || null; |
32 | 68 | } catch (error) { |
33 | 69 | console.error('Error fetching deferred link:', error); |
|
0 commit comments