@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.
@@ -0,0 +1,40 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <!-- What the KissMyApps SDK collects, for the app's privacy report. No tracking. -->
4
+ <plist version="1.0">
5
+ <dict>
6
+ <key>NSPrivacyTracking</key>
7
+ <false/>
8
+ <key>NSPrivacyTrackingDomains</key>
9
+ <array/>
10
+ <key>NSPrivacyCollectedDataTypes</key>
11
+ <array>
12
+ <dict>
13
+ <key>NSPrivacyCollectedDataType</key>
14
+ <string>NSPrivacyCollectedDataTypeDeviceID</string>
15
+ <key>NSPrivacyCollectedDataTypeLinked</key>
16
+ <false/>
17
+ <key>NSPrivacyCollectedDataTypeTracking</key>
18
+ <false/>
19
+ <key>NSPrivacyCollectedDataTypePurposes</key>
20
+ <array>
21
+ <string>NSPrivacyCollectedDataTypePurposeAnalytics</string>
22
+ </array>
23
+ </dict>
24
+ <dict>
25
+ <key>NSPrivacyCollectedDataType</key>
26
+ <string>NSPrivacyCollectedDataTypeProductInteraction</string>
27
+ <key>NSPrivacyCollectedDataTypeLinked</key>
28
+ <false/>
29
+ <key>NSPrivacyCollectedDataTypeTracking</key>
30
+ <false/>
31
+ <key>NSPrivacyCollectedDataTypePurposes</key>
32
+ <array>
33
+ <string>NSPrivacyCollectedDataTypePurposeAnalytics</string>
34
+ </array>
35
+ </dict>
36
+ </array>
37
+ <key>NSPrivacyAccessedAPITypes</key>
38
+ <array/>
39
+ </dict>
40
+ </plist>
@@ -0,0 +1,78 @@
1
+ /** A deep link from KissMyApps, handed to `onDeepLink` once. */
2
+ export interface DeepLink {
3
+ /** The link's deep link value, such as `lesson`, or null when it has only parameters. */
4
+ value: string | null;
5
+ /** The deep link's parameters, such as `{ id: '4' }`. */
6
+ params: Record<string, string>;
7
+ /** True when it came through an install (a deferred deep link), false when a link opened the app. */
8
+ deferred: boolean;
9
+ /** The link that opened the app, or null when the deep link came from the store install. */
10
+ url: string | null;
11
+ }
12
+ /** What the optional native module `KissMyAppsAttribution` reads on the device. */
13
+ export interface AttributionInfo {
14
+ /** Lowercase hex SHA-256 of identifierForVendor (iOS) or ANDROID_ID (Android). */
15
+ deviceKey?: string | null;
16
+ /** The Google Play install referrer (Android). */
17
+ referrer?: string | null;
18
+ /** Seconds since 1970, from the Install Referrer API (Android). */
19
+ referrerClickTs?: number | null;
20
+ /** Seconds since 1970, from the Install Referrer API (Android). */
21
+ installBeginTs?: number | null;
22
+ /** AAAttribution.attributionToken() (iOS 14.3+). */
23
+ appleAdsToken?: string | null;
24
+ /** The click ID after `kma:` on the clipboard, read only when clipboard handoff is on (iOS). */
25
+ clipboardClick?: string | null;
26
+ /** True when Google Play did not answer in time: the next try reads the referrer again. */
27
+ retryReferrer?: boolean;
28
+ }
29
+ /** What the SDK asks the native module for. */
30
+ export interface AttributionRequest {
31
+ /** The first launch: also the install referrer or Apple Ads token; otherwise the device key only. */
32
+ install: boolean;
33
+ /** Read a `kma:` click ID from the clipboard (iOS, clipboard handoff on). */
34
+ clipboard: boolean;
35
+ }
36
+ export declare const INSTALL_PATH = "/v1/attribution/install";
37
+ export declare const OPEN_PATH = "/v1/attribution/open";
38
+ /** The link as the SDK sends it: an https URL of at most 2,048 characters, or null. */
39
+ export declare function httpsLink(value: unknown): string | null;
40
+ /** The device key when it is one: 64 lowercase hex characters. */
41
+ export declare function deviceKey(value: unknown): string | null;
42
+ export interface InstallInput {
43
+ anonymousId: string;
44
+ sessionId: string;
45
+ platform: 'ios' | 'android';
46
+ /** When app_installed was queued, in milliseconds since 1970. */
47
+ installedAt: number;
48
+ sdk: string;
49
+ userId?: string | null;
50
+ appVersion?: string;
51
+ /** The link that opened the app on this launch, if any. */
52
+ link?: string | null;
53
+ info: AttributionInfo;
54
+ /** Whether the app turned clipboard handoff on. */
55
+ clipboard: boolean;
56
+ }
57
+ /** The body of POST /v1/attribution/install. */
58
+ export declare function installBody(input: InstallInput): Record<string, unknown>;
59
+ export interface OpenInput {
60
+ anonymousId: string;
61
+ sessionId: string;
62
+ platform: 'ios' | 'android';
63
+ url: string;
64
+ /** When the link opened the app, in milliseconds since 1970. */
65
+ openedAt: number;
66
+ sdk: string;
67
+ userId?: string | null;
68
+ appVersion?: string;
69
+ deviceKey?: string | null;
70
+ }
71
+ /** The body of POST /v1/attribution/open. */
72
+ export declare function openBody(input: OpenInput): Record<string, unknown>;
73
+ /** The deep link in an answer from either call, or null when it has none. */
74
+ export declare function deepLinkFrom(answer: unknown, url: string | null): DeepLink | null;
75
+ /** The answer's JSON, or null when it is not JSON. */
76
+ export declare function parseAnswer(body: string): unknown;
77
+ /** Settles with the promise's value, or with `fallback` after `ms` or when it fails. */
78
+ export declare function within<T>(promise: Promise<T>, ms: number, fallback: T): Promise<T>;
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ // The two attribution calls every KissMyApps app SDK makes: one install call on the first
3
+ // launch, and one open call for each link that opens the app later. What goes in them, and
4
+ // what comes back, is the `attribution` section of
5
+ // integrations/analytics-mobile/contract/app-events.json.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.OPEN_PATH = exports.INSTALL_PATH = void 0;
8
+ exports.httpsLink = httpsLink;
9
+ exports.deviceKey = deviceKey;
10
+ exports.installBody = installBody;
11
+ exports.openBody = openBody;
12
+ exports.deepLinkFrom = deepLinkFrom;
13
+ exports.parseAnswer = parseAnswer;
14
+ exports.within = within;
15
+ exports.INSTALL_PATH = '/v1/attribution/install';
16
+ exports.OPEN_PATH = '/v1/attribution/open';
17
+ const MAX_URL = 2048;
18
+ const MAX_PARAMS = 10;
19
+ const MAX_REFERRER = 1024;
20
+ const MAX_APPLE_ADS = 4096;
21
+ const DEVICE_KEY = /^[0-9a-f]{64}$/;
22
+ const CLICK_ID = /^[A-Za-z0-9_-]{16,40}$/;
23
+ // React Native's URL class leaves most of its parts unimplemented, so links are read by hand.
24
+ const HTTPS_URL = /^https:\/\/[^\s/?#@]+(?:[/?#]\S*)?$/i;
25
+ /** The link as the SDK sends it: an https URL of at most 2,048 characters, or null. */
26
+ function httpsLink(value) {
27
+ if (typeof value !== 'string')
28
+ return null;
29
+ const link = value.trim();
30
+ return link.length <= MAX_URL && HTTPS_URL.test(link) ? link : null;
31
+ }
32
+ function text(value, max) {
33
+ return typeof value === 'string' && value.length > 0 && value.length <= max ? value : null;
34
+ }
35
+ function seconds(value) {
36
+ // Play answers 0 when it does not know.
37
+ return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : null;
38
+ }
39
+ /** The device key when it is one: 64 lowercase hex characters. */
40
+ function deviceKey(value) {
41
+ return typeof value === 'string' && DEVICE_KEY.test(value) ? value : null;
42
+ }
43
+ /** The body of POST /v1/attribution/install. */
44
+ function installBody(input) {
45
+ const { info } = input;
46
+ const body = {
47
+ anonymous_id: input.anonymousId,
48
+ session_id: input.sessionId,
49
+ platform: input.platform,
50
+ installed_at: new Date(input.installedAt).toISOString(),
51
+ sdk: input.sdk,
52
+ };
53
+ if (input.userId)
54
+ body.user_id = input.userId;
55
+ if (input.appVersion)
56
+ body.app_version = input.appVersion;
57
+ const key = deviceKey(info.deviceKey);
58
+ if (key)
59
+ body.device_key = key;
60
+ const link = httpsLink(input.link);
61
+ if (link)
62
+ body.link_url = link;
63
+ if (input.platform === 'android') {
64
+ // A longer referrer is cut to the contract's 1,024 characters.
65
+ const referrer = typeof info.referrer === 'string' && info.referrer
66
+ ? info.referrer.slice(0, MAX_REFERRER)
67
+ : null;
68
+ const clicked = seconds(info.referrerClickTs);
69
+ const began = seconds(info.installBeginTs);
70
+ if (referrer)
71
+ body.referrer = referrer;
72
+ if (clicked !== null)
73
+ body.referrer_click_ts = clicked;
74
+ if (began !== null)
75
+ body.install_begin_ts = began;
76
+ }
77
+ else {
78
+ const appleAds = text(info.appleAdsToken, MAX_APPLE_ADS);
79
+ if (appleAds)
80
+ body.apple_ads_token = appleAds;
81
+ const click = typeof info.clipboardClick === 'string' ? info.clipboardClick.trim() : '';
82
+ if (input.clipboard && CLICK_ID.test(click))
83
+ body.clipboard_click = click;
84
+ }
85
+ return body;
86
+ }
87
+ /** The body of POST /v1/attribution/open. */
88
+ function openBody(input) {
89
+ const body = {
90
+ anonymous_id: input.anonymousId,
91
+ session_id: input.sessionId,
92
+ platform: input.platform,
93
+ url: input.url,
94
+ opened_at: new Date(input.openedAt).toISOString(),
95
+ sdk: input.sdk,
96
+ };
97
+ if (input.userId)
98
+ body.user_id = input.userId;
99
+ if (input.appVersion)
100
+ body.app_version = input.appVersion;
101
+ const key = deviceKey(input.deviceKey);
102
+ if (key)
103
+ body.device_key = key;
104
+ return body;
105
+ }
106
+ /** The deep link in an answer from either call, or null when it has none. */
107
+ function deepLinkFrom(answer, url) {
108
+ if (!answer || typeof answer !== 'object')
109
+ return null;
110
+ const link = answer.deep_link;
111
+ if (!link || typeof link !== 'object' || Array.isArray(link))
112
+ return null;
113
+ const { value, params, deferred } = link;
114
+ const clean = {};
115
+ if (params && typeof params === 'object' && !Array.isArray(params)) {
116
+ for (const [name, item] of Object.entries(params)) {
117
+ if (Object.keys(clean).length >= MAX_PARAMS)
118
+ break;
119
+ // Text as it is; numbers and true/false as text; anything else is left out.
120
+ if (typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean') {
121
+ clean[name] = String(item);
122
+ }
123
+ }
124
+ }
125
+ return {
126
+ value: typeof value === 'string' ? value : null,
127
+ params: clean,
128
+ deferred: deferred === true,
129
+ url,
130
+ };
131
+ }
132
+ /** The answer's JSON, or null when it is not JSON. */
133
+ function parseAnswer(body) {
134
+ try {
135
+ return JSON.parse(body);
136
+ }
137
+ catch {
138
+ return null;
139
+ }
140
+ }
141
+ /** Settles with the promise's value, or with `fallback` after `ms` or when it fails. */
142
+ function within(promise, ms, fallback) {
143
+ return new Promise((resolve) => {
144
+ const timer = setTimeout(() => resolve(fallback), ms);
145
+ promise.then((value) => {
146
+ clearTimeout(timer);
147
+ resolve(value);
148
+ }, () => {
149
+ clearTimeout(timer);
150
+ resolve(fallback);
151
+ });
152
+ });
153
+ }
package/lib/client.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export declare const SDK_VERSION = "1.0.0";
1
+ import type { AttributionInfo, AttributionRequest, DeepLink } from './attribution';
2
+ export declare const SDK_VERSION = "1.1.0";
2
3
  export declare const DEFAULT_ENDPOINT = "https://events.kissmyapps.dev";
3
4
  export type Properties = Record<string, unknown>;
4
5
  /** AsyncStorage, MMKV or anything with the same two methods. */
@@ -32,8 +33,33 @@ export interface ClientOptions {
32
33
  maxQueue?: number;
33
34
  /** Record installs, updates, opens and backgrounding. Default true. */
34
35
  trackLifecycle?: boolean;
36
+ /**
37
+ * Match the install to the link that brought it on the first launch, and report links that
38
+ * open the app. Default true. Nothing is matched until attribution is on for the project.
39
+ */
40
+ attribution?: boolean;
41
+ /**
42
+ * iPhone only: on the first launch, read a `kma:` click ID that the smart link's page put on
43
+ * the clipboard. Turn it on only with "Clipboard handoff for iPhone" on in the console:
44
+ * iOS asks the person before an app reads what another app copied. Default false.
45
+ */
46
+ clipboardHandoff?: boolean;
35
47
  debug?: boolean;
36
48
  }
49
+ export interface RevenueOptions {
50
+ /** Your product or plan, such as annual_plan. */
51
+ productId?: string;
52
+ /**
53
+ * The payment's ID at the store, such as StoreKit's transaction ID or Google Play's order
54
+ * ID. The same payment reported by the app and by a RevenueCat or Adapty webhook with the
55
+ * same ID counts once.
56
+ */
57
+ transactionId?: string;
58
+ /** The event's name. Default purchase, or refund for a negative amount. */
59
+ event?: string;
60
+ /** More properties for the event. The kma_ revenue properties always win. */
61
+ properties?: Properties;
62
+ }
37
63
  export interface Environment {
38
64
  storage: Storage | null;
39
65
  device(): DeviceInfo;
@@ -41,6 +67,10 @@ export interface Environment {
41
67
  sdk: string;
42
68
  now?: () => number;
43
69
  log?: (message: string) => void;
70
+ /** The link that opened the app, such as Linking.getInitialURL(). */
71
+ initialLink?: () => Promise<string | null>;
72
+ /** What the optional native module reads: device key, install referrer, Apple Ads token, if any. */
73
+ attributionInfo?: (request: AttributionRequest) => Promise<AttributionInfo | null>;
44
74
  }
45
75
  export interface QueuedEvent {
46
76
  name: string;
@@ -77,11 +107,29 @@ export declare class KmaClient {
77
107
  private inForeground;
78
108
  private firstLaunch;
79
109
  private stopped;
110
+ private launched;
111
+ private firstLaunchNow;
112
+ private installRunning;
113
+ private installSent;
114
+ private installPhase;
115
+ private linksBeforeLaunch;
116
+ private waitingLinks;
117
+ private readonly recentLinks;
118
+ private deviceKeyRead;
119
+ private deepLinkHandler;
120
+ private heldDeepLink;
121
+ private readonly initialLink;
80
122
  /** Settles once saved state is read and the launch is recorded. */
81
123
  readonly ready: Promise<void>;
82
124
  constructor(options: ClientOptions, env: Environment);
83
125
  /** Records something a person did. */
84
126
  track(name: string, properties?: Properties): void;
127
+ /**
128
+ * Records a payment: the amount in major units (9.99, not 999), negative for a refund, and
129
+ * its ISO 4217 currency code. Sent as a purchase (or refund) event with kma_revenue and
130
+ * kma_currency. An amount or currency that is not valid sends nothing (a warning in debug).
131
+ */
132
+ revenue(amount: number, currency: string, options?: RevenueOptions): void;
85
133
  /** Records a screen view; later events carry the screen's name. */
86
134
  screen(name: string, properties?: Properties): void;
87
135
  /** Ties this install to your own user ID, with optional traits such as email, name or plan. */
@@ -92,7 +140,22 @@ export declare class KmaClient {
92
140
  reset(): void;
93
141
  /** Stops recording and drops anything not sent yet, until optIn. Remembered across launches. */
94
142
  optOut(): void;
143
+ private clearConsentData;
95
144
  optIn(): void;
145
+ /**
146
+ * Reports a link that opened the app, such as a Universal Link or App Link. Any https link
147
+ * is sent and KissMyApps decides whether it is one of the project's links; its deep link then
148
+ * goes to `onDeepLink`. Returns false when nothing is sent (not https, longer than 2,048
149
+ * characters, attribution off, opted out). The same link handed over twice within 3 seconds
150
+ * counts once, so Linking and the app may both report it.
151
+ */
152
+ handleDeepLink(url: string): boolean;
153
+ /**
154
+ * Where deep links go: the deferred one after an install, and one each time a link opens the
155
+ * app. Each deep link is handed over once; one that arrives before a handler is set is held
156
+ * and handed to the next handler set. Pass null to stop.
157
+ */
158
+ onDeepLink(handler: ((link: DeepLink) => void) | null): void;
96
159
  getAnonymousId(): string;
97
160
  getUserId(): string | null;
98
161
  getSessionId(): string | null;
@@ -123,5 +186,22 @@ export declare class KmaClient {
123
186
  private scheduleFlush;
124
187
  /** Keeps an open app counted as online. */
125
188
  private heartbeat;
189
+ /** The launch: the first one queues the install call; links that came before it are routed. */
190
+ private attributeLaunch;
191
+ private routeLink;
192
+ private routeLaunched;
193
+ /** The device's details; `complete` is false when the install referrer could not be read in time. */
194
+ private readNative;
195
+ /** The first try's details again; the Apple Ads token, and a referrer that timed out, are read anew. */
196
+ private installInfo;
197
+ /** The install call: once, on the first launch; again on the next launches if it failed. */
198
+ private sendInstall;
199
+ /** Links that waited for the install's answer go to the open call now. */
200
+ private installAnswered;
201
+ private installFailed;
202
+ /** The open call, for a link that opened the app after the install was matched. Not retried. */
203
+ private sendOpen;
204
+ /** Hands a deep link to the handler; without one, the newest is held for the next handler. */
205
+ private deliver;
126
206
  private warn;
127
207
  }