@kissmyappss/react-native 1.0.0 → 1.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/CHANGELOG.md +43 -0
- package/KissMyAppsReactNative.podspec +29 -0
- package/README.md +187 -30
- package/android/build.gradle +65 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/AndroidManifestNew.xml +1 -0
- package/android/src/main/java/com/kissmyapps/reactnative/KissMyAppsAttributionModule.kt +138 -0
- package/android/src/main/java/com/kissmyapps/reactnative/KissMyAppsAttributionPackage.kt +15 -0
- package/ios/KissMyAppsAttribution.m +10 -0
- package/ios/KissMyAppsAttribution.swift +75 -0
- package/ios/PrivacyInfo.xcprivacy +40 -0
- package/lib/attribution.d.ts +78 -0
- package/lib/attribution.js +153 -0
- package/lib/client.d.ts +81 -1
- package/lib/client.js +434 -5
- package/lib/index.d.ts +26 -2
- package/lib/index.js +67 -2
- package/package.json +12 -3
- package/react-native.config.js +13 -0
- package/src/attribution.ts +207 -0
- package/src/client.ts +494 -5
- package/src/index.ts +79 -4
package/lib/client.js
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.KmaClient = exports.EVENT_NAME = exports.DEFAULT_ENDPOINT = exports.SDK_VERSION = void 0;
|
|
7
7
|
exports.uuid = uuid;
|
|
8
|
-
|
|
8
|
+
const attribution_1 = require("./attribution");
|
|
9
|
+
exports.SDK_VERSION = '1.1.0';
|
|
9
10
|
exports.DEFAULT_ENDPOINT = 'https://events.kissmyapps.dev';
|
|
10
11
|
exports.EVENT_NAME = /^[A-Za-z][A-Za-z0-9_.:-]{0,79}$/;
|
|
11
12
|
const KEY = /^kma_pk_(live|test)_[A-Za-z0-9]{20,64}$/;
|
|
@@ -13,6 +14,24 @@ const BATCH_SIZE = 50;
|
|
|
13
14
|
const FLUSH_AT = 30;
|
|
14
15
|
const HEARTBEAT_MS = 60000;
|
|
15
16
|
const MAX_PROPERTIES_BYTES = 32000;
|
|
17
|
+
// The first launch's call, then at most three more launches.
|
|
18
|
+
const INSTALL_TRIES = 4;
|
|
19
|
+
// A link handed over twice this close together, by Linking and by the app, counts once.
|
|
20
|
+
const SAME_LINK_MS = 3000;
|
|
21
|
+
// As the iOS SDK: the opening link has 1.5 seconds to arrive before the install call goes.
|
|
22
|
+
const INITIAL_LINK_WAIT_MS = 1500;
|
|
23
|
+
// The native module waits up to 10 seconds for Google Play's install referrer.
|
|
24
|
+
const NATIVE_WAIT_MS = 12000;
|
|
25
|
+
const FLUSH_WAIT_MS = 5000;
|
|
26
|
+
// The properties a payment event carries; reports read revenue from them.
|
|
27
|
+
const REVENUE_KEYS = ['kma_revenue', 'kma_currency', 'kma_product_id', 'kma_transaction_id'];
|
|
28
|
+
const MAX_REVENUE = 1e9;
|
|
29
|
+
const CURRENCY = /^[A-Za-z]{3}$/;
|
|
30
|
+
/** A product or transaction ID as reports keep it: text of 1 to 200 characters. */
|
|
31
|
+
function revenueId(value) {
|
|
32
|
+
const id = typeof value === 'string' ? value.trim() : '';
|
|
33
|
+
return id && id.length <= 200 ? id : undefined;
|
|
34
|
+
}
|
|
16
35
|
function uuid() {
|
|
17
36
|
// Hermes has no crypto.randomUUID; a polyfill may add it.
|
|
18
37
|
const random = globalThis.crypto;
|
|
@@ -41,6 +60,18 @@ class KmaClient {
|
|
|
41
60
|
this.inForeground = false;
|
|
42
61
|
this.firstLaunch = false;
|
|
43
62
|
this.stopped = false;
|
|
63
|
+
this.launched = false;
|
|
64
|
+
// This launch is the first launch: its first link goes with the install call.
|
|
65
|
+
this.firstLaunchNow = false;
|
|
66
|
+
this.installRunning = false;
|
|
67
|
+
// This launch's try is on its way (one try per launch).
|
|
68
|
+
this.installSent = false;
|
|
69
|
+
this.installPhase = 'none';
|
|
70
|
+
this.linksBeforeLaunch = [];
|
|
71
|
+
this.waitingLinks = [];
|
|
72
|
+
this.recentLinks = new Map();
|
|
73
|
+
this.deepLinkHandler = null;
|
|
74
|
+
this.heldDeepLink = null;
|
|
44
75
|
if (!KEY.test((_a = options.key) !== null && _a !== void 0 ? _a : '')) {
|
|
45
76
|
throw new Error('KissMyApps: init needs a client key that starts with kma_pk_');
|
|
46
77
|
}
|
|
@@ -58,6 +89,15 @@ class KmaClient {
|
|
|
58
89
|
optedOut: false,
|
|
59
90
|
};
|
|
60
91
|
this.ready = this.load();
|
|
92
|
+
const opening = env.initialLink && options.attribution !== false
|
|
93
|
+
? (0, attribution_1.within)(Promise.resolve().then(() => { var _a, _b; return (_b = (_a = env.initialLink) === null || _a === void 0 ? void 0 : _a.call(env)) !== null && _b !== void 0 ? _b : null; }), INITIAL_LINK_WAIT_MS, null)
|
|
94
|
+
: Promise.resolve(null);
|
|
95
|
+
// Routed before the install call reads it, so the opening link goes with the install.
|
|
96
|
+
this.initialLink = opening.then((url) => {
|
|
97
|
+
if (url)
|
|
98
|
+
this.handleDeepLink(url);
|
|
99
|
+
return url;
|
|
100
|
+
});
|
|
61
101
|
}
|
|
62
102
|
// Public API ---------------------------------------------------------------------------
|
|
63
103
|
/** Records something a person did. */
|
|
@@ -72,6 +112,42 @@ class KmaClient {
|
|
|
72
112
|
this.enqueue(name, properties, this.ensureSession());
|
|
73
113
|
});
|
|
74
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Records a payment: the amount in major units (9.99, not 999), negative for a refund, and
|
|
117
|
+
* its ISO 4217 currency code. Sent as a purchase (or refund) event with kma_revenue and
|
|
118
|
+
* kma_currency. An amount or currency that is not valid sends nothing (a warning in debug).
|
|
119
|
+
*/
|
|
120
|
+
revenue(amount, currency, options = {}) {
|
|
121
|
+
if (typeof amount !== 'number' ||
|
|
122
|
+
!Number.isFinite(amount) ||
|
|
123
|
+
amount === 0 ||
|
|
124
|
+
Math.abs(amount) > MAX_REVENUE) {
|
|
125
|
+
return this.warn('revenue needs an amount that is not 0 and at most 1,000,000,000 either way (negative for a refund).');
|
|
126
|
+
}
|
|
127
|
+
const code = typeof currency === 'string' ? currency.trim() : '';
|
|
128
|
+
if (!CURRENCY.test(code)) {
|
|
129
|
+
return this.warn(`revenue needs a three-letter currency code such as USD, not "${String(currency)}".`);
|
|
130
|
+
}
|
|
131
|
+
const { productId, transactionId, event, properties } = options !== null && options !== void 0 ? options : {};
|
|
132
|
+
const values = properties && typeof properties === 'object' && !Array.isArray(properties)
|
|
133
|
+
? { ...properties }
|
|
134
|
+
: {};
|
|
135
|
+
for (const field of REVENUE_KEYS)
|
|
136
|
+
delete values[field];
|
|
137
|
+
values.kma_revenue = amount;
|
|
138
|
+
values.kma_currency = code.toUpperCase();
|
|
139
|
+
const product = revenueId(productId);
|
|
140
|
+
const transaction = revenueId(transactionId);
|
|
141
|
+
if (product)
|
|
142
|
+
values.kma_product_id = product;
|
|
143
|
+
else if (productId != null)
|
|
144
|
+
this.warn('productId must be 1 to 200 characters; it was left out.');
|
|
145
|
+
if (transaction)
|
|
146
|
+
values.kma_transaction_id = transaction;
|
|
147
|
+
else if (transactionId != null)
|
|
148
|
+
this.warn('transactionId must be 1 to 200 characters; it was left out.');
|
|
149
|
+
this.track(event || (amount > 0 ? 'purchase' : 'refund'), values);
|
|
150
|
+
}
|
|
75
151
|
/** Records a screen view; later events carry the screen's name. */
|
|
76
152
|
screen(name, properties = {}) {
|
|
77
153
|
const screen = String(name !== null && name !== void 0 ? name : '')
|
|
@@ -99,6 +175,8 @@ class KmaClient {
|
|
|
99
175
|
return this.warn('identify needs a user ID of 1 to 200 characters.');
|
|
100
176
|
this.whenLoaded(() => {
|
|
101
177
|
var _a;
|
|
178
|
+
if (this.stopped || this.state.optedOut)
|
|
179
|
+
return;
|
|
102
180
|
this.state.userId = id;
|
|
103
181
|
if (traits)
|
|
104
182
|
this.state.traits = { ...((_a = this.state.traits) !== null && _a !== void 0 ? _a : {}), ...this.clean(traits) };
|
|
@@ -111,6 +189,8 @@ class KmaClient {
|
|
|
111
189
|
setTraits(traits) {
|
|
112
190
|
this.whenLoaded(() => {
|
|
113
191
|
var _a;
|
|
192
|
+
if (this.stopped || this.state.optedOut)
|
|
193
|
+
return;
|
|
114
194
|
this.state.traits = { ...((_a = this.state.traits) !== null && _a !== void 0 ? _a : {}), ...this.clean(traits) };
|
|
115
195
|
this.identityPending = true;
|
|
116
196
|
this.save();
|
|
@@ -120,6 +200,8 @@ class KmaClient {
|
|
|
120
200
|
/** Forgets the user, for example on sign out: what follows is a new person and session. */
|
|
121
201
|
reset() {
|
|
122
202
|
this.whenLoaded(() => {
|
|
203
|
+
if (this.stopped || this.state.optedOut)
|
|
204
|
+
return;
|
|
123
205
|
void this.flush();
|
|
124
206
|
this.state.userId = null;
|
|
125
207
|
this.state.traits = null;
|
|
@@ -135,16 +217,66 @@ class KmaClient {
|
|
|
135
217
|
optOut() {
|
|
136
218
|
this.whenLoaded(() => {
|
|
137
219
|
this.state.optedOut = true;
|
|
138
|
-
this.
|
|
139
|
-
this.save();
|
|
220
|
+
this.clearConsentData();
|
|
140
221
|
});
|
|
141
222
|
}
|
|
223
|
+
clearConsentData() {
|
|
224
|
+
this.queue = [];
|
|
225
|
+
this.state.userId = null;
|
|
226
|
+
this.state.traits = null;
|
|
227
|
+
this.identityPending = false;
|
|
228
|
+
this.state.session = null;
|
|
229
|
+
this.screenName = null;
|
|
230
|
+
this.state.install = null;
|
|
231
|
+
this.firstLaunch = false;
|
|
232
|
+
this.linksBeforeLaunch = [];
|
|
233
|
+
this.waitingLinks = [];
|
|
234
|
+
this.recentLinks.clear();
|
|
235
|
+
this.heldDeepLink = null;
|
|
236
|
+
this.installPhase = 'answered';
|
|
237
|
+
void this.writeNow();
|
|
238
|
+
}
|
|
142
239
|
optIn() {
|
|
143
240
|
this.whenLoaded(() => {
|
|
241
|
+
if (!this.state.optedOut)
|
|
242
|
+
return;
|
|
243
|
+
this.state.anonymousId = uuid();
|
|
144
244
|
this.state.optedOut = false;
|
|
145
245
|
this.save();
|
|
146
246
|
});
|
|
147
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* Reports a link that opened the app, such as a Universal Link or App Link. Any https link
|
|
250
|
+
* is sent and KissMyApps decides whether it is one of the project's links; its deep link then
|
|
251
|
+
* goes to `onDeepLink`. Returns false when nothing is sent (not https, longer than 2,048
|
|
252
|
+
* characters, attribution off, opted out). The same link handed over twice within 3 seconds
|
|
253
|
+
* counts once, so Linking and the app may both report it.
|
|
254
|
+
*/
|
|
255
|
+
handleDeepLink(url) {
|
|
256
|
+
if (this.options.attribution === false || this.stopped)
|
|
257
|
+
return false;
|
|
258
|
+
const link = (0, attribution_1.httpsLink)(url);
|
|
259
|
+
if (!link)
|
|
260
|
+
return false;
|
|
261
|
+
if (this.loaded && this.state.optedOut)
|
|
262
|
+
return false;
|
|
263
|
+
const at = this.now();
|
|
264
|
+
this.whenLoaded(() => this.routeLink(link, at));
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Where deep links go: the deferred one after an install, and one each time a link opens the
|
|
269
|
+
* app. Each deep link is handed over once; one that arrives before a handler is set is held
|
|
270
|
+
* and handed to the next handler set. Pass null to stop.
|
|
271
|
+
*/
|
|
272
|
+
onDeepLink(handler) {
|
|
273
|
+
this.deepLinkHandler = handler;
|
|
274
|
+
const held = this.heldDeepLink;
|
|
275
|
+
if (handler && held) {
|
|
276
|
+
this.heldDeepLink = null;
|
|
277
|
+
this.deliver(held);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
148
280
|
getAnonymousId() {
|
|
149
281
|
return this.state.anonymousId;
|
|
150
282
|
}
|
|
@@ -187,16 +319,20 @@ class KmaClient {
|
|
|
187
319
|
if (this.stopped || this.inForeground)
|
|
188
320
|
return;
|
|
189
321
|
const launch = this.foregroundAt === 0;
|
|
322
|
+
this.launched = true;
|
|
190
323
|
this.inForeground = true;
|
|
191
324
|
this.foregroundAt = this.now();
|
|
192
325
|
if (this.state.optedOut)
|
|
193
326
|
return;
|
|
194
327
|
const session = this.ensureSession();
|
|
328
|
+
const firstLaunch = launch && this.firstLaunch;
|
|
195
329
|
if (this.options.trackLifecycle !== false) {
|
|
196
330
|
if (launch)
|
|
197
331
|
this.recordInstallOrUpdate(session);
|
|
198
332
|
this.enqueue('app_opened', { from_background: !launch }, session);
|
|
199
333
|
}
|
|
334
|
+
if (launch)
|
|
335
|
+
this.attributeLaunch(session, firstLaunch);
|
|
200
336
|
(_a = this.heartbeatTimer) !== null && _a !== void 0 ? _a : (this.heartbeatTimer = setInterval(() => this.heartbeat(), HEARTBEAT_MS));
|
|
201
337
|
});
|
|
202
338
|
}
|
|
@@ -248,12 +384,15 @@ class KmaClient {
|
|
|
248
384
|
optedOut: saved.optedOut === true,
|
|
249
385
|
version: saved.version,
|
|
250
386
|
build: saved.build,
|
|
387
|
+
install: pendingInstall(saved.install),
|
|
251
388
|
};
|
|
252
389
|
}
|
|
253
390
|
// Nothing saved means a first launch, or the app's data was cleared.
|
|
254
391
|
this.firstLaunch = !(saved === null || saved === void 0 ? void 0 : saved.anonymousId);
|
|
255
392
|
this.queue = Array.isArray(queue) ? queue : [];
|
|
256
393
|
this.identityPending = Boolean(this.state.traits);
|
|
394
|
+
if (this.state.optedOut)
|
|
395
|
+
this.clearConsentData();
|
|
257
396
|
this.loaded = true;
|
|
258
397
|
// What an earlier launch could not send goes now.
|
|
259
398
|
if (this.queue.length)
|
|
@@ -424,8 +563,8 @@ class KmaClient {
|
|
|
424
563
|
}
|
|
425
564
|
return batch;
|
|
426
565
|
}
|
|
427
|
-
async post(body) {
|
|
428
|
-
return this.env.fetch(`${this.options.endpoint}
|
|
566
|
+
async post(body, path = '/v1/events') {
|
|
567
|
+
return this.env.fetch(`${this.options.endpoint}${path}`, {
|
|
429
568
|
method: 'POST',
|
|
430
569
|
headers: { 'content-type': 'application/json', authorization: `Bearer ${this.options.key}` },
|
|
431
570
|
body: JSON.stringify(body),
|
|
@@ -502,6 +641,256 @@ class KmaClient {
|
|
|
502
641
|
})
|
|
503
642
|
.catch(() => undefined);
|
|
504
643
|
}
|
|
644
|
+
// Attribution ---------------------------------------------------------------------------
|
|
645
|
+
/** The launch: the first one queues the install call; links that came before it are routed. */
|
|
646
|
+
attributeLaunch(session, firstLaunch) {
|
|
647
|
+
this.firstLaunch = false;
|
|
648
|
+
const early = this.linksBeforeLaunch.splice(0);
|
|
649
|
+
if (this.options.attribution === false)
|
|
650
|
+
return;
|
|
651
|
+
// Without storage every launch looks like the first, so there is no install to match.
|
|
652
|
+
if (firstLaunch && this.env.storage) {
|
|
653
|
+
this.state.install = {
|
|
654
|
+
at: this.now(),
|
|
655
|
+
anonymousId: this.state.anonymousId,
|
|
656
|
+
sessionId: session.id,
|
|
657
|
+
tries: 0,
|
|
658
|
+
};
|
|
659
|
+
this.firstLaunchNow = true;
|
|
660
|
+
this.save();
|
|
661
|
+
}
|
|
662
|
+
if (this.state.install) {
|
|
663
|
+
this.installPhase = 'waiting';
|
|
664
|
+
for (const link of early)
|
|
665
|
+
this.routeLaunched(link.url, link.at);
|
|
666
|
+
void this.sendInstall();
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
for (const link of early)
|
|
670
|
+
void this.sendOpen(link.url, link.at);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
routeLink(url, at) {
|
|
674
|
+
if (this.stopped || this.state.optedOut)
|
|
675
|
+
return;
|
|
676
|
+
for (const [seen, when] of this.recentLinks) {
|
|
677
|
+
if (at - when >= SAME_LINK_MS)
|
|
678
|
+
this.recentLinks.delete(seen);
|
|
679
|
+
}
|
|
680
|
+
if (this.recentLinks.has(url))
|
|
681
|
+
return;
|
|
682
|
+
this.recentLinks.set(url, at);
|
|
683
|
+
if (!this.launched) {
|
|
684
|
+
if (this.linksBeforeLaunch.length < 10)
|
|
685
|
+
this.linksBeforeLaunch.push({ url, at });
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
this.routeLaunched(url, at);
|
|
689
|
+
}
|
|
690
|
+
routeLaunched(url, at) {
|
|
691
|
+
const pending = this.state.install;
|
|
692
|
+
if (pending && this.installPhase === 'waiting') {
|
|
693
|
+
// Only the first link of the first launch goes with the install call.
|
|
694
|
+
if (this.firstLaunchNow && !this.installSent && !pending.tries && !pending.link) {
|
|
695
|
+
pending.link = url;
|
|
696
|
+
this.save();
|
|
697
|
+
}
|
|
698
|
+
else if (this.waitingLinks.length < 10) {
|
|
699
|
+
// Sent once the install is matched; an open first would record an install of its own.
|
|
700
|
+
this.waitingLinks.push({ url, at });
|
|
701
|
+
}
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
if (pending && this.installPhase === 'failed') {
|
|
705
|
+
this.warn('The link was not sent: the install is matched first, on the next launch.');
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
void this.sendOpen(url, at);
|
|
709
|
+
}
|
|
710
|
+
/** The device's details; `complete` is false when the install referrer could not be read in time. */
|
|
711
|
+
async readNative(install, clipboard) {
|
|
712
|
+
const read = this.env.attributionInfo;
|
|
713
|
+
if (!read)
|
|
714
|
+
return { info: {}, complete: true };
|
|
715
|
+
const request = {
|
|
716
|
+
install,
|
|
717
|
+
clipboard: install && clipboard && this.options.clipboardHandoff === true,
|
|
718
|
+
};
|
|
719
|
+
const info = await (0, attribution_1.within)(Promise.resolve()
|
|
720
|
+
.then(() => read(request))
|
|
721
|
+
.then((answer) => (answer && typeof answer === 'object' ? answer : {}), () => ({})), NATIVE_WAIT_MS, null);
|
|
722
|
+
if (!info)
|
|
723
|
+
return { info: {}, complete: false };
|
|
724
|
+
const key = (0, attribution_1.deviceKey)(info.deviceKey);
|
|
725
|
+
if (key)
|
|
726
|
+
this.deviceKeyRead = key;
|
|
727
|
+
return { info, complete: info.retryReferrer !== true };
|
|
728
|
+
}
|
|
729
|
+
/** The first try's details again; the Apple Ads token, and a referrer that timed out, are read anew. */
|
|
730
|
+
async installInfo(pending) {
|
|
731
|
+
const saved = pending.info;
|
|
732
|
+
if (saved && pending.infoComplete && this.env.device().platform === 'android') {
|
|
733
|
+
return { info: saved, complete: true };
|
|
734
|
+
}
|
|
735
|
+
// The clipboard is read on the first try only.
|
|
736
|
+
const read = await this.readNative(true, !saved);
|
|
737
|
+
if (!saved)
|
|
738
|
+
return read;
|
|
739
|
+
return {
|
|
740
|
+
info: { ...read.info, ...definedOnly(saved), appleAdsToken: read.info.appleAdsToken },
|
|
741
|
+
complete: pending.infoComplete === true || read.complete,
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
/** The install call: once, on the first launch; again on the next launches if it failed. */
|
|
745
|
+
async sendInstall() {
|
|
746
|
+
if (this.installRunning)
|
|
747
|
+
return;
|
|
748
|
+
this.installRunning = true;
|
|
749
|
+
try {
|
|
750
|
+
await this.initialLink;
|
|
751
|
+
const first = this.state.install;
|
|
752
|
+
if (!first)
|
|
753
|
+
return this.installAnswered();
|
|
754
|
+
const { info, complete } = await this.installInfo(first);
|
|
755
|
+
// app_installed and its session reach the server first.
|
|
756
|
+
await (0, attribution_1.within)(this.flush(), FLUSH_WAIT_MS, undefined);
|
|
757
|
+
const pending = this.state.install;
|
|
758
|
+
if (!pending)
|
|
759
|
+
return this.installAnswered();
|
|
760
|
+
if (this.stopped || this.state.optedOut || this.installSent)
|
|
761
|
+
return;
|
|
762
|
+
this.installSent = true;
|
|
763
|
+
pending.tries += 1;
|
|
764
|
+
if (!pending.info || !pending.infoComplete) {
|
|
765
|
+
pending.info = savedInfo(info);
|
|
766
|
+
pending.infoComplete = complete;
|
|
767
|
+
}
|
|
768
|
+
const device = this.env.device();
|
|
769
|
+
const body = (0, attribution_1.installBody)({
|
|
770
|
+
anonymousId: pending.anonymousId,
|
|
771
|
+
sessionId: pending.sessionId,
|
|
772
|
+
platform: device.platform,
|
|
773
|
+
installedAt: pending.at,
|
|
774
|
+
sdk: this.env.sdk,
|
|
775
|
+
// Not after a reset: the install belongs to the person who installed.
|
|
776
|
+
userId: pending.anonymousId === this.state.anonymousId ? this.state.userId : null,
|
|
777
|
+
appVersion: this.options.appVersion,
|
|
778
|
+
link: pending.link,
|
|
779
|
+
info,
|
|
780
|
+
clipboard: this.options.clipboardHandoff === true,
|
|
781
|
+
});
|
|
782
|
+
await this.writeNow();
|
|
783
|
+
let response;
|
|
784
|
+
try {
|
|
785
|
+
if (this.state.install !== pending || this.state.optedOut)
|
|
786
|
+
return;
|
|
787
|
+
response = await this.post(body, attribution_1.INSTALL_PATH);
|
|
788
|
+
}
|
|
789
|
+
catch {
|
|
790
|
+
if (this.state.install === pending)
|
|
791
|
+
this.installFailed(pending, 'no connection');
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
if (this.state.install !== pending || this.state.optedOut)
|
|
795
|
+
return;
|
|
796
|
+
if (!response.ok && retryable(response.status)) {
|
|
797
|
+
return this.installFailed(pending, `status ${response.status}`);
|
|
798
|
+
}
|
|
799
|
+
// Answered: a refused body would be refused again, and an unreadable answer is still one.
|
|
800
|
+
this.state.install = null;
|
|
801
|
+
void this.writeNow();
|
|
802
|
+
const answer = await response.text().catch(() => '');
|
|
803
|
+
if (response.ok) {
|
|
804
|
+
this.deliver((0, attribution_1.deepLinkFrom)((0, attribution_1.parseAnswer)(answer), (0, attribution_1.httpsLink)(pending.link)));
|
|
805
|
+
}
|
|
806
|
+
else {
|
|
807
|
+
this.warn(`The install was refused (${response.status}): ${answer}`);
|
|
808
|
+
}
|
|
809
|
+
this.installAnswered();
|
|
810
|
+
}
|
|
811
|
+
finally {
|
|
812
|
+
this.installRunning = false;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
/** Links that waited for the install's answer go to the open call now. */
|
|
816
|
+
installAnswered() {
|
|
817
|
+
this.installPhase = 'answered';
|
|
818
|
+
for (const link of this.waitingLinks.splice(0))
|
|
819
|
+
void this.sendOpen(link.url, link.at);
|
|
820
|
+
}
|
|
821
|
+
installFailed(pending, reason) {
|
|
822
|
+
if (pending.tries >= INSTALL_TRIES) {
|
|
823
|
+
this.state.install = null;
|
|
824
|
+
this.warn(`The install could not be matched (${reason}); giving up.`);
|
|
825
|
+
this.installAnswered();
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
this.warn(`The install could not be matched yet (${reason}); trying again next launch.`);
|
|
829
|
+
this.installPhase = 'failed';
|
|
830
|
+
if (this.waitingLinks.splice(0).length) {
|
|
831
|
+
this.warn('Links that opened the app were not sent: the install is matched first.');
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
void this.writeNow();
|
|
835
|
+
}
|
|
836
|
+
/** The open call, for a link that opened the app after the install was matched. Not retried. */
|
|
837
|
+
async sendOpen(url, at) {
|
|
838
|
+
var _a;
|
|
839
|
+
if (this.stopped || this.state.optedOut)
|
|
840
|
+
return;
|
|
841
|
+
const session = this.ensureSession();
|
|
842
|
+
const anonymousId = this.state.anonymousId;
|
|
843
|
+
const userId = this.state.userId;
|
|
844
|
+
const key = (_a = this.deviceKeyRead) !== null && _a !== void 0 ? _a : (0, attribution_1.deviceKey)((await this.readNative(false, false)).info.deviceKey);
|
|
845
|
+
if (this.stopped || this.state.optedOut || anonymousId !== this.state.anonymousId)
|
|
846
|
+
return;
|
|
847
|
+
const body = (0, attribution_1.openBody)({
|
|
848
|
+
anonymousId,
|
|
849
|
+
sessionId: session.id,
|
|
850
|
+
platform: this.env.device().platform,
|
|
851
|
+
url,
|
|
852
|
+
openedAt: at,
|
|
853
|
+
sdk: this.env.sdk,
|
|
854
|
+
userId,
|
|
855
|
+
appVersion: this.options.appVersion,
|
|
856
|
+
deviceKey: key,
|
|
857
|
+
});
|
|
858
|
+
try {
|
|
859
|
+
const response = await this.post(body, attribution_1.OPEN_PATH);
|
|
860
|
+
const answer = await response.text().catch(() => '');
|
|
861
|
+
if (this.state.optedOut || anonymousId !== this.state.anonymousId)
|
|
862
|
+
return;
|
|
863
|
+
if (!response.ok) {
|
|
864
|
+
this.warn(`The link was refused (${response.status}): ${answer}`);
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
this.deliver((0, attribution_1.deepLinkFrom)((0, attribution_1.parseAnswer)(answer), url));
|
|
868
|
+
}
|
|
869
|
+
catch {
|
|
870
|
+
this.warn('The link could not be sent: no connection.');
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
/** Hands a deep link to the handler; without one, the newest is held for the next handler. */
|
|
874
|
+
deliver(link) {
|
|
875
|
+
if (!link || this.stopped || this.state.optedOut)
|
|
876
|
+
return;
|
|
877
|
+
const anonymousId = this.state.anonymousId;
|
|
878
|
+
const handler = this.deepLinkHandler;
|
|
879
|
+
if (!handler) {
|
|
880
|
+
this.heldDeepLink = link;
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
// Later, so a handler set during a render or an effect runs after it.
|
|
884
|
+
setTimeout(() => {
|
|
885
|
+
try {
|
|
886
|
+
if (!this.state.optedOut && anonymousId === this.state.anonymousId)
|
|
887
|
+
handler(link);
|
|
888
|
+
}
|
|
889
|
+
catch (error) {
|
|
890
|
+
this.warn(`onDeepLink threw: ${String(error)}`);
|
|
891
|
+
}
|
|
892
|
+
}, 0);
|
|
893
|
+
}
|
|
505
894
|
warn(message) {
|
|
506
895
|
var _a;
|
|
507
896
|
if (this.options.debug)
|
|
@@ -509,3 +898,43 @@ class KmaClient {
|
|
|
509
898
|
}
|
|
510
899
|
}
|
|
511
900
|
exports.KmaClient = KmaClient;
|
|
901
|
+
function retryable(status) {
|
|
902
|
+
return status >= 500 || status === 408 || status === 429 || status < 400;
|
|
903
|
+
}
|
|
904
|
+
function pendingInstall(value) {
|
|
905
|
+
if (!value || typeof value !== 'object')
|
|
906
|
+
return null;
|
|
907
|
+
const saved = value;
|
|
908
|
+
if (typeof saved.at !== 'number' ||
|
|
909
|
+
typeof saved.anonymousId !== 'string' ||
|
|
910
|
+
typeof saved.sessionId !== 'string')
|
|
911
|
+
return null;
|
|
912
|
+
const info = saved.info && typeof saved.info === 'object' ? savedInfo(saved.info) : undefined;
|
|
913
|
+
return {
|
|
914
|
+
at: saved.at,
|
|
915
|
+
anonymousId: saved.anonymousId,
|
|
916
|
+
sessionId: saved.sessionId,
|
|
917
|
+
link: typeof saved.link === 'string' ? saved.link : undefined,
|
|
918
|
+
tries: typeof saved.tries === 'number' ? saved.tries : 0,
|
|
919
|
+
info,
|
|
920
|
+
infoComplete: info ? saved.infoComplete === true : undefined,
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
/** What is kept for the next tries: everything but the Apple Ads token, which expires. */
|
|
924
|
+
function savedInfo(info) {
|
|
925
|
+
const kept = {};
|
|
926
|
+
if (typeof info.deviceKey === 'string')
|
|
927
|
+
kept.deviceKey = info.deviceKey;
|
|
928
|
+
if (typeof info.referrer === 'string')
|
|
929
|
+
kept.referrer = info.referrer;
|
|
930
|
+
if (typeof info.referrerClickTs === 'number')
|
|
931
|
+
kept.referrerClickTs = info.referrerClickTs;
|
|
932
|
+
if (typeof info.installBeginTs === 'number')
|
|
933
|
+
kept.installBeginTs = info.installBeginTs;
|
|
934
|
+
if (typeof info.clipboardClick === 'string')
|
|
935
|
+
kept.clipboardClick = info.clipboardClick;
|
|
936
|
+
return kept;
|
|
937
|
+
}
|
|
938
|
+
function definedOnly(info) {
|
|
939
|
+
return Object.fromEntries(Object.entries(info).filter(([, value]) => value !== undefined && value !== null));
|
|
940
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,17 +1,41 @@
|
|
|
1
|
+
import type { DeepLink } from './attribution';
|
|
1
2
|
import { KmaClient } from './client';
|
|
2
|
-
import type { ClientOptions, Properties, Storage } from './client';
|
|
3
|
+
import type { ClientOptions, Properties, RevenueOptions, Storage } from './client';
|
|
3
4
|
import type { NavigationRefLike } from './navigation';
|
|
4
5
|
export { KmaClient, SDK_VERSION } from './client';
|
|
5
|
-
export type { ClientOptions, DeviceInfo, Properties, Storage } from './client';
|
|
6
|
+
export type { ClientOptions, DeviceInfo, Properties, RevenueOptions, Storage } from './client';
|
|
7
|
+
export type { AttributionInfo, DeepLink } from './attribution';
|
|
6
8
|
export type { NavigationRefLike } from './navigation';
|
|
7
9
|
export interface InitOptions extends ClientOptions {
|
|
8
10
|
/** Where the SDK keeps its IDs and offline queue, such as AsyncStorage. */
|
|
9
11
|
storage?: Storage;
|
|
12
|
+
/**
|
|
13
|
+
* Read the link that opened the app (Linking.getInitialURL) and links opened while it runs
|
|
14
|
+
* (Linking's `url` events). Default true. Turn it off to call handleDeepLink yourself.
|
|
15
|
+
*/
|
|
16
|
+
deepLinks?: boolean;
|
|
10
17
|
}
|
|
11
18
|
export declare const KissMyApps: {
|
|
12
19
|
/** Starts the SDK once, as early as possible, with the project's client key. */
|
|
13
20
|
init(options: InitOptions): KmaClient;
|
|
21
|
+
/**
|
|
22
|
+
* Reports a link that opened the app. With `deepLinks` on (the default) the SDK already
|
|
23
|
+
* reads Linking, so call this only for links that reach the app another way. Returns false
|
|
24
|
+
* when the link is not sent (not an https link, attribution off, opted out).
|
|
25
|
+
*/
|
|
26
|
+
handleDeepLink(url: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Where deep links go: the deferred one after an install, and one each time a link opens
|
|
29
|
+
* the app. Each is handed over once; one that arrives before a handler is set waits for it.
|
|
30
|
+
* Returns a function that removes the handler.
|
|
31
|
+
*/
|
|
32
|
+
onDeepLink(handler: (link: DeepLink) => void): () => void;
|
|
14
33
|
track(name: string, properties?: Properties): void;
|
|
34
|
+
/**
|
|
35
|
+
* Records a payment: the amount in major units (9.99, not 999), negative for a refund, and
|
|
36
|
+
* its ISO 4217 currency code, such as `revenue(9.99, 'USD', { transactionId })`.
|
|
37
|
+
*/
|
|
38
|
+
revenue(amount: number, currency: string, options?: RevenueOptions): void;
|
|
15
39
|
screen(name: string, properties?: Properties): void;
|
|
16
40
|
identify(userId: string | number, traits?: Properties): void;
|
|
17
41
|
setTraits(traits: Properties): void;
|