@ondropp/dropplink-react-native 0.0.0-stage → 0.2.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/README.md +333 -2
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/app/ondropp/dropplink/reactnative/RNDropplinkModule.kt +158 -0
- package/android/src/main/java/app/ondropp/dropplink/reactnative/RNDropplinkPackage.kt +27 -0
- package/dropplink-react-native.podspec +28 -0
- package/ios/RNDropplink.m +40 -0
- package/ios/RNDropplink.swift +159 -0
- package/lib/module/Dropplink.js +163 -0
- package/lib/module/Dropplink.js.map +1 -0
- package/lib/module/NativeDropplink.js +17 -0
- package/lib/module/NativeDropplink.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useDropplinkDeepLink.js +19 -0
- package/lib/module/useDropplinkDeepLink.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/Dropplink.d.ts +58 -0
- package/lib/typescript/src/Dropplink.d.ts.map +1 -0
- package/lib/typescript/src/NativeDropplink.d.ts +40 -0
- package/lib/typescript/src/NativeDropplink.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +62 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/useDropplinkDeepLink.d.ts +7 -0
- package/lib/typescript/src/useDropplinkDeepLink.d.ts.map +1 -0
- package/package.json +119 -4
- package/src/Dropplink.ts +202 -0
- package/src/NativeDropplink.ts +48 -0
- package/src/index.ts +12 -0
- package/src/types.ts +66 -0
- package/src/useDropplinkDeepLink.ts +19 -0
package/README.md
CHANGED
|
@@ -1,3 +1,334 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Dropplink for React Native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@ondropp/dropplink-react-native` attributes installs to the Dropplink link, channel and creator that
|
|
4
|
+
brought them, delivers deep links (the deferred one after a fresh install, and App Link / Universal
|
|
5
|
+
Link opens), and records in-app events and the signed-in user.
|
|
6
|
+
|
|
7
|
+
It is a thin bridge over the native SDKs
|
|
8
|
+
([`app.ondropp:dropplink-android`](../android) and the [`Dropplink`](../ios) pod). Install ids, the Play
|
|
9
|
+
Install Referrer, request signing, storage, retries and pending-link buffering all happen natively; the JS
|
|
10
|
+
only forwards calls and converts results. Behaviour is defined in [docs/sdk-spec.md](../docs/sdk-spec.md).
|
|
11
|
+
|
|
12
|
+
- [Requirements](#requirements)
|
|
13
|
+
- [Install](#install)
|
|
14
|
+
- [Keys](#keys)
|
|
15
|
+
- [Initialize](#initialize)
|
|
16
|
+
- [Deep links](#deep-links)
|
|
17
|
+
- [App Links (Android)](#app-links-android)
|
|
18
|
+
- [Universal Links (iOS)](#universal-links-ios)
|
|
19
|
+
- [Events, revenue and users](#events-revenue-and-users)
|
|
20
|
+
- [API](#api)
|
|
21
|
+
- [Test your integration](#test-your-integration)
|
|
22
|
+
- [Troubleshooting](#troubleshooting)
|
|
23
|
+
- [Architecture](#architecture)
|
|
24
|
+
- [Development](#development)
|
|
25
|
+
|
|
26
|
+
## Requirements
|
|
27
|
+
|
|
28
|
+
- React Native 0.76 or newer, New or old Architecture.
|
|
29
|
+
- Android 7.0 (API 24)+, iOS: React Native's minimum.
|
|
30
|
+
- Expo: works in a [development build](https://docs.expo.dev/develop/development-builds/introduction/)
|
|
31
|
+
(`npx expo prebuild`), not in Expo Go.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
npm install @ondropp/dropplink-react-native
|
|
37
|
+
cd ios && pod install
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Autolinking registers the native module on both platforms. Rebuild the app (`npx react-native run-android`
|
|
41
|
+
/ `run-ios`); a Metro reload alone doesn't pick up native code.
|
|
42
|
+
|
|
43
|
+
## Keys
|
|
44
|
+
|
|
45
|
+
Each app has two SDK keys in the Dropplink dashboard (**SDK setup**): one for Android and one for iOS. A key
|
|
46
|
+
is a key id (`ak_…`) and a secret.
|
|
47
|
+
|
|
48
|
+
**Never hard-code the secret in JS that is committed to git.** Load it from build configuration instead,
|
|
49
|
+
for example with [`react-native-config`](https://github.com/lugg/react-native-config) and a git-ignored
|
|
50
|
+
`.env`:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
# .env (in .gitignore)
|
|
54
|
+
DROPPLINK_ANDROID_KEY_ID=ak_...
|
|
55
|
+
DROPPLINK_ANDROID_SECRET=...
|
|
56
|
+
DROPPLINK_IOS_KEY_ID=ak_...
|
|
57
|
+
DROPPLINK_IOS_SECRET=...
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
In CI, write the `.env` from secrets before building. Any other build-time mechanism (Gradle
|
|
61
|
+
`BuildConfig` fields, xcconfig, `babel-plugin-transform-inline-environment-variables`) works too; the
|
|
62
|
+
SDK only needs the two strings at `initialize`.
|
|
63
|
+
|
|
64
|
+
## Initialize
|
|
65
|
+
|
|
66
|
+
Call `initialize` once, as early as possible: at the top of `index.js`, before `AppRegistry.registerComponent`,
|
|
67
|
+
so the install is reported even if the user leaves the first screen quickly.
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
// index.js
|
|
71
|
+
import { AppRegistry, Platform } from 'react-native';
|
|
72
|
+
import Config from 'react-native-config';
|
|
73
|
+
import { Dropplink } from '@ondropp/dropplink-react-native';
|
|
74
|
+
import App from './App';
|
|
75
|
+
|
|
76
|
+
Dropplink.initialize({
|
|
77
|
+
keyId: Platform.select({ ios: Config.DROPPLINK_IOS_KEY_ID, default: Config.DROPPLINK_ANDROID_KEY_ID }),
|
|
78
|
+
secret: Platform.select({ ios: Config.DROPPLINK_IOS_SECRET, default: Config.DROPPLINK_ANDROID_SECRET }),
|
|
79
|
+
debug: __DEV__, // native logs, tag "Dropplink". Never ship true.
|
|
80
|
+
}).catch((e) => console.warn('Dropplink.initialize failed', e));
|
|
81
|
+
|
|
82
|
+
AppRegistry.registerComponent('MyApp', () => App);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`initialize` returns immediately; `/v1/init` is sent in the background by the native SDK. Later calls
|
|
86
|
+
are ignored. You can also initialize natively (`Dropplink.initialize(...)` in `MainApplication.onCreate`
|
|
87
|
+
/ `AppDelegate`) if you want it to run before the JS bundle loads. The JS `initialize` is then a no-op,
|
|
88
|
+
but pass `platformWrapper = "rn"` in the native config so the dashboard shows the right SDK.
|
|
89
|
+
|
|
90
|
+
`baseUrl` defaults to `https://attribution.ondropp.app`. Override it only for local testing (for example
|
|
91
|
+
`http://10.0.2.2:4100` from the Android emulator).
|
|
92
|
+
|
|
93
|
+
## Deep links
|
|
94
|
+
|
|
95
|
+
One listener receives both kinds of link:
|
|
96
|
+
|
|
97
|
+
| Link | `isDeferred` | `url` |
|
|
98
|
+
| --- | --- | --- |
|
|
99
|
+
| Fresh install from a Dropplink link (delivered after the first `/v1/init`) | `true` | `null` |
|
|
100
|
+
| App already installed, link tapped (App Link / Universal Link) | `false` | the opened URL |
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
import { useEffect } from 'react';
|
|
104
|
+
import { Dropplink, useDropplinkDeepLink } from '@ondropp/dropplink-react-native';
|
|
105
|
+
|
|
106
|
+
export default function App() {
|
|
107
|
+
// Every Dropplink deep link while App is mounted.
|
|
108
|
+
useDropplinkDeepLink((link) => navigation.navigate(routeFor(link.path), link.params));
|
|
109
|
+
|
|
110
|
+
// Forwards the launch URL and later Linking 'url' events to Dropplink.handleDeepLink.
|
|
111
|
+
useEffect(() => Dropplink.listenToLinking(), []);
|
|
112
|
+
// ...
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Without the hook:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
const sub = Dropplink.addDeepLinkListener((link) => { /* ... */ });
|
|
120
|
+
sub.remove();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
If a link arrives before any JS listener is added (the deferred link usually does, because `/v1/init`
|
|
124
|
+
can finish before your navigator mounts), the native SDK holds the latest one in memory and delivers it
|
|
125
|
+
when the first listener is added. Add your listener where you can navigate, not earlier.
|
|
126
|
+
|
|
127
|
+
`listenToLinking()` wires React Native's `Linking` (`getInitialURL()` and `'url'` events) into
|
|
128
|
+
`handleDeepLink`. It returns an unsubscribe function. You can forward every URL: non-`http(s)` URLs and
|
|
129
|
+
URLs that aren't Dropplink links for your app are ignored natively (the listener isn't called). If you
|
|
130
|
+
already handle `Linking` yourself, call `Dropplink.handleDeepLink(url)` from there instead.
|
|
131
|
+
|
|
132
|
+
## App Links (Android)
|
|
133
|
+
|
|
134
|
+
1. Register your link domain (e.g. `go.brand.com`) for the app in the Dropplink dashboard. Dropplink serves
|
|
135
|
+
`/.well-known/assetlinks.json` for it; add your release (and Play App Signing) SHA-256 fingerprints there.
|
|
136
|
+
2. Add a verified intent filter to the main activity in `android/app/src/main/AndroidManifest.xml`:
|
|
137
|
+
|
|
138
|
+
```xml
|
|
139
|
+
<activity android:name=".MainActivity" android:launchMode="singleTask" ...>
|
|
140
|
+
<intent-filter android:autoVerify="true">
|
|
141
|
+
<action android:name="android.intent.action.VIEW" />
|
|
142
|
+
<category android:name="android.intent.category.DEFAULT" />
|
|
143
|
+
<category android:name="android.intent.category.BROWSABLE" />
|
|
144
|
+
<data android:scheme="https" android:host="go.brand.com" />
|
|
145
|
+
<!-- One path segment only, so custom-redirect links (/w/<code>) open in the browser. -->
|
|
146
|
+
<data android:pathAdvancedPattern="/[a-z0-9_-]+" />
|
|
147
|
+
</intent-filter>
|
|
148
|
+
</activity>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
React Native's `Linking` already reads the intent's URL (launch and `onNewIntent`), so
|
|
152
|
+
`listenToLinking()` is all the JS needs. Check verification with
|
|
153
|
+
`adb shell pm get-app-links <your.package>`.
|
|
154
|
+
|
|
155
|
+
## Universal Links (iOS)
|
|
156
|
+
|
|
157
|
+
1. Register your link domain for the app in the Dropplink dashboard. Dropplink serves
|
|
158
|
+
`apple-app-site-association` for it.
|
|
159
|
+
2. Xcode → target → **Signing & Capabilities → + Associated Domains** → `applinks:go.brand.com`.
|
|
160
|
+
3. Let React Native's `Linking` see Universal Links. In `AppDelegate`, forward user activities to
|
|
161
|
+
`RCTLinkingManager` (the React Native template may already include this):
|
|
162
|
+
|
|
163
|
+
```swift
|
|
164
|
+
// AppDelegate.swift
|
|
165
|
+
import React
|
|
166
|
+
|
|
167
|
+
func application(_ application: UIApplication, continue userActivity: NSUserActivity,
|
|
168
|
+
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
|
|
169
|
+
RCTLinkingManager.application(application, continue: userActivity, restorationHandler: restorationHandler)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
func application(_ app: UIApplication, open url: URL,
|
|
173
|
+
options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
|
|
174
|
+
RCTLinkingManager.application(app, open: url, options: options)
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
```objc
|
|
179
|
+
// AppDelegate.mm
|
|
180
|
+
#import <React/RCTLinkingManager.h>
|
|
181
|
+
|
|
182
|
+
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
|
|
183
|
+
restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> *))restorationHandler {
|
|
184
|
+
return [RCTLinkingManager application:application continueUserActivity:userActivity
|
|
185
|
+
restorationHandler:restorationHandler];
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Then `listenToLinking()` forwards them. Tap links from Notes or Messages to test; typing one into Safari's
|
|
190
|
+
address bar doesn't open Universal Links.
|
|
191
|
+
|
|
192
|
+
## Events, revenue and users
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
// Funnel events. Names are snake_case; up to 20 props of string, number, boolean or null.
|
|
196
|
+
Dropplink.track('add_to_cart', { props: { sku: 'A1', qty: 2 } });
|
|
197
|
+
Dropplink.track('purchase', { revenue: { amount: 499, currency: 'INR', orderId: 'ord_123' } });
|
|
198
|
+
|
|
199
|
+
// After sign-up / login. Calling it again with the same id is a no-op, so every launch is fine.
|
|
200
|
+
// Pass an opaque id, not an email. email / phone are optional and hashed on the device.
|
|
201
|
+
Dropplink.identify(user.id, { email: user.email, phone: user.phone });
|
|
202
|
+
|
|
203
|
+
// On logout. Later events carry no user.
|
|
204
|
+
Dropplink.logout();
|
|
205
|
+
|
|
206
|
+
// Consent: false clears queued events and stops events and identify, persisted across launches.
|
|
207
|
+
// Install attribution and deep links keep working.
|
|
208
|
+
Dropplink.setTrackingEnabled(userConsented);
|
|
209
|
+
const enabled = await Dropplink.isTrackingEnabled();
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
These calls return immediately and never throw. The native SDK checks each event (invalid ones are dropped
|
|
213
|
+
with a debug log), keeps a queue on disk and sends it in the background, after `/v1/init` and with
|
|
214
|
+
retries.
|
|
215
|
+
|
|
216
|
+
**Revenue:** revenue sent with `track` is shown in the dashboard as **app-reported**. Purchases that pay
|
|
217
|
+
creators must come from your server: send them to `/v1/s2s/events` from your backend when the order is
|
|
218
|
+
confirmed, and use `track` in the app for the funnel (views, add to cart, checkout started).
|
|
219
|
+
|
|
220
|
+
## Privacy
|
|
221
|
+
|
|
222
|
+
The native SDKs do the collecting, so what's sent and stored is exactly as on Android and iOS:
|
|
223
|
+
[docs/privacy.md](../docs/privacy.md). Store answers (Play Data safety, App Privacy) per feature:
|
|
224
|
+
[docs/store-disclosures.md](../docs/store-disclosures.md). Consent for EU / UK users (call `initialize` only
|
|
225
|
+
after consent, `setTrackingEnabled(false)` on withdrawal) and children's apps: [docs/consent.md](../docs/consent.md).
|
|
226
|
+
|
|
227
|
+
## API
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import { Dropplink, useDropplinkDeepLink } from '@ondropp/dropplink-react-native';
|
|
231
|
+
|
|
232
|
+
Dropplink.initialize(config: DropplinkConfig): Promise<void>
|
|
233
|
+
Dropplink.addDeepLinkListener(listener: (link: DeepLink) => void): { remove(): void }
|
|
234
|
+
Dropplink.handleDeepLink(url: string): Promise<void>
|
|
235
|
+
Dropplink.getAttribution(): Promise<Attribution | null> // null before the first successful init
|
|
236
|
+
Dropplink.getInstallId(): Promise<string | null> // null before initialize
|
|
237
|
+
Dropplink.listenToLinking(): () => void // returns unsubscribe
|
|
238
|
+
Dropplink.track(name: string, options?: { props?: EventProps; revenue?: Revenue }): void
|
|
239
|
+
Dropplink.identify(userId: string, traits?: IdentifyTraits): void
|
|
240
|
+
Dropplink.logout(): void
|
|
241
|
+
Dropplink.setTrackingEnabled(enabled: boolean): void
|
|
242
|
+
Dropplink.isTrackingEnabled(): Promise<boolean> // false if the module isn't linked
|
|
243
|
+
useDropplinkDeepLink(handler: (link: DeepLink) => void): void
|
|
244
|
+
|
|
245
|
+
type DropplinkConfig = { keyId: string; secret: string; debug?: boolean; baseUrl?: string };
|
|
246
|
+
type DeepLink = { path: string; params: Record<string, string>; url: string | null; isDeferred: boolean };
|
|
247
|
+
type Attribution = {
|
|
248
|
+
type: 'attributed' | 'organic';
|
|
249
|
+
method: 'install_referrer' | 'fingerprint' | 'none';
|
|
250
|
+
clickId?: string; linkId?: string; campaignId?: string; creatorId?: string;
|
|
251
|
+
source?: string; confidence?: number; attributedAt?: string;
|
|
252
|
+
};
|
|
253
|
+
type EventProps = Record<string, string | number | boolean | null>;
|
|
254
|
+
type Revenue = { amount: number; currency: string; orderId?: string };
|
|
255
|
+
type IdentifyTraits = { email?: string; phone?: string };
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Promises resolve once the native call has been handed off; they don't wait for the network. The event and
|
|
259
|
+
user calls never throw (if the module isn't linked they do nothing and warn once in development). The other
|
|
260
|
+
promises reject
|
|
261
|
+
only if the native module isn't linked, or (`initialize`, Android) on invalid config in a debuggable
|
|
262
|
+
build of the host app, so the mistake shows up during development; release builds log it and disable
|
|
263
|
+
the SDK. Network and server errors never reach JS; they are logged natively when `debug` is on.
|
|
264
|
+
`getAttribution()` is cached on disk natively, so later launches have it immediately.
|
|
265
|
+
|
|
266
|
+
## Test your integration
|
|
267
|
+
|
|
268
|
+
1. Run with `debug: true`. Logcat (`adb logcat -s Dropplink`) or the Xcode console shows:
|
|
269
|
+
|
|
270
|
+
```
|
|
271
|
+
Dropplink: install id 6f1c2a9e-… (paste into Dropplink → Install debugger)
|
|
272
|
+
Dropplink: init 200 attributed via install_referrer, link 6711a0…, deep link /product/123
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Or show `await Dropplink.getInstallId()` on a debug screen.
|
|
276
|
+
2. Paste the install id into **Dropplink dashboard → Install debugger** to see the device's inits, its
|
|
277
|
+
attribution and the click it matched.
|
|
278
|
+
3. Deferred deep link: uninstall, tap a Dropplink link, install and launch. Android attributes by Play
|
|
279
|
+
Install Referrer (install through a Play internal test track to get a real referrer); iOS by
|
|
280
|
+
fingerprint (same network). The listener receives the link with `isDeferred: true`.
|
|
281
|
+
4. App Link / Universal Link: with the app installed, tap a link. The listener receives it with
|
|
282
|
+
`isDeferred: false`.
|
|
283
|
+
|
|
284
|
+
The [example](./example) app shows the install id, attribution and last deep link on one screen.
|
|
285
|
+
|
|
286
|
+
## Troubleshooting
|
|
287
|
+
|
|
288
|
+
| Symptom | Cause | Fix |
|
|
289
|
+
| --- | --- | --- |
|
|
290
|
+
| `'@ondropp/dropplink-react-native' doesn't seem to be linked` | Native code isn't in the binary. | `pod install`, rebuild the app; use a development build, not Expo Go. |
|
|
291
|
+
| `Invalid DropplinkConfig … Dropplink is disabled` (Android) / `invalid config, SDK disabled` (iOS) | Empty `keyId` / `secret`, or `keyId` doesn't start with `ak_`. Usually the `.env` value didn't reach the build. | Log `Config.DROPPLINK_…` presence (not the value); rebuild after editing `.env`. |
|
|
292
|
+
| `401 invalid_key` | Key unknown or revoked, or the app was deleted. | Copy the key again from the dashboard. |
|
|
293
|
+
| `400 platform_mismatch` | Android key in the iOS app or vice versa. | Pick the key per platform with `Platform.select`. |
|
|
294
|
+
| `401 bad_signature` | Wrong secret for the key id. | Copy the secret again; check for trailing spaces. |
|
|
295
|
+
| `401 stale_timestamp` | Device clock is off by more than 5 minutes. Retried. | Set the device to automatic time. |
|
|
296
|
+
| `network error` / `5xx` / `429` | Retried 3 times (1 s, 2 s, 4 s), then next launch. | Check connectivity and `baseUrl`. |
|
|
297
|
+
| Deep link listener never called | No listener added, or the URL never reached `handleDeepLink`. | Add the listener / `useDropplinkDeepLink`; call `listenToLinking()`; check App Links verification / Associated Domains and the iOS `RCTLinkingManager` hooks. |
|
|
298
|
+
| `not a Dropplink link` (debug log) | `handleDeepLink` got another URL. | Expected; ignore. |
|
|
299
|
+
| `no deep link path` / `no path for this link` (debug log) | The link has no deep link path set. | Set one on the link in the dashboard. |
|
|
300
|
+
| Listener fires twice | Two listeners added (e.g. hook in two mounted components). | Keep one listener near your navigator. |
|
|
301
|
+
|
|
302
|
+
## Architecture
|
|
303
|
+
|
|
304
|
+
The module is a **classic native module** (`ReactContextBaseJavaModule` on Android, `RCTEventEmitter` +
|
|
305
|
+
`RCT_EXTERN_MODULE` on iOS) looked up through `TurboModuleRegistry.get('RNDropplink')`, with a typed
|
|
306
|
+
`Spec` in [`src/NativeDropplink.ts`](./src/NativeDropplink.ts).
|
|
307
|
+
|
|
308
|
+
Why not a codegen TurboModule: React Native 0.76+ enables the New Architecture by default and serves
|
|
309
|
+
classic modules through its interop layer, so this one module works on both architectures with no
|
|
310
|
+
codegen, no generated C++ and no `#ifdef RCT_NEW_ARCH_ENABLED` branches. Every call is a cheap,
|
|
311
|
+
asynchronous hand-off to the native SDK, so a TurboModule's synchronous JSI calls would buy nothing. If the
|
|
312
|
+
interop layer is ever removed, adding `codegenConfig` to `package.json` and making the native classes
|
|
313
|
+
extend the generated specs is a contained change; the JS API and `Spec` stay the same.
|
|
314
|
+
|
|
315
|
+
Deep links are emitted as the `DropplinkDeepLink` event (`RCTDeviceEventEmitter` on Android,
|
|
316
|
+
`sendEvent` on iOS). The JS adds the native SDK listener when its first JS listener is added and removes it
|
|
317
|
+
when the last one goes, so the native SDK keeps buffering links while nobody in JS is listening. On a JS
|
|
318
|
+
reload the native listener is cleared too.
|
|
319
|
+
|
|
320
|
+
The native config is always created with `platformWrapper = "rn"`, so the server sees `sdkVersion`
|
|
321
|
+
`0.2.1-rn`.
|
|
322
|
+
|
|
323
|
+
## Development
|
|
324
|
+
|
|
325
|
+
```sh
|
|
326
|
+
npm ci
|
|
327
|
+
npm run typecheck
|
|
328
|
+
npm run lint
|
|
329
|
+
npm test # Jest, native module mocked
|
|
330
|
+
npm run prepare # react-native-builder-bob → lib/
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Native code is compiled by the host app's build. To try it end to end, see [example/README.md](./example/README.md).
|
|
334
|
+
CI: [`.github/workflows/react-native.yml`](../.github/workflows/react-native.yml).
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.RNDropplink = [
|
|
3
|
+
kotlinVersion: "2.0.21",
|
|
4
|
+
minSdkVersion: 24,
|
|
5
|
+
compileSdkVersion: 36
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
ext.getExtOrDefault = { prop ->
|
|
9
|
+
if (rootProject.ext.has(prop)) {
|
|
10
|
+
return rootProject.ext.get(prop)
|
|
11
|
+
}
|
|
12
|
+
return RNDropplink[prop]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
repositories {
|
|
16
|
+
google()
|
|
17
|
+
mavenCentral()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
dependencies {
|
|
21
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
22
|
+
// noinspection DifferentKotlinGradleVersion
|
|
23
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
apply plugin: "com.android.library"
|
|
28
|
+
|
|
29
|
+
if (project.extensions.findByName("kotlin") == null) {
|
|
30
|
+
apply plugin: "kotlin-android"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
repositories {
|
|
34
|
+
google()
|
|
35
|
+
mavenCentral()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
android {
|
|
39
|
+
namespace "app.ondropp.dropplink.reactnative"
|
|
40
|
+
|
|
41
|
+
compileSdkVersion getExtOrDefault("compileSdkVersion")
|
|
42
|
+
|
|
43
|
+
defaultConfig {
|
|
44
|
+
minSdkVersion getExtOrDefault("minSdkVersion")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
compileOptions {
|
|
48
|
+
sourceCompatibility JavaVersion.VERSION_17
|
|
49
|
+
targetCompatibility JavaVersion.VERSION_17
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
dependencies {
|
|
54
|
+
// Version supplied by the app's React Native Gradle plugin.
|
|
55
|
+
implementation "com.facebook.react:react-android"
|
|
56
|
+
// `api` so apps can also call Dropplink.initialize natively from MainApplication.
|
|
57
|
+
api "app.ondropp:dropplink-android:0.2.1"
|
|
58
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
package app.ondropp.dropplink.reactnative
|
|
2
|
+
|
|
3
|
+
import app.ondropp.dropplink.DeepLink
|
|
4
|
+
import app.ondropp.dropplink.DeepLinkListener
|
|
5
|
+
import app.ondropp.dropplink.Dropplink
|
|
6
|
+
import app.ondropp.dropplink.DropplinkConfig
|
|
7
|
+
import app.ondropp.dropplink.Revenue
|
|
8
|
+
import com.facebook.react.bridge.Arguments
|
|
9
|
+
import com.facebook.react.bridge.Promise
|
|
10
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
11
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
12
|
+
import com.facebook.react.bridge.ReactMethod
|
|
13
|
+
import com.facebook.react.bridge.ReadableMap
|
|
14
|
+
import com.facebook.react.bridge.ReadableType
|
|
15
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Thin bridge to the native Dropplink SDK. All behaviour (storage, signing, retries, pending-link
|
|
19
|
+
* buffering) lives in `app.ondropp:dropplink-android`; this class only converts arguments.
|
|
20
|
+
*
|
|
21
|
+
* A classic native module: React Native 0.76+ serves it to JS through the TurboModule interop
|
|
22
|
+
* layer under the New Architecture.
|
|
23
|
+
*/
|
|
24
|
+
class RNDropplinkModule(reactContext: ReactApplicationContext) :
|
|
25
|
+
ReactContextBaseJavaModule(reactContext) {
|
|
26
|
+
|
|
27
|
+
override fun getName(): String = NAME
|
|
28
|
+
|
|
29
|
+
@ReactMethod
|
|
30
|
+
fun initializeSdk(config: ReadableMap, promise: Promise) {
|
|
31
|
+
try {
|
|
32
|
+
val base = DropplinkConfig(
|
|
33
|
+
keyId = config.stringOrNull("keyId").orEmpty(),
|
|
34
|
+
secret = config.stringOrNull("secret").orEmpty(),
|
|
35
|
+
debug = config.hasKey("debug") && !config.isNull("debug") && config.getBoolean("debug"),
|
|
36
|
+
platformWrapper = PLATFORM_WRAPPER,
|
|
37
|
+
)
|
|
38
|
+
// Leave the SDK's default baseUrl in place unless JS passed one.
|
|
39
|
+
val sdkConfig = config.stringOrNull("baseUrl")?.let { base.copy(baseUrl = it) } ?: base
|
|
40
|
+
Dropplink.initialize(reactApplicationContext.applicationContext, sdkConfig)
|
|
41
|
+
promise.resolve(null)
|
|
42
|
+
} catch (e: Exception) {
|
|
43
|
+
// The SDK only throws for invalid config in debug builds of the host app.
|
|
44
|
+
promise.reject("dropplink_initialize_failed", e.message, e)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@ReactMethod
|
|
49
|
+
fun handleDeepLink(url: String, promise: Promise) {
|
|
50
|
+
Dropplink.handleDeepLink(url)
|
|
51
|
+
promise.resolve(null)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
@ReactMethod
|
|
55
|
+
fun getAttribution(promise: Promise) {
|
|
56
|
+
promise.resolve(Dropplink.getAttribution()?.let { Arguments.makeNativeMap(it.toMap()) })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@ReactMethod
|
|
60
|
+
fun getInstallId(promise: Promise) {
|
|
61
|
+
promise.resolve(Dropplink.getInstallId())
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* JS calls this with `true` when its first deep-link listener is added (the SDK then delivers
|
|
66
|
+
* any link it buffered) and `false` when the last one is removed (the SDK buffers again).
|
|
67
|
+
*/
|
|
68
|
+
@ReactMethod
|
|
69
|
+
fun setDeepLinkListenerEnabled(enabled: Boolean) {
|
|
70
|
+
Dropplink.setDeepLinkListener(if (enabled) DeepLinkListener { emitDeepLink(it) } else null)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Events and users (sdk-spec section 11). The SDK validates, queues and sends; it never throws.
|
|
74
|
+
|
|
75
|
+
@ReactMethod
|
|
76
|
+
fun track(name: String, props: ReadableMap?, revenue: ReadableMap?) {
|
|
77
|
+
Dropplink.track(name, props?.toProps(), revenue?.toRevenue())
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@ReactMethod
|
|
81
|
+
fun identify(userId: String, email: String?, phone: String?) {
|
|
82
|
+
Dropplink.identify(userId, email, phone)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@ReactMethod
|
|
86
|
+
fun logout() {
|
|
87
|
+
Dropplink.logout()
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@ReactMethod
|
|
91
|
+
fun setTrackingEnabled(enabled: Boolean) {
|
|
92
|
+
Dropplink.setTrackingEnabled(enabled)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@ReactMethod
|
|
96
|
+
fun isTrackingEnabled(promise: Promise) {
|
|
97
|
+
promise.resolve(Dropplink.isTrackingEnabled())
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Required by NativeEventEmitter; subscription counting is done in JS.
|
|
101
|
+
@ReactMethod
|
|
102
|
+
fun addListener(@Suppress("UNUSED_PARAMETER") eventName: String) = Unit
|
|
103
|
+
|
|
104
|
+
@ReactMethod
|
|
105
|
+
fun removeListeners(@Suppress("UNUSED_PARAMETER") count: Double) = Unit
|
|
106
|
+
|
|
107
|
+
override fun invalidate() {
|
|
108
|
+
// JS context is going away (reload/teardown): let the SDK buffer links again.
|
|
109
|
+
Dropplink.setDeepLinkListener(null)
|
|
110
|
+
super.invalidate()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private fun emitDeepLink(link: DeepLink) {
|
|
114
|
+
if (!reactApplicationContext.hasActiveReactInstance()) return
|
|
115
|
+
reactApplicationContext
|
|
116
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
|
117
|
+
.emit(DEEP_LINK_EVENT, Arguments.makeNativeMap(link.toMap()))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private fun ReadableMap.stringOrNull(key: String): String? =
|
|
121
|
+
if (hasKey(key) && !isNull(key)) getString(key) else null
|
|
122
|
+
|
|
123
|
+
// Booleans stay Boolean and numbers Double, so the SDK sends `true`, not `1`. Lists and maps are
|
|
124
|
+
// passed on as-is so the SDK drops them with its own debug log.
|
|
125
|
+
private fun ReadableMap.toProps(): Map<String, Any?> {
|
|
126
|
+
val out = LinkedHashMap<String, Any?>()
|
|
127
|
+
val keys = keySetIterator()
|
|
128
|
+
while (keys.hasNextKey()) {
|
|
129
|
+
val key = keys.nextKey()
|
|
130
|
+
out[key] = when (getType(key)) {
|
|
131
|
+
ReadableType.Null -> null
|
|
132
|
+
ReadableType.Boolean -> getBoolean(key)
|
|
133
|
+
ReadableType.Number -> getDouble(key)
|
|
134
|
+
ReadableType.String -> getString(key)
|
|
135
|
+
ReadableType.Map -> getMap(key)?.toHashMap()
|
|
136
|
+
ReadableType.Array -> getArray(key)?.toArrayList()
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return out
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// A missing or mistyped amount / currency becomes NaN / "", which the SDK rejects (dropping the
|
|
143
|
+
// event with a debug log), so validation stays in one place.
|
|
144
|
+
private fun ReadableMap.toRevenue(): Revenue = Revenue(
|
|
145
|
+
amount = if (isType("amount", ReadableType.Number)) getDouble("amount") else Double.NaN,
|
|
146
|
+
currency = if (isType("currency", ReadableType.String)) getString("currency").orEmpty() else "",
|
|
147
|
+
orderId = if (isType("orderId", ReadableType.String)) getString("orderId") else null,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
private fun ReadableMap.isType(key: String, type: ReadableType): Boolean =
|
|
151
|
+
hasKey(key) && getType(key) == type
|
|
152
|
+
|
|
153
|
+
companion object {
|
|
154
|
+
const val NAME = "RNDropplink"
|
|
155
|
+
const val DEEP_LINK_EVENT = "DropplinkDeepLink"
|
|
156
|
+
private const val PLATFORM_WRAPPER = "rn"
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
package app.ondropp.dropplink.reactnative
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
|
|
9
|
+
/** Picked up by React Native autolinking. */
|
|
10
|
+
class RNDropplinkPackage : BaseReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
|
|
12
|
+
if (name == RNDropplinkModule.NAME) RNDropplinkModule(reactContext) else null
|
|
13
|
+
|
|
14
|
+
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
|
|
15
|
+
mapOf(
|
|
16
|
+
RNDropplinkModule.NAME to ReactModuleInfo(
|
|
17
|
+
name = RNDropplinkModule.NAME,
|
|
18
|
+
className = RNDropplinkModule::class.java.name,
|
|
19
|
+
canOverrideExistingModule = false,
|
|
20
|
+
needsEagerInit = false,
|
|
21
|
+
isCxxModule = false,
|
|
22
|
+
// Classic module, served through the New Architecture's interop layer.
|
|
23
|
+
isTurboModule = false,
|
|
24
|
+
),
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "dropplink-react-native"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/ondropp/dropplink-sdks.git", :tag => "v#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,swift}"
|
|
17
|
+
s.swift_version = "5.9"
|
|
18
|
+
|
|
19
|
+
# The native SDK holds all the logic; this pod is only the bridge.
|
|
20
|
+
s.dependency "Dropplink", "0.2.1"
|
|
21
|
+
|
|
22
|
+
# React Native's helper adds React-Core and the New Architecture dependencies.
|
|
23
|
+
if respond_to?(:install_modules_dependencies, true)
|
|
24
|
+
install_modules_dependencies(s)
|
|
25
|
+
else
|
|
26
|
+
s.dependency "React-Core"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#import <React/RCTBridgeModule.h>
|
|
2
|
+
#import <React/RCTEventEmitter.h>
|
|
3
|
+
|
|
4
|
+
// Exposes the Swift class in RNDropplink.swift to React Native. Selectors must match its @objc names.
|
|
5
|
+
@interface RCT_EXTERN_MODULE(RNDropplink, RCTEventEmitter)
|
|
6
|
+
|
|
7
|
+
RCT_EXTERN_METHOD(initializeSdk:(NSDictionary *)config
|
|
8
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
9
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
10
|
+
|
|
11
|
+
RCT_EXTERN_METHOD(handleDeepLink:(NSString *)url
|
|
12
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
13
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
14
|
+
|
|
15
|
+
RCT_EXTERN_METHOD(getAttribution:(RCTPromiseResolveBlock)resolve
|
|
16
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
17
|
+
|
|
18
|
+
RCT_EXTERN_METHOD(getInstallId:(RCTPromiseResolveBlock)resolve
|
|
19
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
20
|
+
|
|
21
|
+
RCT_EXTERN_METHOD(setDeepLinkListenerEnabled:(BOOL)enabled)
|
|
22
|
+
|
|
23
|
+
RCT_EXTERN_METHOD(track:(NSString *)name
|
|
24
|
+
props:(nullable NSDictionary *)props
|
|
25
|
+
revenue:(nullable NSDictionary *)revenue)
|
|
26
|
+
|
|
27
|
+
RCT_EXTERN_METHOD(identify:(NSString *)userId
|
|
28
|
+
email:(nullable NSString *)email
|
|
29
|
+
phone:(nullable NSString *)phone)
|
|
30
|
+
|
|
31
|
+
RCT_EXTERN_METHOD(logout)
|
|
32
|
+
|
|
33
|
+
RCT_EXTERN_METHOD(setTrackingEnabled:(BOOL)enabled)
|
|
34
|
+
|
|
35
|
+
RCT_EXTERN_METHOD(isTrackingEnabled:(RCTPromiseResolveBlock)resolve
|
|
36
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
37
|
+
|
|
38
|
+
// addListener: / removeListeners: are inherited from RCTEventEmitter.
|
|
39
|
+
|
|
40
|
+
@end
|