posthog-react-native 1.1.3 → 2.0.0-alpha7

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.
Files changed (129) hide show
  1. package/README.md +9 -0
  2. package/lib/index.cjs.js +1761 -0
  3. package/lib/index.cjs.js.map +1 -0
  4. package/lib/index.d.ts +211 -0
  5. package/lib/index.esm.js +1725 -0
  6. package/lib/index.esm.js.map +1 -0
  7. package/lib/node_modules/tslib/tslib.es6.d.ts +35 -0
  8. package/lib/posthog-core/src/eventemitter.d.ts +8 -0
  9. package/lib/posthog-core/src/index.d.ts +84 -0
  10. package/lib/posthog-core/src/lz-string.d.ts +8 -0
  11. package/lib/posthog-core/src/types.d.ts +68 -0
  12. package/lib/posthog-core/src/utils.d.ts +13 -0
  13. package/lib/posthog-react-native/index.d.ts +10 -0
  14. package/lib/posthog-react-native/src/PostHogProvider.d.ts +13 -0
  15. package/lib/posthog-react-native/src/PosthogContext.d.ts +5 -0
  16. package/lib/posthog-react-native/src/autocapture.d.ts +3 -0
  17. package/lib/posthog-react-native/src/hooks/useFeatureFlag.d.ts +1 -0
  18. package/lib/posthog-react-native/src/hooks/useFeatureFlags.d.ts +3 -0
  19. package/lib/posthog-react-native/src/hooks/useLifecycleTracker.d.ts +2 -0
  20. package/lib/posthog-react-native/src/hooks/useNavigationTracker.d.ts +5 -0
  21. package/lib/posthog-react-native/src/hooks/usePostHog.d.ts +2 -0
  22. package/lib/posthog-react-native/src/legacy.d.ts +4 -0
  23. package/lib/posthog-react-native/src/optional-imports.d.ts +6 -0
  24. package/lib/posthog-react-native/src/posthog-rn.d.ts +14 -0
  25. package/lib/posthog-react-native/src/storage.d.ts +8 -0
  26. package/lib/posthog-react-native/src/types.d.ts +12 -0
  27. package/package.json +35 -51
  28. package/RNPostHog.podspec +0 -21
  29. package/android/build.gradle +0 -46
  30. package/android/src/main/AndroidManifest.xml +0 -1
  31. package/android/src/main/java/com/posthog/reactnative/core/RNPostHogModule.kt +0 -249
  32. package/android/src/main/java/com/posthog/reactnative/core/RNPostHogPackage.kt +0 -44
  33. package/build/cjs/__mocks__/bridge.d.ts +0 -14
  34. package/build/cjs/__mocks__/bridge.js +0 -15
  35. package/build/cjs/__mocks__/bridge.js.map +0 -1
  36. package/build/cjs/__tests__/bridge.spec.d.ts +0 -1
  37. package/build/cjs/__tests__/bridge.spec.js +0 -19
  38. package/build/cjs/__tests__/bridge.spec.js.map +0 -1
  39. package/build/cjs/__tests__/configuration.spec.d.ts +0 -1
  40. package/build/cjs/__tests__/configuration.spec.js +0 -131
  41. package/build/cjs/__tests__/configuration.spec.js.map +0 -1
  42. package/build/cjs/__tests__/index.spec.d.ts +0 -1
  43. package/build/cjs/__tests__/index.spec.js +0 -9
  44. package/build/cjs/__tests__/index.spec.js.map +0 -1
  45. package/build/cjs/__tests__/posthog.spec.d.ts +0 -1
  46. package/build/cjs/__tests__/posthog.spec.js +0 -192
  47. package/build/cjs/__tests__/posthog.spec.js.map +0 -1
  48. package/build/cjs/bridge.d.ts +0 -50
  49. package/build/cjs/bridge.js +0 -9
  50. package/build/cjs/bridge.js.map +0 -1
  51. package/build/cjs/configuration.d.ts +0 -3
  52. package/build/cjs/configuration.js +0 -94
  53. package/build/cjs/configuration.js.map +0 -1
  54. package/build/cjs/index.d.ts +0 -5
  55. package/build/cjs/index.js +0 -6
  56. package/build/cjs/index.js.map +0 -1
  57. package/build/cjs/make-pkg.d.ts +0 -1
  58. package/build/cjs/make-pkg.js +0 -5
  59. package/build/cjs/make-pkg.js.map +0 -1
  60. package/build/cjs/middleware.d.ts +0 -41
  61. package/build/cjs/middleware.js +0 -114
  62. package/build/cjs/middleware.js.map +0 -1
  63. package/build/cjs/posthog.d.ts +0 -228
  64. package/build/cjs/posthog.js +0 -309
  65. package/build/cjs/posthog.js.map +0 -1
  66. package/build/cjs/utils.d.ts +0 -1
  67. package/build/cjs/utils.js +0 -7
  68. package/build/cjs/utils.js.map +0 -1
  69. package/build/cjs/wrapper.d.ts +0 -22
  70. package/build/cjs/wrapper.js +0 -114
  71. package/build/cjs/wrapper.js.map +0 -1
  72. package/build/esm/__mocks__/bridge.d.ts +0 -14
  73. package/build/esm/__mocks__/bridge.js +0 -13
  74. package/build/esm/__mocks__/bridge.js.map +0 -1
  75. package/build/esm/__tests__/bridge.spec.d.ts +0 -1
  76. package/build/esm/__tests__/bridge.spec.js +0 -19
  77. package/build/esm/__tests__/bridge.spec.js.map +0 -1
  78. package/build/esm/__tests__/configuration.spec.d.ts +0 -1
  79. package/build/esm/__tests__/configuration.spec.js +0 -129
  80. package/build/esm/__tests__/configuration.spec.js.map +0 -1
  81. package/build/esm/__tests__/index.spec.d.ts +0 -1
  82. package/build/esm/__tests__/index.spec.js +0 -7
  83. package/build/esm/__tests__/index.spec.js.map +0 -1
  84. package/build/esm/__tests__/posthog.spec.d.ts +0 -1
  85. package/build/esm/__tests__/posthog.spec.js +0 -190
  86. package/build/esm/__tests__/posthog.spec.js.map +0 -1
  87. package/build/esm/bridge.d.ts +0 -50
  88. package/build/esm/bridge.js +0 -7
  89. package/build/esm/bridge.js.map +0 -1
  90. package/build/esm/configuration.d.ts +0 -3
  91. package/build/esm/configuration.js +0 -92
  92. package/build/esm/configuration.js.map +0 -1
  93. package/build/esm/index.d.ts +0 -5
  94. package/build/esm/index.js +0 -4
  95. package/build/esm/index.js.map +0 -1
  96. package/build/esm/make-pkg.d.ts +0 -1
  97. package/build/esm/make-pkg.js +0 -3
  98. package/build/esm/make-pkg.js.map +0 -1
  99. package/build/esm/middleware.d.ts +0 -41
  100. package/build/esm/middleware.js +0 -112
  101. package/build/esm/middleware.js.map +0 -1
  102. package/build/esm/posthog.d.ts +0 -228
  103. package/build/esm/posthog.js +0 -307
  104. package/build/esm/posthog.js.map +0 -1
  105. package/build/esm/utils.d.ts +0 -1
  106. package/build/esm/utils.js +0 -4
  107. package/build/esm/utils.js.map +0 -1
  108. package/build/esm/wrapper.d.ts +0 -22
  109. package/build/esm/wrapper.js +0 -112
  110. package/build/esm/wrapper.js.map +0 -1
  111. package/build/package.json +0 -3
  112. package/ios/RNPostHog/RNPostHog.h +0 -9
  113. package/ios/RNPostHog/RNPostHog.m +0 -128
  114. package/ios/RNPostHog.xcodeproj/project.pbxproj +0 -336
  115. package/ios/RNPostHog.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  116. package/src/__mocks__/bridge.ts +0 -12
  117. package/src/__tests__/bridge.spec.ts +0 -23
  118. package/src/__tests__/configuration.spec.ts +0 -79
  119. package/src/__tests__/index.spec.ts +0 -7
  120. package/src/__tests__/posthog.spec.ts +0 -106
  121. package/src/bridge.ts +0 -61
  122. package/src/configuration.ts +0 -61
  123. package/src/index.ts +0 -5
  124. package/src/make-pkg.ts +0 -3
  125. package/src/middleware.ts +0 -128
  126. package/src/modules.d.ts +0 -2
  127. package/src/posthog.ts +0 -305
  128. package/src/utils.ts +0 -3
  129. package/src/wrapper.ts +0 -61
package/src/make-pkg.ts DELETED
@@ -1,3 +0,0 @@
1
- import { version } from '../package.json'
2
-
3
- console.log(JSON.stringify({ version }, null, 2))
package/src/middleware.ts DELETED
@@ -1,128 +0,0 @@
1
- import { Context, Options, JsonMap } from './bridge'
2
- import { assertNever } from './utils'
3
- import { NativeWrapper } from './wrapper'
4
-
5
- export interface MiddlewarePayload<T extends string, D extends {}> {
6
- type: T
7
- data: D
8
- context: Context
9
- next(context?: Partial<Context>): void
10
- // tslint:disable-next-line:unified-signatures
11
- next(context?: Partial<Context>, data?: D): void
12
- }
13
-
14
- export interface CapturePayload
15
- extends MiddlewarePayload<
16
- 'capture',
17
- {
18
- event: string
19
- properties: JsonMap
20
- }
21
- > {}
22
-
23
- export interface ScreenPayload
24
- extends MiddlewarePayload<
25
- 'screen',
26
- {
27
- screen: string
28
- properties: JsonMap
29
- }
30
- > {}
31
-
32
- export interface IdentifyPayload
33
- extends MiddlewarePayload<
34
- 'identify',
35
- {
36
- distinctId: string
37
- properties: JsonMap
38
- }
39
- > {}
40
-
41
- export interface AliasPayload
42
- extends MiddlewarePayload<
43
- 'alias',
44
- {
45
- alias: string
46
- }
47
- > {}
48
-
49
- export type Payload = CapturePayload | IdentifyPayload | ScreenPayload | AliasPayload
50
-
51
- export type Middleware = (payload: Payload) => void | Promise<void>
52
- export type PayloadFromType<T> = Extract<Payload, { type: T }>
53
-
54
- export class MiddlewareChain {
55
- private readonly middlewares: Middleware[] = []
56
-
57
- constructor(private readonly wrapper: NativeWrapper<any>) {}
58
-
59
- public add(middleware: Middleware) {
60
- this.middlewares.push(middleware)
61
- }
62
-
63
- public async run<T extends Payload['type'], P extends PayloadFromType<T>>(type: T, data: P['data']) {
64
- const payload: Payload = await this.exec(type, data)
65
-
66
- switch (payload.type) {
67
- case 'alias':
68
- return this.wrapper.run('alias', alias => alias(payload.data.alias))
69
- case 'identify':
70
- return this.wrapper.run('identify', identify => identify(payload.data.distinctId, payload.data.properties))
71
- case 'screen':
72
- return this.wrapper.run('screen', screen => screen(payload.data.screen, payload.data.properties))
73
- case 'capture':
74
- return this.wrapper.run('capture', capture => capture(payload.data.event, payload.data.properties))
75
- default:
76
- return assertNever(payload)
77
- }
78
- }
79
-
80
- private async exec<T extends Payload['type'], P extends PayloadFromType<T>>(
81
- type: T,
82
- data: P['data'],
83
- index = 0
84
- ): Promise<P> {
85
- const { middlewares } = this
86
- const middleware = middlewares[index]
87
- const ctx = {}
88
-
89
- if (index >= middlewares.length || !middleware) {
90
- return makePayload(type, data)
91
- }
92
-
93
- let called = false
94
-
95
- return new Promise<P>((resolve, reject) =>
96
- Promise.resolve(
97
- middleware.call(
98
- middleware,
99
- makePayload(type, data, (nextProps = data) => {
100
- if (called) {
101
- throw new Error('middleware.payload.next() can only be called one time')
102
- }
103
-
104
- called = true
105
- this.exec(type, nextProps, index + 1)
106
- .then(resolve)
107
- .catch(reject)
108
- })
109
- )
110
- ).catch(reject)
111
- )
112
- }
113
- }
114
-
115
- const notImplemented = (name: string) => () => {
116
- throw new Error(`.${name}() not implemented`)
117
- }
118
-
119
- const makePayload = <T extends Payload['type'], P extends PayloadFromType<T>>(
120
- type: T,
121
- data: P['data'],
122
- next: (data?: P['data']) => void = notImplemented('next')
123
- ) =>
124
- ({
125
- data,
126
- next,
127
- type
128
- } as P)
package/src/modules.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare module '*.json'
2
- declare module 'react-native'
package/src/posthog.ts DELETED
@@ -1,305 +0,0 @@
1
- import Bridge, { JsonMap } from './bridge'
2
- import { configure } from './configuration'
3
- import { Middleware, MiddlewareChain } from './middleware'
4
- import { ErrorHandler, NativeWrapper } from './wrapper'
5
-
6
- // prettier-ignore
7
- export module PostHog {
8
- export interface Configuration {
9
- /**
10
- * Which PostHog host to send events to.
11
- *
12
- * `https://app.posthog.com` by default
13
- */
14
- host?: string
15
- /**
16
- * Whether the posthog client should automatically make a screen call when a
17
- * view controller is added to a view hierarchy.
18
- * Because the iOS underlying implementation uses method swizzling,
19
- * we recommend initializing the posthog client as early as possible.
20
- *
21
- * Disabled by default.
22
- */
23
- recordScreenViews?: boolean
24
- /**
25
- * Whether the posthog client should automatically capture application lifecycle events, such as
26
- * "Application Installed", "Application Updated" and "Application Opened".
27
- *
28
- * Disabled by default.
29
- */
30
- captureApplicationLifecycleEvents?: boolean
31
-
32
- /**
33
- * Whether the posthog client should automatically capture deep links. You'll still need to call the
34
- * continueUserActivity and openURL methods on the posthog client.
35
- *
36
- * Disabled by default.
37
- */
38
- captureDeepLinks?: boolean
39
-
40
- /**
41
- * Register a set of integrations to be used with this PostHog instance.
42
- */
43
- debug?: boolean
44
-
45
- /**
46
- * The number of queued events that the posthog client should flush at.
47
- * Setting this to `1` will not queue any events and will use more battery.
48
- *
49
- * `20` by default.
50
- */
51
- flushAt?: number
52
-
53
- /**
54
- * The amount of time to wait before each tick of the flush timer, in seconds.
55
- * Smaller values will make events delivered in a more real-time manner and also use more battery.
56
- * A value smaller than 10 seconds will seriously degrade overall performance.
57
- *
58
- * `30` seconds by default.
59
- */
60
- flushInterval?: number
61
-
62
- /**
63
- * iOS specific settings.
64
- */
65
- ios?: {
66
- /**
67
- * Whether the posthog client should use location services.
68
- * If `true` and the host app hasn't asked for permission to use location services then the user will be
69
- * presented with an alert view asking to do so. `false` by default. If `true`, please make sure to add a
70
- * description for `NSLocationAlwaysUsageDescription` in your `Info.plist` explaining why your app is
71
- * accessing Location APIs.
72
- *
73
- * Disabled by default.
74
- */
75
- shouldUseLocationServices?: boolean
76
- /**
77
- * The maximum number of items to queue before starting to drop old ones. This should be a value greater
78
- * than zero, the behaviour is undefined otherwise. `1000` by default.
79
- *
80
- * `1000` by default.
81
- */
82
- maxQueueSize?: number
83
- /**
84
- * Whether the posthog client should record bluetooth information. If `true`, please make sure to add a
85
- * description for `NSBluetoothPeripheralUsageDescription` in your `Info.plist` explaining explaining why
86
- * your app is accessing Bluetooth APIs. `false` by default.
87
- *
88
- * Disabled by default.
89
- */
90
- shouldUseBluetooth?: boolean
91
- /**
92
- * Whether the posthog client should automatically capture in-app purchases from the App Store
93
- *
94
- * Disabled by default.
95
- */
96
- captureInAppPurchases?: boolean
97
- /**
98
- * Whether the posthog client should automatically capture push notifications.
99
- *
100
- * Disabled by default.
101
- */
102
- capturePushNotifications?: boolean
103
- }
104
- /**
105
- * Android specific settings.
106
- */
107
- android?: {
108
- /**
109
- * Whether the posthog client should client the device identifier.
110
- * The device identifier is obtained using :
111
- * - `android.provider.Settings.Secure.ANDROID_ID`
112
- * - `android.os.Build.SERIAL`
113
- * - or Telephony Identifier retrieved via TelephonyManager as available
114
- *
115
- * Enabled by default.
116
- */
117
- collectDeviceId?: boolean
118
- }
119
- }
120
-
121
- export class Client {
122
- /**
123
- * Whether the client is ready to send events to PostHog.
124
- *
125
- * This becomes `true` when `.setup()` succeeds.
126
- * All calls will be queued until it becomes `true`.
127
- */
128
- public readonly ready = false
129
-
130
- private readonly wrapper = new NativeWrapper(this, err =>
131
- this.handleError(err)
132
- )
133
- private readonly handlers: ErrorHandler[] = []
134
- private readonly middlewares = new MiddlewareChain(this.wrapper)
135
-
136
- /**
137
- * Catch React-Native bridge errors
138
- *
139
- * These errors are emitted when calling the native counterpart.
140
- * This only applies to methods with no return value (`Promise<void>`),
141
- * methods like `getAnonymousId` do reject promises.
142
- */
143
- public catch(handler: ErrorHandler) {
144
- this.handlers.push(handler)
145
-
146
- return this
147
- }
148
-
149
- public middleware(middleware: Middleware) {
150
- this.middlewares.add(middleware)
151
-
152
- return this
153
- }
154
-
155
- /**
156
- * Use the native configuration.
157
- *
158
- * You'll need to call this method when you configure PostHog's singleton
159
- * using the native API.
160
- */
161
- public useNativeConfiguration() {
162
- if(this.ready) {
163
- throw new Error('PostHog has already been configured')
164
- }
165
-
166
- this.wrapper.ready()
167
-
168
- return this
169
- }
170
-
171
- /**
172
- * Setup the PostHog module. All calls made before are queued
173
- * and only executed if the configuration was successful.
174
- *
175
- * ```js
176
- * await posthog.setup('YOUR_API_KEY', {
177
- * captureAppLifecycleEvents: true,
178
- * ios: {
179
- * capturePushNotifications: true
180
- * }
181
- * })
182
- * ```
183
- *
184
- * @param apiKey Your PostHog.com API key
185
- * @param configuration An optional {@link Configuration} object.
186
- */
187
- public async setup(apiKey: string, configuration: Configuration = {}) {
188
- await Bridge.setup(
189
- await configure(apiKey, configuration)
190
- )
191
- this.wrapper.ready()
192
- }
193
-
194
- /**
195
- * Record the actions your users perform.
196
- *
197
- * When a user performs an action in your app, you'll want to capture that action for later analysis.
198
- * Use the event name to say what the user did, and properties to specify any interesting details of the action.
199
- *
200
- * @param event The name of the event you're capturing.
201
- * We recommend using human-readable names like `Played a Song` or `Updated Status`.
202
- * @param properties A dictionary of properties for the event.
203
- * If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc.
204
- */
205
- public async capture(event: string, properties: JsonMap = {}) {
206
- await this.middlewares.run('capture', { event, properties })
207
- }
208
-
209
- /**
210
- * Record the screens or views your users see.
211
- *
212
- * When a user views a screen in your app, you'll want to record that here.
213
- *
214
- * @param screen The title of the screen being viewed.
215
- * We recommend using human-readable names like 'Photo Feed' or 'Completed Purchase Screen'.
216
- * @param properties A dictionary of properties for the screen view event.
217
- * If the event was 'Added to Shopping Cart', it might have properties like price, productType, etc.
218
- */
219
- public async screen(screen: string, properties: JsonMap = {}) {
220
- await this.middlewares.run('screen', { screen, properties })
221
- }
222
-
223
- /**
224
- * Associate a user with their unique ID and record traits about them.
225
- *
226
- * When you learn more about who your user is, you can record that information with identify.
227
- *
228
- * @param distinctId database ID (or email address) for this user.
229
- * If you don't have a userId but want to record traits, you should pass nil.
230
- * @param properties A dictionary of properties you know about the user. Things like: email, name, plan, etc.
231
- */
232
- public async identify(distinctId: string, properties: JsonMap = {}) {
233
- await this.middlewares.run('identify', { distinctId, properties })
234
- }
235
-
236
-
237
- /**
238
- * Merge two user identities, effectively connecting two sets of user data as one.
239
- *
240
- * When you learn more about who the group is, you can record that information with group.
241
- *
242
- * @param alias The existing ID you want to link to the current distinct ID.
243
- */
244
- public async alias(alias: string) {
245
- await this.middlewares.run('alias', { alias })
246
- }
247
-
248
- /**
249
- * Reset any user state that is cached on the device.
250
- *
251
- * This is useful when a user logs out and you want to clear the identity.
252
- * It will clear any traits or userId's cached on the device.
253
- */
254
- public async reset() {
255
- await this.wrapper.run('reset', reset => reset())
256
- }
257
-
258
- /**
259
- * Trigger an upload of all queued events.
260
- *
261
- * This is useful when you want to force all messages queued on the device to be uploaded.
262
- * Please note that not all integrations respond to this method.
263
- */
264
- public async flush() {
265
- await this.wrapper.run('flush', flush => flush())
266
- }
267
-
268
- /**
269
- * Enable the sending of posthog data. Enabled by default.
270
- *
271
- * Occasionally used in conjunction with disable user opt-out handling.
272
- */
273
- public async enable() {
274
- await this.wrapper.run('enable', enable => enable())
275
- }
276
-
277
- /**
278
- * Completely disable the sending of any posthog data.
279
- *
280
- * If you have a way for users to actively or passively (sometimes based on location) opt-out of
281
- * posthog data collection, you can use this method to turn off all data collection.
282
- */
283
- public async disable() {
284
- await this.wrapper.run('disable', disable => disable())
285
- }
286
-
287
- /** Retrieve the anonymousId. */
288
- public async getAnonymousId(): Promise<string> {
289
- await this.wrapper.wait()
290
-
291
- return Bridge.getAnonymousId()
292
- }
293
-
294
- private handleError(error: Error) {
295
- const { handlers } = this
296
-
297
- if (!handlers.length) {
298
- console.error('Uncaught PostHog error', error)
299
- throw error
300
- } else {
301
- handlers.forEach(handler => handler(error))
302
- }
303
- }
304
- }
305
- }
package/src/utils.ts DELETED
@@ -1,3 +0,0 @@
1
- export function assertNever(never: never) {
2
- throw new Error('Expected never, got ' + never)
3
- }
package/src/wrapper.ts DELETED
@@ -1,61 +0,0 @@
1
- import bridge, { Bridge } from './bridge'
2
-
3
- export interface NativeDelegate {
4
- ready: boolean
5
- }
6
- export type ErrorHandler = (err: Error) => void
7
-
8
- export class NativeWrapper<T extends NativeDelegate> {
9
- constructor(
10
- private readonly delegate: T,
11
- private readonly handler: ErrorHandler,
12
- private readonly queue: Array<() => void> = []
13
- ) {}
14
-
15
- /**
16
- * Run a bridge method.
17
- * It first waits for `.setup()` or `.useNativeConfiguration()` to be
18
- * called and redirects exceptions to `handler`.
19
- * @param method Name of the method to call.
20
- * @param caller Function with the bridge function as first argument.
21
- */
22
- public async run<M extends keyof Bridge>(
23
- method: M,
24
- caller: (fn: Bridge[M]) => Promise<void>
25
- ) {
26
- const { delegate, handler, queue } = this
27
-
28
- if (delegate.ready) {
29
- await run()
30
- } else {
31
- queue.push(run)
32
- }
33
-
34
- async function run() {
35
- try {
36
- await caller(bridge[method])
37
- } catch (err) {
38
- return handler(err)
39
- }
40
- }
41
- }
42
-
43
- /** Waits for `.setup()` or `.useNativeConfiguration()` to be called. */
44
- public async wait() {
45
- if (this.delegate.ready) {
46
- return
47
- }
48
-
49
- return new Promise(resolve => this.queue.push(resolve))
50
- }
51
-
52
- public ready() {
53
- const { delegate, queue } = this
54
-
55
- delegate.ready = true
56
-
57
- while (queue.length) {
58
- queue.shift()!()
59
- }
60
- }
61
- }