1+ import { Platform } from "react-native" ;
2+
3+ import Constants from "expo-constants" ;
4+ import * as Device from "expo-device" ;
5+
16import { SDK_HEADER_VALUE } from "../../version" ;
27import type { RequiredConfig } from "../types" ;
38
@@ -23,6 +28,35 @@ export type UniversalLinkClickResult =
2328 effectiveLimit ?: number ;
2429 } ;
2530
31+ type ClickPlatform = "ios" | "android" | "unknown" ;
32+
33+ const resolvePlatform = ( ) : ClickPlatform =>
34+ Platform . OS === "ios" || Platform . OS === "android" ? Platform . OS : "unknown" ;
35+
36+ const extractParams = ( url : string ) : Record < string , string > | undefined => {
37+ try {
38+ const { searchParams } = new URL ( url ) ;
39+ const params : Record < string , string > = { } ;
40+ searchParams . forEach ( ( value , key ) => {
41+ params [ key ] = value ;
42+ } ) ;
43+ return Object . keys ( params ) . length > 0 ? params : undefined ;
44+ } catch {
45+ return undefined ;
46+ }
47+ } ;
48+
49+ const buildMetadata = ( ) : Record < string , string > => {
50+ const raw : Record < string , string | null | undefined > = {
51+ os_version : Device . osVersion ,
52+ app_version : Constants . nativeAppVersion ,
53+ device_model : Device . modelName ,
54+ } ;
55+ return Object . fromEntries (
56+ Object . entries ( raw ) . filter ( ( entry ) : entry is [ string , string ] => typeof entry [ 1 ] === "string" ) ,
57+ ) ;
58+ } ;
59+
2660export const sendUniversalLinkClick = async ( {
2761 apiKey : API_KEY ,
2862 appID,
@@ -31,6 +65,9 @@ export const sendUniversalLinkClick = async ({
3165 url : string ;
3266} ) : Promise < UniversalLinkClickResult > => {
3367 try {
68+ const params = extractParams ( url ) ;
69+ const metadata = buildMetadata ( ) ;
70+
3471 const response = await fetch ( API_URL , {
3572 method : "POST" ,
3673 headers : {
@@ -39,7 +76,13 @@ export const sendUniversalLinkClick = async ({
3976 "X-App-ID" : appID ,
4077 "X-SDK" : SDK_HEADER_VALUE ,
4178 } ,
42- body : JSON . stringify ( { url, timestamp : Date . now ( ) } ) ,
79+ body : JSON . stringify ( {
80+ url,
81+ timestamp : Date . now ( ) ,
82+ platform : resolvePlatform ( ) ,
83+ ...( params !== undefined && { params } ) ,
84+ ...( Object . keys ( metadata ) . length > 0 && { metadata } ) ,
85+ } ) ,
4386 } ) ;
4487
4588 let body : UniversalLinkClickResponseBody | null = null ;
0 commit comments