@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/src/index.ts CHANGED
@@ -1,23 +1,56 @@
1
1
  // KissMyApps analytics for React Native: sessions, installs, opens, screens and your own
2
- // events, sent to your KissMyApps project. Pure JavaScript, so there is nothing to link.
2
+ // events, sent to your KissMyApps project, plus install attribution and deep links. Plain
3
+ // JavaScript; the optional native module KissMyAppsAttribution adds the device key, the
4
+ // Google Play install referrer and the Apple Ads token when it is linked.
3
5
 
4
- import { AppState } from 'react-native'
6
+ import { AppState, Linking, NativeModules } from 'react-native'
7
+ import type { AttributionInfo, AttributionRequest, DeepLink } from './attribution'
8
+ import { httpsLink } from './attribution'
5
9
  import { KmaClient, SDK_VERSION } from './client'
6
- import type { ClientOptions, Properties, Storage } from './client'
10
+ import type { ClientOptions, Properties, RevenueOptions, Storage } from './client'
7
11
  import { deviceInfo } from './device'
8
12
  import { navigationTracker } from './navigation'
9
13
  import type { NavigationRefLike } from './navigation'
10
14
 
11
15
  export { KmaClient, SDK_VERSION } from './client'
12
- export type { ClientOptions, DeviceInfo, Properties, Storage } from './client'
16
+ export type { ClientOptions, DeviceInfo, Properties, RevenueOptions, Storage } from './client'
17
+ export type { AttributionInfo, DeepLink } from './attribution'
13
18
  export type { NavigationRefLike } from './navigation'
14
19
 
15
20
  export interface InitOptions extends ClientOptions {
16
21
  /** Where the SDK keeps its IDs and offline queue, such as AsyncStorage. */
17
22
  storage?: Storage
23
+ /**
24
+ * Read the link that opened the app (Linking.getInitialURL) and links opened while it runs
25
+ * (Linking's `url` events). Default true. Turn it off to call handleDeepLink yourself.
26
+ */
27
+ deepLinks?: boolean
28
+ }
29
+
30
+ interface NativeAttribution {
31
+ getAttributionInfo(request: AttributionRequest): Promise<AttributionInfo | null>
32
+ /** Android: the activity was reopened from Recents, so its link is an old one. */
33
+ launchedFromHistory?(): Promise<boolean>
34
+ }
35
+
36
+ /** The link that opened the app, unless Android reopened the activity from Recents. */
37
+ async function openingLink(native: NativeAttribution | null): Promise<string | null> {
38
+ const url = await Linking.getInitialURL()
39
+ if (!url || !native?.launchedFromHistory) return url
40
+ const old = await native.launchedFromHistory().catch(() => false)
41
+ return old === true ? null : url
42
+ }
43
+
44
+ /** The optional native module, when the app has it linked (not in Expo Go). */
45
+ function nativeAttribution(): NativeAttribution | null {
46
+ const module: unknown = NativeModules.KissMyAppsAttribution
47
+ return module && typeof (module as NativeAttribution).getAttributionInfo === 'function'
48
+ ? (module as NativeAttribution)
49
+ : null
18
50
  }
19
51
 
20
52
  let client: KmaClient | null = null
53
+ let deepLinkHandler: ((link: DeepLink) => void) | null = null
21
54
  // Calls made before init run once it is called.
22
55
  const early: ((client: KmaClient) => void)[] = []
23
56
 
@@ -30,24 +63,66 @@ export const KissMyApps = {
30
63
  /** Starts the SDK once, as early as possible, with the project's client key. */
31
64
  init(options: InitOptions): KmaClient {
32
65
  if (client) return client
66
+ const native = nativeAttribution()
67
+ // Linking is there in every React Native app; a test's stand-in may leave it out.
68
+ const links =
69
+ options.deepLinks !== false &&
70
+ options.attribution !== false &&
71
+ Boolean(Linking?.getInitialURL)
33
72
  const started = new KmaClient(options, {
34
73
  storage: options.storage ?? null,
35
74
  device: deviceInfo,
36
75
  fetch: (url, init) => fetch(url, init),
37
76
  sdk: `kma-react-native/${SDK_VERSION}`,
77
+ initialLink: links ? () => openingLink(native) : undefined,
78
+ attributionInfo: native ? (request) => native.getAttributionInfo(request) : undefined,
38
79
  })
39
80
  client = started
81
+ if (deepLinkHandler) started.onDeepLink(deepLinkHandler)
40
82
  if (AppState.currentState !== 'background') started.foreground()
41
83
  AppState.addEventListener('change', (state) => {
42
84
  if (state === 'active') started.foreground()
43
85
  else if (state === 'background') started.background()
44
86
  })
87
+ if (links) Linking.addEventListener?.('url', ({ url }) => started.handleDeepLink(url))
45
88
  for (const run of early.splice(0)) run(started)
46
89
  return started
47
90
  },
91
+ /**
92
+ * Reports a link that opened the app. With `deepLinks` on (the default) the SDK already
93
+ * reads Linking, so call this only for links that reach the app another way. Returns false
94
+ * when the link is not sent (not an https link, attribution off, opted out).
95
+ */
96
+ handleDeepLink(url: string): boolean {
97
+ if (client) return client.handleDeepLink(url)
98
+ if (!httpsLink(url)) return false
99
+ call((kma) => kma.handleDeepLink(url))
100
+ return true
101
+ },
102
+ /**
103
+ * Where deep links go: the deferred one after an install, and one each time a link opens
104
+ * the app. Each is handed over once; one that arrives before a handler is set waits for it.
105
+ * Returns a function that removes the handler.
106
+ */
107
+ onDeepLink(handler: (link: DeepLink) => void): () => void {
108
+ deepLinkHandler = handler
109
+ client?.onDeepLink(handler)
110
+ return () => {
111
+ if (deepLinkHandler !== handler) return
112
+ deepLinkHandler = null
113
+ client?.onDeepLink(null)
114
+ }
115
+ },
48
116
  track(name: string, properties?: Properties): void {
49
117
  call((kma) => kma.track(name, properties))
50
118
  },
119
+ /**
120
+ * Records a payment: the amount in major units (9.99, not 999), negative for a refund, and
121
+ * its ISO 4217 currency code, such as `revenue(9.99, 'USD', { transactionId })`.
122
+ */
123
+ revenue(amount: number, currency: string, options?: RevenueOptions): void {
124
+ call((kma) => kma.revenue(amount, currency, options))
125
+ },
51
126
  screen(name: string, properties?: Properties): void {
52
127
  call((kma) => kma.screen(name, properties))
53
128
  },