@traqio/react-native 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/TraqioExpo.podspec +30 -0
- package/android/build.gradle +57 -0
- package/android/src/main/AndroidManifest.xml +12 -0
- package/android/src/main/java/io/khufu/traqio/expo/AdvertisingId.kt +34 -0
- package/android/src/main/java/io/khufu/traqio/expo/InstallReferrerReader.kt +58 -0
- package/android/src/main/java/io/khufu/traqio/expo/ReferrerFetch.kt +211 -0
- package/android/src/main/java/io/khufu/traqio/expo/TraqioModule.kt +68 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +26 -0
- package/build/index.js.map +1 -0
- package/build/native.d.ts +20 -0
- package/build/native.d.ts.map +1 -0
- package/build/native.js +115 -0
- package/build/native.js.map +1 -0
- package/expo-module.config.json +5 -0
- package/ios/TraqioAdvertisingId.swift +50 -0
- package/ios/TraqioModule.swift +75 -0
- package/ios/TraqioSkan.swift +109 -0
- package/package.json +73 -3
- package/src/index.ts +6 -0
- package/src/native.ts +122 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Khufu FZCO
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @traqio/react-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The [Traqio](https://traqio.app) SDK for React Native: mobile install attribution and
|
|
4
|
+
ROAS for Meta and TikTok campaigns. On Android it reads the Google Play Install
|
|
5
|
+
Referrer — deterministic attribution, no advertising id by default (opt-in, below). On iOS it registers installs
|
|
6
|
+
with SKAdNetwork, sets conversion values from the mapping you publish, and reports the
|
|
7
|
+
Apple Ads attribution token. It queues your events offline and resolves deep links.
|
|
8
|
+
|
|
9
|
+
One package: an autolinked native module (Kotlin + Swift) and the JavaScript engine
|
|
10
|
+
[`@traqio/core`](https://www.npmjs.com/package/@traqio/core). For an Expo app, use the
|
|
11
|
+
alias [`@traqio/expo`](https://www.npmjs.com/package/@traqio/expo) — the same module —
|
|
12
|
+
and never both.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
The native half is an Expo module, so a bare React Native project needs Expo Modules
|
|
17
|
+
once:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npx install-expo-modules@latest
|
|
21
|
+
npm install @traqio/react-native @react-native-async-storage/async-storage
|
|
22
|
+
cd ios && pod install
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
AsyncStorage is how the SDK reports the install exactly once and keeps queued events
|
|
26
|
+
across restarts.
|
|
27
|
+
|
|
28
|
+
## Initialize
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// index.js — before AppRegistry.registerComponent
|
|
32
|
+
import { Traqio, getNativeBridge } from "@traqio/react-native";
|
|
33
|
+
|
|
34
|
+
Traqio.init({
|
|
35
|
+
sdkKey: "YOUR_SDK_KEY", // from the Traqio console — public by design
|
|
36
|
+
endpoint: "https://api.traqio.app", // origin only
|
|
37
|
+
native: getNativeBridge(), // Install Referrer (Android), SKAdNetwork + Apple Ads (iOS)
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
On iOS, declare Traqio as the SKAdNetwork postback endpoint in `Info.plist`:
|
|
42
|
+
|
|
43
|
+
```xml
|
|
44
|
+
<key>NSAdvertisingAttributionReportEndpoint</key>
|
|
45
|
+
<string>https://api.traqio.app</string>
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Track events
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
Traqio.setUserId(user.id); // the same id you give RevenueCat (app_user_id)
|
|
52
|
+
Traqio.track("purchase", { value: 9.99, currency: "EUR", transactionId: purchase.transactionId });
|
|
53
|
+
|
|
54
|
+
Traqio.onDeepLink(({ deepLink }) => {
|
|
55
|
+
if (deepLink) navigateTo(deepLink); // direct opens and the deferred link of a fresh install
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Advertising id (opt-in)
|
|
60
|
+
|
|
61
|
+
Off by default (`collectAdvertisingId: false`): the SDK reads no IDFA / GAID and never
|
|
62
|
+
shows the App Tracking Transparency prompt. Turn it on when you buy on a network that
|
|
63
|
+
attributes by that id (**Unity Ads**, **AppLovin**):
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
Traqio.init({ sdkKey: "SDK_KEY", endpoint: "https://api.traqio.app", collectAdvertisingId: true });
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
- **iOS**: ask ATT in your app before `init` and link AdSupport; the IDFA is read only if
|
|
70
|
+
the user said yes.
|
|
71
|
+
- **Android**: add `com.google.android.gms:play-services-ads-identifier` and the
|
|
72
|
+
`com.google.android.gms.permission.AD_ID` permission; nothing is sent when the user
|
|
73
|
+
limited ad tracking.
|
|
74
|
+
|
|
75
|
+
It is sent with the install only, matched hashed, and never used as the user's identity.
|
|
76
|
+
|
|
77
|
+
## Requirements
|
|
78
|
+
|
|
79
|
+
- React Native 0.74+, with Expo Modules installed.
|
|
80
|
+
- iOS 15.1+. Android: no permission needed.
|
|
81
|
+
- Not Expo Go — a development build or a release build.
|
|
82
|
+
|
|
83
|
+
## Documentation
|
|
84
|
+
|
|
85
|
+
Setup, deep links, platform notes: **https://traqio.app/docs/sdks/react-native** ·
|
|
86
|
+
full API: https://traqio.app/docs/sdks/javascript
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
# Named TraqioExpo (not Traqio) so it can't collide with the standalone iOS pod
|
|
6
|
+
# shipped from packages/sdk-ios. Expo autolinking picks this up from node_modules.
|
|
7
|
+
Pod::Spec.new do |s|
|
|
8
|
+
s.name = 'TraqioExpo'
|
|
9
|
+
s.version = package['version']
|
|
10
|
+
s.summary = package['description']
|
|
11
|
+
s.description = package['description']
|
|
12
|
+
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
|
13
|
+
s.author = { 'khufu.io' => 'hello@khufu.io' }
|
|
14
|
+
s.homepage = package['homepage']
|
|
15
|
+
s.platforms = { :ios => '15.1' }
|
|
16
|
+
s.swift_version = '5.9'
|
|
17
|
+
# Autolinking installs this as a :path pod from node_modules, so CocoaPods never
|
|
18
|
+
# fetches a source: no public repo to name (ruling cmvgfbip).
|
|
19
|
+
s.source = { :path => '.' }
|
|
20
|
+
s.static_framework = true
|
|
21
|
+
|
|
22
|
+
s.dependency 'ExpoModulesCore'
|
|
23
|
+
|
|
24
|
+
s.pod_target_xcconfig = {
|
|
25
|
+
'DEFINES_MODULE' => 'YES',
|
|
26
|
+
'SWIFT_COMPILATION_MODE' => 'wholemodule'
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
s.source_files = 'ios/**/*.{h,m,mm,swift,hpp,cpp}'
|
|
30
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Expo module build script. It is NOT built standalone: Expo autolinking adds this
|
|
2
|
+
// directory as a Gradle subproject of the host app, which is where `expo-modules-core`
|
|
3
|
+
// and the Android SDK versions come from (hence safeExtGet + the core plugin below).
|
|
4
|
+
apply plugin: 'com.android.library'
|
|
5
|
+
apply plugin: 'kotlin-android'
|
|
6
|
+
apply plugin: 'maven-publish'
|
|
7
|
+
|
|
8
|
+
group = 'io.khufu.traqio'
|
|
9
|
+
version = '0.1.0'
|
|
10
|
+
|
|
11
|
+
def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
|
|
12
|
+
apply from: expoModulesCorePlugin
|
|
13
|
+
applyKotlinExpoModulesCorePlugin()
|
|
14
|
+
useCoreDependencies()
|
|
15
|
+
useExpoPublishing()
|
|
16
|
+
|
|
17
|
+
// Falls back to our own floor when the host app doesn't pin these (it usually does).
|
|
18
|
+
def safeExtGet(prop, fallback) {
|
|
19
|
+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
android {
|
|
23
|
+
// Distinct from the standalone io.khufu.traqio:traqio-android AAR (packages/sdk-android)
|
|
24
|
+
// so an app could depend on both without an R/BuildConfig or duplicate-class clash.
|
|
25
|
+
namespace "io.khufu.traqio.expo"
|
|
26
|
+
compileSdk safeExtGet("compileSdkVersion", 35)
|
|
27
|
+
|
|
28
|
+
defaultConfig {
|
|
29
|
+
minSdk safeExtGet("minSdkVersion", 24)
|
|
30
|
+
versionCode 1
|
|
31
|
+
versionName "0.1.0"
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
compileOptions {
|
|
35
|
+
sourceCompatibility JavaVersion.VERSION_17
|
|
36
|
+
targetCompatibility JavaVersion.VERSION_17
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
kotlinOptions {
|
|
40
|
+
jvmTarget = "17"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
lintOptions {
|
|
44
|
+
abortOnError false
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
publishing {
|
|
48
|
+
singleVariant("release") {
|
|
49
|
+
withSourcesJar()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
dependencies {
|
|
55
|
+
// Play Install Referrer — the source of deterministic Android attribution.
|
|
56
|
+
implementation "com.android.installreferrer:installreferrer:2.2"
|
|
57
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
+
<!-- No permission needed: the Play Install Referrer API is bound via the Play Store app.
|
|
3
|
+
The <queries> block IS needed on Android 11+ — package-visibility filtering hides
|
|
4
|
+
com.android.vending from apps Play did not install itself (sideloads, dev builds,
|
|
5
|
+
`adb install`), and the referrer read then fails with FEATURE_NOT_SUPPORTED. -->
|
|
6
|
+
<queries>
|
|
7
|
+
<package android:name="com.android.vending" />
|
|
8
|
+
<intent>
|
|
9
|
+
<action android:name="com.google.android.finsky.BIND_GET_INSTALL_REFERRER_SERVICE" />
|
|
10
|
+
</intent>
|
|
11
|
+
</queries>
|
|
12
|
+
</manifest>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
package io.khufu.traqio.expo
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The GAID — read ONLY when the host app opted in (`collectAdvertisingId = true`,
|
|
7
|
+
* decision cmvjohk6). This SDK never shows a prompt and declares no permission.
|
|
8
|
+
*
|
|
9
|
+
* Google's `AdvertisingIdClient` is reached by reflection, never as a dependency:
|
|
10
|
+
* `play-services-ads-identifier` merges the AD_ID permission into every app that
|
|
11
|
+
* includes it, and an app that does not opt in must not end up declaring it because
|
|
12
|
+
* of us. An app that opts in adds the library and the
|
|
13
|
+
* `com.google.android.gms.permission.AD_ID` permission itself; without either this
|
|
14
|
+
* answers null (Android 13+ also returns zeros without the permission).
|
|
15
|
+
*
|
|
16
|
+
* BLOCKING (Google's call does IPC): never on the main thread.
|
|
17
|
+
* Kept identical to packages/sdk-android/.../AdvertisingId.kt (the native SDK).
|
|
18
|
+
*/
|
|
19
|
+
internal object AdvertisingId {
|
|
20
|
+
private const val ZERO = "00000000-0000-0000-0000-000000000000"
|
|
21
|
+
|
|
22
|
+
fun read(context: Context): String? {
|
|
23
|
+
return try {
|
|
24
|
+
val client = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient")
|
|
25
|
+
val info = client.getMethod("getAdvertisingIdInfo", Context::class.java).invoke(null, context) ?: return null
|
|
26
|
+
val limited = info.javaClass.getMethod("isLimitAdTrackingEnabled").invoke(info) as? Boolean ?: true
|
|
27
|
+
val id = info.javaClass.getMethod("getId").invoke(info) as? String
|
|
28
|
+
if (limited || id.isNullOrBlank() || id == ZERO) null else id
|
|
29
|
+
} catch (_: Throwable) {
|
|
30
|
+
// Library absent, Play services missing or outdated, permission missing: no id.
|
|
31
|
+
null
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
package io.khufu.traqio.expo
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.os.Handler
|
|
5
|
+
import android.os.Looper
|
|
6
|
+
import com.android.installreferrer.api.InstallReferrerClient
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reads the Google Play **Install Referrer**. The referrer string carries our click id
|
|
10
|
+
* (…&traq_click=<clickId>&…) which the ingestion API matches to a stored Click →
|
|
11
|
+
* deterministic attribution. No IDFA, no fingerprinting.
|
|
12
|
+
*
|
|
13
|
+
* Wired into the Expo module's `getInstallReferrer()` bridge method; the JS SDK posts
|
|
14
|
+
* the raw string in `install`. All the retry/timeout/one-answer logic lives in
|
|
15
|
+
* [ReferrerFetch] so it is unit-tested without a device.
|
|
16
|
+
*
|
|
17
|
+
* Requires: implementation("com.android.installreferrer:installreferrer:2.2")
|
|
18
|
+
*/
|
|
19
|
+
object InstallReferrerReader {
|
|
20
|
+
|
|
21
|
+
/** @param callback receives exactly one result, on an arbitrary thread. */
|
|
22
|
+
fun read(context: Context, callback: (ReferrerResult) -> Unit) {
|
|
23
|
+
val appContext = context.applicationContext
|
|
24
|
+
val handler = Handler(Looper.getMainLooper())
|
|
25
|
+
ReferrerFetch(
|
|
26
|
+
connect = { PlayReferrerConnection(InstallReferrerClient.newBuilder(appContext).build()) },
|
|
27
|
+
scheduler = { delayMs, block -> handler.postDelayed({ block() }, delayMs) },
|
|
28
|
+
).start(callback)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private class PlayReferrerConnection(
|
|
33
|
+
private val client: InstallReferrerClient,
|
|
34
|
+
) : ReferrerConnection {
|
|
35
|
+
|
|
36
|
+
override fun startConnection(listener: ReferrerConnectionListener) {
|
|
37
|
+
client.startConnection(
|
|
38
|
+
object : com.android.installreferrer.api.InstallReferrerStateListener {
|
|
39
|
+
override fun onInstallReferrerSetupFinished(responseCode: Int) =
|
|
40
|
+
listener.onSetupFinished(responseCode)
|
|
41
|
+
|
|
42
|
+
override fun onInstallReferrerServiceDisconnected() = listener.onDisconnected()
|
|
43
|
+
},
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override fun readDetails(): ReferrerDetailsSnapshot {
|
|
48
|
+
val d = client.installReferrer
|
|
49
|
+
return ReferrerDetailsSnapshot(
|
|
50
|
+
installReferrer = d.installReferrer,
|
|
51
|
+
referrerClickTimestampSeconds = d.referrerClickTimestampSeconds,
|
|
52
|
+
installBeginTimestampSeconds = d.installBeginTimestampSeconds,
|
|
53
|
+
googlePlayInstantParam = d.googlePlayInstantParam,
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
override fun endConnection() = client.endConnection()
|
|
58
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
package io.khufu.traqio.expo
|
|
2
|
+
|
|
3
|
+
import java.util.concurrent.atomic.AtomicBoolean
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Android-free core of the Play Install Referrer read, so it can be unit-tested on a
|
|
7
|
+
* plain JVM (see `packages/sdk/android-tests`). [InstallReferrerReader] plugs the real
|
|
8
|
+
* `InstallReferrerClient` and a main-thread `Handler` into it.
|
|
9
|
+
*
|
|
10
|
+
* The referrer is readable exactly once in an install's life — the Play Store drops it
|
|
11
|
+
* after the first successful read on some devices, and the app is only "new" once — so
|
|
12
|
+
* the read has to be defensive: the Play service can answer twice, disconnect instead
|
|
13
|
+
* of answering, or never answer at all.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Mirrors `InstallReferrerClient.InstallReferrerResponse` (kept local so this file has no Android import). */
|
|
17
|
+
internal object ReferrerResponse {
|
|
18
|
+
const val SERVICE_DISCONNECTED = -1
|
|
19
|
+
const val OK = 0
|
|
20
|
+
const val SERVICE_UNAVAILABLE = 1
|
|
21
|
+
const val FEATURE_NOT_SUPPORTED = 2
|
|
22
|
+
const val DEVELOPER_ERROR = 3
|
|
23
|
+
const val PERMISSION_ERROR = 4
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Why a read ended. The JS side only branches on [ReferrerResult.retryable]; the status
|
|
28
|
+
* is what shows up in logs and in the host app's debug screen.
|
|
29
|
+
*/
|
|
30
|
+
object ReferrerStatus {
|
|
31
|
+
/** A referrer string was returned. */
|
|
32
|
+
const val OK = "ok"
|
|
33
|
+
/** Play answered, but this install carries no referrer — an organic install. */
|
|
34
|
+
const val NO_REFERRER = "no_referrer"
|
|
35
|
+
/** No Play Store / too old (sideloaded build, AOSP emulator, Huawei…). Never going to work. */
|
|
36
|
+
const val FEATURE_NOT_SUPPORTED = "feature_not_supported"
|
|
37
|
+
/** Play is there but couldn't serve us right now — worth another launch. */
|
|
38
|
+
const val SERVICE_UNAVAILABLE = "service_unavailable"
|
|
39
|
+
/** Play dropped the binding before answering. */
|
|
40
|
+
const val SERVICE_DISCONNECTED = "service_disconnected"
|
|
41
|
+
/** Bad call on our side (wrong context, already-closed client). */
|
|
42
|
+
const val DEVELOPER_ERROR = "developer_error"
|
|
43
|
+
/** Play refused the caller. */
|
|
44
|
+
const val PERMISSION_ERROR = "permission_error"
|
|
45
|
+
/** Play never answered within the deadline. */
|
|
46
|
+
const val TIMEOUT = "timeout"
|
|
47
|
+
/** Binder/RemoteException, or Play Store crashed mid-call. */
|
|
48
|
+
const val ERROR = "error"
|
|
49
|
+
/** Android only: no Context to bind from (module not attached yet). */
|
|
50
|
+
const val NO_CONTEXT = "no_context"
|
|
51
|
+
/** iOS: there is no Install Referrer, attribution goes through SKAN. */
|
|
52
|
+
const val NOT_SUPPORTED_PLATFORM = "not_supported_platform"
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The parts of `ReferrerDetails` we forward to JS. */
|
|
56
|
+
data class ReferrerDetailsSnapshot(
|
|
57
|
+
val installReferrer: String?,
|
|
58
|
+
val referrerClickTimestampSeconds: Long,
|
|
59
|
+
val installBeginTimestampSeconds: Long,
|
|
60
|
+
val googlePlayInstantParam: Boolean,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param retryable true when trying again on a later launch could still produce a
|
|
65
|
+
* referrer. Terminal outcomes (no Play Store, organic install) are false so the SDK
|
|
66
|
+
* stops waiting and reports the install.
|
|
67
|
+
*/
|
|
68
|
+
data class ReferrerResult(
|
|
69
|
+
val status: String,
|
|
70
|
+
val retryable: Boolean,
|
|
71
|
+
val details: ReferrerDetailsSnapshot? = null,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
/** One binding to the Play Store referrer service. */
|
|
75
|
+
internal interface ReferrerConnection {
|
|
76
|
+
fun startConnection(listener: ReferrerConnectionListener)
|
|
77
|
+
/** @throws Exception when the binder call fails. */
|
|
78
|
+
fun readDetails(): ReferrerDetailsSnapshot
|
|
79
|
+
fun endConnection()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
internal interface ReferrerConnectionListener {
|
|
83
|
+
fun onSetupFinished(responseCode: Int)
|
|
84
|
+
fun onDisconnected()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Injected so tests drive retries/timeouts without sleeping. */
|
|
88
|
+
internal fun interface DelayScheduler {
|
|
89
|
+
fun schedule(delayMs: Long, block: () -> Unit)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
internal class ReferrerFetch(
|
|
93
|
+
private val connect: () -> ReferrerConnection,
|
|
94
|
+
private val scheduler: DelayScheduler,
|
|
95
|
+
private val maxAttempts: Int = 3,
|
|
96
|
+
private val retryDelayMs: Long = 800L,
|
|
97
|
+
private val timeoutMs: Long = 10_000L,
|
|
98
|
+
) {
|
|
99
|
+
private val finished = AtomicBoolean(false)
|
|
100
|
+
private var callback: ((ReferrerResult) -> Unit)? = null
|
|
101
|
+
|
|
102
|
+
/** Calls [callback] exactly once, on whichever thread the Play service answers on. */
|
|
103
|
+
fun start(callback: (ReferrerResult) -> Unit) {
|
|
104
|
+
this.callback = callback
|
|
105
|
+
// A binding that never answers would otherwise leave the JS promise pending
|
|
106
|
+
// forever, and with it `Traqio.init()`.
|
|
107
|
+
scheduler.schedule(timeoutMs) { finish(ReferrerResult(ReferrerStatus.TIMEOUT, retryable = true)) }
|
|
108
|
+
attempt(1)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private fun attempt(n: Int) {
|
|
112
|
+
if (finished.get()) return
|
|
113
|
+
val connection = try {
|
|
114
|
+
connect()
|
|
115
|
+
} catch (e: Exception) {
|
|
116
|
+
return retryOrFail(n, ReferrerStatus.ERROR, retryable = true)
|
|
117
|
+
}
|
|
118
|
+
// The Play service is allowed to call us back more than once per binding
|
|
119
|
+
// (setup-finished then disconnected); only the first answer decides.
|
|
120
|
+
val answered = AtomicBoolean(false)
|
|
121
|
+
|
|
122
|
+
val listener = object : ReferrerConnectionListener {
|
|
123
|
+
override fun onSetupFinished(responseCode: Int) {
|
|
124
|
+
if (!answered.compareAndSet(false, true)) return
|
|
125
|
+
when (responseCode) {
|
|
126
|
+
ReferrerResponse.OK -> {
|
|
127
|
+
val details = try {
|
|
128
|
+
connection.readDetails()
|
|
129
|
+
} catch (e: Exception) {
|
|
130
|
+
close(connection)
|
|
131
|
+
return retryOrFail(n, ReferrerStatus.ERROR, retryable = true)
|
|
132
|
+
}
|
|
133
|
+
close(connection)
|
|
134
|
+
val referrer = details.installReferrer
|
|
135
|
+
if (referrer.isNullOrBlank()) {
|
|
136
|
+
// Play answered "nothing here" — that is the organic install
|
|
137
|
+
// answer, not a failure. Retrying would never change it.
|
|
138
|
+
finish(ReferrerResult(ReferrerStatus.NO_REFERRER, retryable = false))
|
|
139
|
+
} else {
|
|
140
|
+
finish(ReferrerResult(ReferrerStatus.OK, retryable = false, details = details))
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
ReferrerResponse.SERVICE_UNAVAILABLE -> {
|
|
144
|
+
close(connection)
|
|
145
|
+
retryOrFail(n, ReferrerStatus.SERVICE_UNAVAILABLE, retryable = true)
|
|
146
|
+
}
|
|
147
|
+
ReferrerResponse.SERVICE_DISCONNECTED -> {
|
|
148
|
+
close(connection)
|
|
149
|
+
retryOrFail(n, ReferrerStatus.SERVICE_DISCONNECTED, retryable = true)
|
|
150
|
+
}
|
|
151
|
+
ReferrerResponse.FEATURE_NOT_SUPPORTED -> {
|
|
152
|
+
close(connection)
|
|
153
|
+
finish(ReferrerResult(ReferrerStatus.FEATURE_NOT_SUPPORTED, retryable = false))
|
|
154
|
+
}
|
|
155
|
+
ReferrerResponse.DEVELOPER_ERROR -> {
|
|
156
|
+
close(connection)
|
|
157
|
+
finish(ReferrerResult(ReferrerStatus.DEVELOPER_ERROR, retryable = false))
|
|
158
|
+
}
|
|
159
|
+
ReferrerResponse.PERMISSION_ERROR -> {
|
|
160
|
+
close(connection)
|
|
161
|
+
finish(ReferrerResult(ReferrerStatus.PERMISSION_ERROR, retryable = false))
|
|
162
|
+
}
|
|
163
|
+
else -> {
|
|
164
|
+
close(connection)
|
|
165
|
+
retryOrFail(n, ReferrerStatus.ERROR, retryable = true)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
override fun onDisconnected() {
|
|
171
|
+
if (!answered.compareAndSet(false, true)) return
|
|
172
|
+
close(connection)
|
|
173
|
+
retryOrFail(n, ReferrerStatus.SERVICE_DISCONNECTED, retryable = true)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
connection.startConnection(listener)
|
|
179
|
+
} catch (e: Exception) {
|
|
180
|
+
// Some OEM builds throw straight out of bindService instead of reporting
|
|
181
|
+
// FEATURE_NOT_SUPPORTED.
|
|
182
|
+
if (!answered.compareAndSet(false, true)) return
|
|
183
|
+
close(connection)
|
|
184
|
+
retryOrFail(n, ReferrerStatus.ERROR, retryable = true)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private fun retryOrFail(n: Int, status: String, retryable: Boolean) {
|
|
189
|
+
if (finished.get()) return
|
|
190
|
+
if (n >= maxAttempts) {
|
|
191
|
+
finish(ReferrerResult(status, retryable))
|
|
192
|
+
return
|
|
193
|
+
}
|
|
194
|
+
scheduler.schedule(retryDelayMs * n) { attempt(n + 1) }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private fun close(connection: ReferrerConnection) {
|
|
198
|
+
try {
|
|
199
|
+
connection.endConnection()
|
|
200
|
+
} catch (e: Exception) {
|
|
201
|
+
// Already closed / service gone — nothing left to release.
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private fun finish(result: ReferrerResult) {
|
|
206
|
+
if (!finished.compareAndSet(false, true)) return
|
|
207
|
+
val cb = callback
|
|
208
|
+
callback = null // let the host app's callback (and its captured Promise) go
|
|
209
|
+
cb?.invoke(result)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
package io.khufu.traqio.expo
|
|
2
|
+
|
|
3
|
+
import expo.modules.kotlin.Promise
|
|
4
|
+
import expo.modules.kotlin.modules.Module
|
|
5
|
+
import expo.modules.kotlin.modules.ModuleDefinition
|
|
6
|
+
|
|
7
|
+
// Expo native module bridging the JS SDK to Android. Exposes the Play Install
|
|
8
|
+
// Referrer to `getNativeBridge()` in src/native.ts. SKAN and Apple Ads attribution
|
|
9
|
+
// are iOS-only (no-ops here).
|
|
10
|
+
class TraqioModule : Module() {
|
|
11
|
+
override fun definition() = ModuleDefinition {
|
|
12
|
+
Name("Traqio")
|
|
13
|
+
|
|
14
|
+
Property("platform") { "android" }
|
|
15
|
+
|
|
16
|
+
// Resolves to the same record shape as the iOS module, so the JS bridge is
|
|
17
|
+
// platform-agnostic: { status, retryable, referrer?, referrerClickTs?,
|
|
18
|
+
// installBeginTs?, googlePlayInstant? }. Never rejects — a failed read is a
|
|
19
|
+
// status, and the SDK decides from `retryable` whether to try again next launch.
|
|
20
|
+
AsyncFunction("getInstallReferrer") { promise: Promise ->
|
|
21
|
+
val context = appContext.reactContext?.applicationContext
|
|
22
|
+
if (context == null) {
|
|
23
|
+
promise.resolve(toRecord(ReferrerResult(ReferrerStatus.NO_CONTEXT, retryable = true)))
|
|
24
|
+
} else {
|
|
25
|
+
InstallReferrerReader.read(context) { result -> promise.resolve(toRecord(result)) }
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Basic, non-fingerprinting device context.
|
|
30
|
+
Function("getContext") {
|
|
31
|
+
mapOf(
|
|
32
|
+
"deviceModel" to android.os.Build.MODEL,
|
|
33
|
+
"osVersion" to android.os.Build.VERSION.RELEASE,
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// SKAN is iOS-only. Present so the shared JS bridge can call it
|
|
38
|
+
// unconditionally instead of branching on platform at every call site.
|
|
39
|
+
Function("updateConversionValue") { _: Int, _: String?, _: Boolean? -> }
|
|
40
|
+
|
|
41
|
+
// Apple's AdServices framework is iOS-only. Present, resolving null, for the
|
|
42
|
+
// same reason as above: `getNativeBridge()` builds ONE bridge object and the JS
|
|
43
|
+
// core must not have to branch on platform to decide which methods exist.
|
|
44
|
+
AsyncFunction("getAttributionToken") { promise: Promise -> promise.resolve(null) }
|
|
45
|
+
|
|
46
|
+
// The GAID, ONLY when the JS core asks — which it does only when the host set
|
|
47
|
+
// `collectAdvertisingId: true` (decision cmvjohk6). Google's call blocks, so it
|
|
48
|
+
// runs off the calling thread. Null without the ads-identifier library, without
|
|
49
|
+
// the AD_ID permission, or when the user limited ad tracking.
|
|
50
|
+
AsyncFunction("getAdvertisingId") { promise: Promise ->
|
|
51
|
+
val context = appContext.reactContext?.applicationContext
|
|
52
|
+
if (context == null) {
|
|
53
|
+
promise.resolve(null)
|
|
54
|
+
} else {
|
|
55
|
+
Thread { promise.resolve(AdvertisingId.read(context)) }.start()
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private fun toRecord(result: ReferrerResult): Map<String, Any?> = mapOf(
|
|
62
|
+
"status" to result.status,
|
|
63
|
+
"retryable" to result.retryable,
|
|
64
|
+
"referrer" to result.details?.installReferrer,
|
|
65
|
+
"referrerClickTs" to result.details?.referrerClickTimestampSeconds,
|
|
66
|
+
"installBeginTs" to result.details?.installBeginTimestampSeconds,
|
|
67
|
+
"googlePlayInstant" to result.details?.googlePlayInstantParam,
|
|
68
|
+
)
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAC"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.normalizeInstallReferrer = exports.getNativeBridge = void 0;
|
|
18
|
+
// @traqio/react-native — the RN / Expo surface. The platform-neutral engine lives
|
|
19
|
+
// in @traqio/core (queue, transport, SKAN conversion values, AdServices); this
|
|
20
|
+
// package adds the autolinkable Expo native module (Kotlin + Swift) and the bridge
|
|
21
|
+
// that wires it to the core. Re-exporting the core keeps a host app on one import.
|
|
22
|
+
__exportStar(require("@traqio/core"), exports);
|
|
23
|
+
var native_1 = require("./native");
|
|
24
|
+
Object.defineProperty(exports, "getNativeBridge", { enumerable: true, get: function () { return native_1.getNativeBridge; } });
|
|
25
|
+
Object.defineProperty(exports, "normalizeInstallReferrer", { enumerable: true, get: function () { return native_1.normalizeInstallReferrer; } });
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,kFAAkF;AAClF,+EAA+E;AAC/E,mFAAmF;AACnF,mFAAmF;AACnF,+CAA6B;AAC7B,mCAAqE;AAA5D,yGAAA,eAAe,OAAA;AAAE,kHAAA,wBAAwB,OAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CoarseValue, InstallReferrerResult, NativeBridge } from "@traqio/core";
|
|
2
|
+
/**
|
|
3
|
+
* Turns the native record into the SDK's `InstallReferrerResult`. Exported because
|
|
4
|
+
* this is the seam where a host app running an older native build (missing method,
|
|
5
|
+
* `null` answer) must degrade instead of throwing.
|
|
6
|
+
*/
|
|
7
|
+
export declare function normalizeInstallReferrer(raw: unknown): InstallReferrerResult;
|
|
8
|
+
export declare function getNativeBridge(): NativeBridge | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Set the iOS SKAN conversion value directly, for apps that own their own
|
|
11
|
+
* schema instead of using the mapping published in the dashboard. Most apps
|
|
12
|
+
* should NOT call this: `Traqio.track(..., { value })` already drives the value
|
|
13
|
+
* through the app's mapping, and mixing both means two things fighting over the
|
|
14
|
+
* same six bits.
|
|
15
|
+
*
|
|
16
|
+
* `lockWindow: true` closes the current conversion window immediately — the
|
|
17
|
+
* postback arrives sooner, at the cost of ignoring the rest of the window.
|
|
18
|
+
*/
|
|
19
|
+
export declare function updateSkanConversionValue(fineValue: number, coarse?: CoarseValue, lockWindow?: boolean): void;
|
|
20
|
+
//# sourceMappingURL=native.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,qBAAqB,EAErB,YAAY,EACb,MAAM,cAAc,CAAC;AAUtB;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,OAAO,GAAG,qBAAqB,CAe5E;AAQD,wBAAgB,eAAe,IAAI,YAAY,GAAG,SAAS,CAsD1D;AAED;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,WAAW,EACpB,UAAU,UAAQ,GACjB,IAAI,CAQN"}
|
package/build/native.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeInstallReferrer = normalizeInstallReferrer;
|
|
4
|
+
exports.getNativeBridge = getNativeBridge;
|
|
5
|
+
exports.updateSkanConversionValue = updateSkanConversionValue;
|
|
6
|
+
function finiteNumber(value) {
|
|
7
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Turns the native record into the SDK's `InstallReferrerResult`. Exported because
|
|
11
|
+
* this is the seam where a host app running an older native build (missing method,
|
|
12
|
+
* `null` answer) must degrade instead of throwing.
|
|
13
|
+
*/
|
|
14
|
+
function normalizeInstallReferrer(raw) {
|
|
15
|
+
const r = raw;
|
|
16
|
+
if (!r || typeof r.status !== "string") {
|
|
17
|
+
// Native module present but too old to answer — retrying never helps.
|
|
18
|
+
return { status: "unavailable", retryable: false };
|
|
19
|
+
}
|
|
20
|
+
const referrer = typeof r.referrer === "string" && r.referrer.trim() ? r.referrer : undefined;
|
|
21
|
+
return {
|
|
22
|
+
status: r.status,
|
|
23
|
+
retryable: r.retryable === true,
|
|
24
|
+
referrer,
|
|
25
|
+
referrerClickTs: finiteNumber(r.referrerClickTs),
|
|
26
|
+
installBeginTs: finiteNumber(r.installBeginTs),
|
|
27
|
+
googlePlayInstant: typeof r.googlePlayInstant === "boolean" ? r.googlePlayInstant : undefined,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// Wires the Expo native module → the JS SDK's NativeBridge. On Android it reads
|
|
31
|
+
// the Play Install Referrer (InstallReferrerReader.kt); on iOS it drives SKAN
|
|
32
|
+
// (TraqioSkan.swift). Pass the result to Traqio.init({ native: getNativeBridge() }).
|
|
33
|
+
//
|
|
34
|
+
// Requires the app to have the native module installed (dev build / EAS, not Expo Go).
|
|
35
|
+
// We import lazily so the SDK also works in pure-JS test contexts.
|
|
36
|
+
function getNativeBridge() {
|
|
37
|
+
try {
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
39
|
+
const { requireNativeModule } = require("expo-modules-core");
|
|
40
|
+
const m = requireNativeModule("Traqio");
|
|
41
|
+
return {
|
|
42
|
+
platform: m.platform, // "android" | "ios"
|
|
43
|
+
// The native side answers with a status instead of throwing, but a bridge
|
|
44
|
+
// teardown mid-call still can — a rejected install read must not take
|
|
45
|
+
// `Traqio.init()` down with it.
|
|
46
|
+
getInstallReferrer: async () => {
|
|
47
|
+
try {
|
|
48
|
+
return normalizeInstallReferrer(await m.getInstallReferrer?.());
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return { status: "error", retryable: true };
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
getContext: async () => {
|
|
55
|
+
try {
|
|
56
|
+
return (await m.getContext?.()) ?? {};
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
// iOS drives SKAdNetwork through this; the Android module implements it as
|
|
63
|
+
// a no-op so the JS core never has to branch on platform.
|
|
64
|
+
updateConversionValue: (fine, coarse, lockWindow) => m.updateConversionValue?.(fine, coarse, lockWindow ?? false),
|
|
65
|
+
// Apple Ads attribution token (iOS 14.3+). Resolves null rather than throwing
|
|
66
|
+
// on a native build that predates the method, or a device the framework has
|
|
67
|
+
// nothing for — which is most devices, since most installs are not Search Ads.
|
|
68
|
+
getAttributionToken: async () => {
|
|
69
|
+
try {
|
|
70
|
+
const token = await m.getAttributionToken?.();
|
|
71
|
+
return typeof token === "string" && token.trim() ? token : null;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
// IDFA / GAID — only ever called when the host passed
|
|
78
|
+
// `collectAdvertisingId: true` (decision cmvjohk6). Never prompts; null on a
|
|
79
|
+
// native build that predates the method, without ATT, or without AD_ID.
|
|
80
|
+
getAdvertisingId: async () => {
|
|
81
|
+
try {
|
|
82
|
+
const id = await m.getAdvertisingId?.();
|
|
83
|
+
return typeof id === "string" && id.trim() ? id : null;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return undefined; // native module not present (e.g. tests / web)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Set the iOS SKAN conversion value directly, for apps that own their own
|
|
97
|
+
* schema instead of using the mapping published in the dashboard. Most apps
|
|
98
|
+
* should NOT call this: `Traqio.track(..., { value })` already drives the value
|
|
99
|
+
* through the app's mapping, and mixing both means two things fighting over the
|
|
100
|
+
* same six bits.
|
|
101
|
+
*
|
|
102
|
+
* `lockWindow: true` closes the current conversion window immediately — the
|
|
103
|
+
* postback arrives sooner, at the cost of ignoring the rest of the window.
|
|
104
|
+
*/
|
|
105
|
+
function updateSkanConversionValue(fineValue, coarse, lockWindow = false) {
|
|
106
|
+
try {
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
108
|
+
const { requireNativeModule } = require("expo-modules-core");
|
|
109
|
+
requireNativeModule("Traqio").updateConversionValue?.(fineValue, coarse, lockWindow);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
/* no-op off-device */
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.js","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
#if canImport(ObjectiveC)
|
|
3
|
+
import ObjectiveC
|
|
4
|
+
#endif
|
|
5
|
+
|
|
6
|
+
/// The IDFA — read ONLY when the host app opted in (`collectAdvertisingId: true`,
|
|
7
|
+
/// decision cmvjohk6), and only if the app ALREADY obtained App Tracking
|
|
8
|
+
/// Transparency. This SDK never shows the ATT prompt.
|
|
9
|
+
///
|
|
10
|
+
/// Both frameworks are reached through the Objective-C runtime, never linked: an
|
|
11
|
+
/// app that does not opt in ships no AdSupport / AppTrackingTransparency symbol
|
|
12
|
+
/// because of Traqio, and so has nothing to declare for us. An app that opts in
|
|
13
|
+
/// links AdSupport itself (most already do through their ad SDK — AppLovin MAX,
|
|
14
|
+
/// Unity Ads); without it the classes are absent and this answers nil.
|
|
15
|
+
///
|
|
16
|
+
/// Kept identical to packages/sdk-ios/Sources/TraqioSDK/TraqioAdvertisingId.swift.
|
|
17
|
+
enum TraqioAdvertisingId {
|
|
18
|
+
private static let zero = "00000000-0000-0000-0000-000000000000"
|
|
19
|
+
/// `ATTrackingAuthorizationStatus.authorized`.
|
|
20
|
+
private static let authorized: UInt = 3
|
|
21
|
+
|
|
22
|
+
static func read() -> String? {
|
|
23
|
+
#if canImport(ObjectiveC)
|
|
24
|
+
if #available(iOS 14, macOS 11, tvOS 14, *) {
|
|
25
|
+
// No ATT class = framework not linked by the app = it never asked = no.
|
|
26
|
+
guard trackingStatus() == authorized else { return nil }
|
|
27
|
+
}
|
|
28
|
+
guard let cls = NSClassFromString("ASIdentifierManager") as AnyObject?,
|
|
29
|
+
let manager = cls.perform(NSSelectorFromString("sharedManager"))?.takeUnretainedValue(),
|
|
30
|
+
let uuid = manager.perform(NSSelectorFromString("advertisingIdentifier"))?.takeUnretainedValue() as? NSUUID
|
|
31
|
+
else { return nil }
|
|
32
|
+
let id = uuid.uuidString
|
|
33
|
+
return id == zero ? nil : id
|
|
34
|
+
#else
|
|
35
|
+
return nil
|
|
36
|
+
#endif
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#if canImport(ObjectiveC)
|
|
40
|
+
/// `ATTrackingManager.trackingAuthorizationStatus`, a class property returning an
|
|
41
|
+
/// NSUInteger — called through its IMP because `perform` only returns objects.
|
|
42
|
+
private static func trackingStatus() -> UInt? {
|
|
43
|
+
let sel = NSSelectorFromString("trackingAuthorizationStatus")
|
|
44
|
+
guard let cls = NSClassFromString("ATTrackingManager"), (cls as AnyObject).responds(to: sel) else { return nil }
|
|
45
|
+
typealias Getter = @convention(c) (AnyObject, Selector) -> UInt
|
|
46
|
+
let imp = (cls as AnyObject).method(for: sel)
|
|
47
|
+
return unsafeBitCast(imp, to: Getter.self)(cls, sel)
|
|
48
|
+
}
|
|
49
|
+
#endif
|
|
50
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import UIKit
|
|
3
|
+
#if canImport(AdServices)
|
|
4
|
+
import AdServices
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
// Expo native module bridging the JS SDK to iOS. iOS has no Install Referrer, so
|
|
8
|
+
// there is no click of OURS to match — install attribution is SKAN (registered on
|
|
9
|
+
// create) plus Apple Ads, which self-attributes through AdServices. The SKAN surface
|
|
10
|
+
// mirrors src/skan.ts one-to-one; `getAttributionToken` mirrors src/adservices.ts.
|
|
11
|
+
public class TraqioModule: Module {
|
|
12
|
+
public func definition() -> ModuleDefinition {
|
|
13
|
+
Name("Traqio")
|
|
14
|
+
|
|
15
|
+
Property("platform") { "ios" }
|
|
16
|
+
|
|
17
|
+
OnCreate {
|
|
18
|
+
TraqioSkan.registerForAttribution()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Same record shape as Android so the JS bridge is platform-agnostic. There is
|
|
22
|
+
// no Install Referrer on iOS, and never will be — hence retryable: false.
|
|
23
|
+
AsyncFunction("getInstallReferrer") { () -> [String: Any?] in
|
|
24
|
+
return ["status": "not_supported_platform", "retryable": false]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Function("getContext") { () -> [String: String] in
|
|
28
|
+
var info = utsname()
|
|
29
|
+
uname(&info)
|
|
30
|
+
let machine = withUnsafePointer(to: &info.machine) {
|
|
31
|
+
$0.withMemoryRebound(to: CChar.self, capacity: 1) { String(validatingUTF8: $0) ?? "" }
|
|
32
|
+
}
|
|
33
|
+
return ["deviceModel": machine, "osVersion": UIDevice.current.systemVersion]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The JS core computes fine + coarse from the mapping it fetched from
|
|
37
|
+
// GET /skan/mapping/<sdkKey>; the native side only talks to SKAdNetwork.
|
|
38
|
+
// `coarse` is "low" | "medium" | "high"; anything else falls back to a tier
|
|
39
|
+
// derived from the fine value.
|
|
40
|
+
Function("updateConversionValue") { (value: Int, coarse: String?, lockWindow: Bool?) in
|
|
41
|
+
if let coarse = coarse, let tier = TraqioCoarseValue(name: coarse) {
|
|
42
|
+
TraqioSkan.updateConversionValue(value, coarse: tier, lockWindow: lockWindow ?? false)
|
|
43
|
+
} else {
|
|
44
|
+
TraqioSkan.updateConversionValue(value)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Apple Ads (Search Ads) attribution. The token is an opaque, single-purpose
|
|
49
|
+
// claim ticket valid 24 HOURS — not an identifier, and no ATT prompt of our own.
|
|
50
|
+
// The JS core posts it; the SERVER resolves it with Apple, because Apple answers
|
|
51
|
+
// 404 until the token is known on its side and that retry has to outlive the app
|
|
52
|
+
// process. See packages/sdk/src/adservices.ts and apps/api/src/apple-ads/.
|
|
53
|
+
//
|
|
54
|
+
// Resolves nil rather than throwing: `attributionToken()` throws on a device with
|
|
55
|
+
// no attribution record, which is MOST devices, and a rejected promise on every
|
|
56
|
+
// launch of every app that is not running Search Ads is noise, not a signal.
|
|
57
|
+
// The IDFA, ONLY when the JS core asks — which it does only when the host set
|
|
58
|
+
// `collectAdvertisingId: true` (decision cmvjohk6). Never prompts: answers only
|
|
59
|
+
// if the app ALREADY holds ATT. Resolved through the Objective-C runtime so this
|
|
60
|
+
// module links neither AdSupport nor AppTrackingTransparency — an app that never
|
|
61
|
+
// opts in ships no IDFA symbol because of us. See TraqioAdvertisingId.swift.
|
|
62
|
+
AsyncFunction("getAdvertisingId") { () -> String? in
|
|
63
|
+
return TraqioAdvertisingId.read()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
AsyncFunction("getAttributionToken") { () -> String? in
|
|
67
|
+
#if canImport(AdServices)
|
|
68
|
+
if #available(iOS 14.3, *) {
|
|
69
|
+
return try? AAAttribution.attributionToken()
|
|
70
|
+
}
|
|
71
|
+
#endif
|
|
72
|
+
return nil
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import StoreKit
|
|
3
|
+
|
|
4
|
+
/// SKAN 4 coarse conversion value. Apple substitutes it for the fine value in low
|
|
5
|
+
/// postback data tiers and in the 2nd/3rd conversion windows, so an app that only
|
|
6
|
+
/// sets the fine value is invisible for most of its installs.
|
|
7
|
+
@objc public enum TraqioCoarseValue: Int {
|
|
8
|
+
case low = 0, medium = 1, high = 2
|
|
9
|
+
|
|
10
|
+
/// Parse the wire form the JS bridge passes down.
|
|
11
|
+
public init?(name: String) {
|
|
12
|
+
switch name {
|
|
13
|
+
case "low": self = .low
|
|
14
|
+
case "medium": self = .medium
|
|
15
|
+
case "high": self = .high
|
|
16
|
+
default: return nil
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// iOS SKAdNetwork conversion values, for the Expo module and the Capacitor plugin.
|
|
22
|
+
/// The two packages ship byte-identical copies of this file
|
|
23
|
+
/// (packages/sdk/ios, packages/sdk-capacitor/ios/Plugin); the Capacitor tests fail
|
|
24
|
+
/// when they drift, so edit both.
|
|
25
|
+
///
|
|
26
|
+
/// What SKAN can and cannot do, so nobody builds on a wrong assumption: it gives
|
|
27
|
+
/// Apple SIX BITS about this install, delivered to the ad network (and to our
|
|
28
|
+
/// postback endpoint) at least 24h after the conversion window closes, and
|
|
29
|
+
/// withheld entirely when the campaign is too small for crowd anonymity. It is
|
|
30
|
+
/// aggregated, coarse and late by construction — never device-level. No IDFA, no
|
|
31
|
+
/// pasteboard, no fingerprinting.
|
|
32
|
+
///
|
|
33
|
+
/// This file is deliberately thin: the revenue→value mapping lives in the JS core
|
|
34
|
+
/// (`src/skan.ts`), which fetches it from `GET /skan/mapping/<sdkKey>` so the
|
|
35
|
+
/// device encodes with exactly the mapping the server decodes with. The native
|
|
36
|
+
/// side only enforces what only it can: the SKAN API ladder and the
|
|
37
|
+
/// monotonic-value rule. (The standalone Swift SDK in `packages/sdk-ios` carries
|
|
38
|
+
/// the mapping itself, because it has no JS layer.)
|
|
39
|
+
@objc public class TraqioSkan: NSObject {
|
|
40
|
+
private static let lastFineKey = "traq_skan_fine"
|
|
41
|
+
private static let maxFineValue = 63
|
|
42
|
+
|
|
43
|
+
/// Register the install. Call once, at first launch: without it Apple has no
|
|
44
|
+
/// conversion to attribute and the ad network never receives a postback.
|
|
45
|
+
@objc public static func registerForAttribution() {
|
|
46
|
+
if #available(iOS 16.1, *) {
|
|
47
|
+
SKAdNetwork.updatePostbackConversionValue(0, coarseValue: .low, lockWindow: false) { _ in }
|
|
48
|
+
} else if #available(iOS 15.4, *) {
|
|
49
|
+
SKAdNetwork.updatePostbackConversionValue(0) { _ in }
|
|
50
|
+
} else {
|
|
51
|
+
SKAdNetwork.registerAppForAdNetworkAttribution()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Update the conversion value.
|
|
56
|
+
///
|
|
57
|
+
/// `lockWindow: true` finalises the current conversion window immediately
|
|
58
|
+
/// instead of waiting for it to close — the postback arrives sooner, at the cost
|
|
59
|
+
/// of ignoring everything the user does for the rest of the window.
|
|
60
|
+
@objc public static func updateConversionValue(
|
|
61
|
+
_ fineValue: Int,
|
|
62
|
+
coarse: TraqioCoarseValue,
|
|
63
|
+
lockWindow: Bool
|
|
64
|
+
) {
|
|
65
|
+
guard fineValue >= 0, fineValue <= maxFineValue else { return } // Apple ignores out-of-range anyway
|
|
66
|
+
guard shouldSend(fineValue) else { return }
|
|
67
|
+
|
|
68
|
+
if #available(iOS 16.1, *) {
|
|
69
|
+
SKAdNetwork.updatePostbackConversionValue(fineValue, coarseValue: coarse.skCoarse, lockWindow: lockWindow) { _ in }
|
|
70
|
+
} else if #available(iOS 15.4, *) {
|
|
71
|
+
SKAdNetwork.updatePostbackConversionValue(fineValue) { _ in }
|
|
72
|
+
} else {
|
|
73
|
+
SKAdNetwork.updateConversionValue(fineValue)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// Overload for callers with no coarse tier of their own: derive one from where
|
|
78
|
+
/// the fine value sits in the 0–63 range. Crude, but a rough coarse value still
|
|
79
|
+
/// beats none, which is what most low-tier postbacks would otherwise carry.
|
|
80
|
+
@objc public static func updateConversionValue(_ fineValue: Int) {
|
|
81
|
+
let third = maxFineValue / 3
|
|
82
|
+
let coarse: TraqioCoarseValue = fineValue <= third ? .low : (fineValue <= 2 * third ? .medium : .high)
|
|
83
|
+
updateConversionValue(fineValue, coarse: coarse, lockWindow: false)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// Only ever send an increasing fine value. On SKAN 3 and earlier the rolling
|
|
87
|
+
/// 24h measurement timer restarts only for a value GREATER than the previous
|
|
88
|
+
/// one, so a lower value both fails to extend the window and can regress what
|
|
89
|
+
/// the postback ends up reporting.
|
|
90
|
+
private static func shouldSend(_ fineValue: Int) -> Bool {
|
|
91
|
+
let defaults = UserDefaults.standard
|
|
92
|
+
if defaults.object(forKey: lastFineKey) != nil, fineValue <= defaults.integer(forKey: lastFineKey) {
|
|
93
|
+
return false
|
|
94
|
+
}
|
|
95
|
+
defaults.set(fineValue, forKey: lastFineKey)
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@available(iOS 16.1, *)
|
|
101
|
+
extension TraqioCoarseValue {
|
|
102
|
+
fileprivate var skCoarse: SKAdNetwork.CoarseConversionValue {
|
|
103
|
+
switch self {
|
|
104
|
+
case .low: return .low
|
|
105
|
+
case .medium: return .medium
|
|
106
|
+
case .high: return .high
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@traqio/react-native",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Traqio SDK for React Native / Expo — deterministic install attribution (Play Install Referrer) + SKAN. No IDFA, no fingerprinting.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/khufuio/traqio.git",
|
|
9
|
+
"directory": "packages/sdk"
|
|
10
|
+
},
|
|
11
|
+
"author": "khufu.io <hello@khufu.io>",
|
|
12
|
+
"homepage": "https://traqio.app/docs/sdks/react-native",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"attribution",
|
|
15
|
+
"mmp",
|
|
16
|
+
"expo",
|
|
17
|
+
"react-native",
|
|
18
|
+
"install-referrer",
|
|
19
|
+
"skadnetwork",
|
|
20
|
+
"roas"
|
|
21
|
+
],
|
|
22
|
+
"main": "build/index.js",
|
|
23
|
+
"types": "build/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./build/index.d.ts",
|
|
27
|
+
"default": "./build/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"files": [
|
|
33
|
+
"build",
|
|
34
|
+
"src",
|
|
35
|
+
"!src/**/*.test.ts",
|
|
36
|
+
"android",
|
|
37
|
+
"ios",
|
|
38
|
+
"expo-module.config.json",
|
|
39
|
+
"TraqioExpo.podspec",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@traqio/core": "0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@react-native-async-storage/async-storage": ">=1.21.0",
|
|
47
|
+
"expo": ">=51.0.0",
|
|
48
|
+
"expo-modules-core": "*",
|
|
49
|
+
"react-native": ">=0.74.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"@react-native-async-storage/async-storage": {
|
|
53
|
+
"optional": true
|
|
54
|
+
},
|
|
55
|
+
"expo": {
|
|
56
|
+
"optional": true
|
|
57
|
+
},
|
|
58
|
+
"expo-modules-core": {
|
|
59
|
+
"optional": true
|
|
60
|
+
},
|
|
61
|
+
"react-native": {
|
|
62
|
+
"optional": true
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"typescript": "^5",
|
|
67
|
+
"vitest": "^4.1.12"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "tsc -p tsconfig.build.json",
|
|
71
|
+
"clean": "rm -rf build",
|
|
72
|
+
"check:package": "node scripts/check-package.mjs",
|
|
73
|
+
"typecheck": "tsc --noEmit",
|
|
74
|
+
"test": "vitest run"
|
|
75
|
+
}
|
|
6
76
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// @traqio/react-native — the RN / Expo surface. The platform-neutral engine lives
|
|
2
|
+
// in @traqio/core (queue, transport, SKAN conversion values, AdServices); this
|
|
3
|
+
// package adds the autolinkable Expo native module (Kotlin + Swift) and the bridge
|
|
4
|
+
// that wires it to the core. Re-exporting the core keeps a host app on one import.
|
|
5
|
+
export * from "@traqio/core";
|
|
6
|
+
export { getNativeBridge, normalizeInstallReferrer } from "./native";
|
package/src/native.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CoarseValue,
|
|
3
|
+
InstallReferrerResult,
|
|
4
|
+
InstallReferrerStatus,
|
|
5
|
+
NativeBridge,
|
|
6
|
+
} from "@traqio/core";
|
|
7
|
+
|
|
8
|
+
// `require` is provided by the React Native / Metro runtime (we import the native
|
|
9
|
+
// module lazily so the SDK also works in pure-JS/test contexts).
|
|
10
|
+
declare const require: (module: string) => any;
|
|
11
|
+
|
|
12
|
+
function finiteNumber(value: unknown): number | undefined {
|
|
13
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Turns the native record into the SDK's `InstallReferrerResult`. Exported because
|
|
18
|
+
* this is the seam where a host app running an older native build (missing method,
|
|
19
|
+
* `null` answer) must degrade instead of throwing.
|
|
20
|
+
*/
|
|
21
|
+
export function normalizeInstallReferrer(raw: unknown): InstallReferrerResult {
|
|
22
|
+
const r = raw as Record<string, unknown> | null | undefined;
|
|
23
|
+
if (!r || typeof r.status !== "string") {
|
|
24
|
+
// Native module present but too old to answer — retrying never helps.
|
|
25
|
+
return { status: "unavailable", retryable: false };
|
|
26
|
+
}
|
|
27
|
+
const referrer = typeof r.referrer === "string" && r.referrer.trim() ? r.referrer : undefined;
|
|
28
|
+
return {
|
|
29
|
+
status: r.status as InstallReferrerStatus,
|
|
30
|
+
retryable: r.retryable === true,
|
|
31
|
+
referrer,
|
|
32
|
+
referrerClickTs: finiteNumber(r.referrerClickTs),
|
|
33
|
+
installBeginTs: finiteNumber(r.installBeginTs),
|
|
34
|
+
googlePlayInstant: typeof r.googlePlayInstant === "boolean" ? r.googlePlayInstant : undefined,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Wires the Expo native module → the JS SDK's NativeBridge. On Android it reads
|
|
39
|
+
// the Play Install Referrer (InstallReferrerReader.kt); on iOS it drives SKAN
|
|
40
|
+
// (TraqioSkan.swift). Pass the result to Traqio.init({ native: getNativeBridge() }).
|
|
41
|
+
//
|
|
42
|
+
// Requires the app to have the native module installed (dev build / EAS, not Expo Go).
|
|
43
|
+
// We import lazily so the SDK also works in pure-JS test contexts.
|
|
44
|
+
export function getNativeBridge(): NativeBridge | undefined {
|
|
45
|
+
try {
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
47
|
+
const { requireNativeModule } = require("expo-modules-core");
|
|
48
|
+
const m = requireNativeModule("Traqio");
|
|
49
|
+
return {
|
|
50
|
+
platform: m.platform, // "android" | "ios"
|
|
51
|
+
// The native side answers with a status instead of throwing, but a bridge
|
|
52
|
+
// teardown mid-call still can — a rejected install read must not take
|
|
53
|
+
// `Traqio.init()` down with it.
|
|
54
|
+
getInstallReferrer: async () => {
|
|
55
|
+
try {
|
|
56
|
+
return normalizeInstallReferrer(await m.getInstallReferrer?.());
|
|
57
|
+
} catch {
|
|
58
|
+
return { status: "error", retryable: true };
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
getContext: async () => {
|
|
62
|
+
try {
|
|
63
|
+
return (await m.getContext?.()) ?? {};
|
|
64
|
+
} catch {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
// iOS drives SKAdNetwork through this; the Android module implements it as
|
|
69
|
+
// a no-op so the JS core never has to branch on platform.
|
|
70
|
+
updateConversionValue: (fine, coarse, lockWindow) =>
|
|
71
|
+
m.updateConversionValue?.(fine, coarse, lockWindow ?? false),
|
|
72
|
+
// Apple Ads attribution token (iOS 14.3+). Resolves null rather than throwing
|
|
73
|
+
// on a native build that predates the method, or a device the framework has
|
|
74
|
+
// nothing for — which is most devices, since most installs are not Search Ads.
|
|
75
|
+
getAttributionToken: async () => {
|
|
76
|
+
try {
|
|
77
|
+
const token = await m.getAttributionToken?.();
|
|
78
|
+
return typeof token === "string" && token.trim() ? token : null;
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
// IDFA / GAID — only ever called when the host passed
|
|
84
|
+
// `collectAdvertisingId: true` (decision cmvjohk6). Never prompts; null on a
|
|
85
|
+
// native build that predates the method, without ATT, or without AD_ID.
|
|
86
|
+
getAdvertisingId: async () => {
|
|
87
|
+
try {
|
|
88
|
+
const id = await m.getAdvertisingId?.();
|
|
89
|
+
return typeof id === "string" && id.trim() ? id : null;
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
} catch {
|
|
96
|
+
return undefined; // native module not present (e.g. tests / web)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Set the iOS SKAN conversion value directly, for apps that own their own
|
|
102
|
+
* schema instead of using the mapping published in the dashboard. Most apps
|
|
103
|
+
* should NOT call this: `Traqio.track(..., { value })` already drives the value
|
|
104
|
+
* through the app's mapping, and mixing both means two things fighting over the
|
|
105
|
+
* same six bits.
|
|
106
|
+
*
|
|
107
|
+
* `lockWindow: true` closes the current conversion window immediately — the
|
|
108
|
+
* postback arrives sooner, at the cost of ignoring the rest of the window.
|
|
109
|
+
*/
|
|
110
|
+
export function updateSkanConversionValue(
|
|
111
|
+
fineValue: number,
|
|
112
|
+
coarse?: CoarseValue,
|
|
113
|
+
lockWindow = false,
|
|
114
|
+
): void {
|
|
115
|
+
try {
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
117
|
+
const { requireNativeModule } = require("expo-modules-core");
|
|
118
|
+
requireNativeModule("Traqio").updateConversionValue?.(fineValue, coarse, lockWindow);
|
|
119
|
+
} catch {
|
|
120
|
+
/* no-op off-device */
|
|
121
|
+
}
|
|
122
|
+
}
|