@emit-vision/sdk-react-native 0.1.1 → 0.2.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/README.md CHANGED
@@ -70,6 +70,48 @@ function SaveButton() {
70
70
  }
71
71
  ```
72
72
 
73
+ ## Consent
74
+
75
+ Pass `consentRequired: true` to `initRN`, then record the user's choice:
76
+
77
+ ```tsx
78
+ import { grantConsent, declineConsent } from "@emit-vision/sdk-react-native";
79
+
80
+ <Button title="Accept" onPress={() => grantConsent()} />
81
+ <Button title="Decline" onPress={() => declineConsent()} />
82
+ ```
83
+
84
+ When you pass `asyncStorage` to `initRN`, the choice is saved through it, so a
85
+ grant or a decline survives app restarts and the user isn't asked again.
86
+
87
+ **Without `asyncStorage` (and without a `consentStorage`), the choice is kept
88
+ in memory only and is forgotten when the app restarts.**
89
+
90
+ To persist consent somewhere other than AsyncStorage, pass a `consentStorage`
91
+ to `initRN`. It takes precedence over `asyncStorage` for consent. `getItem`
92
+ must answer synchronously; writes may be async:
93
+
94
+ ```ts
95
+ import type { ConsentStorage } from "@emit-vision/sdk-js";
96
+
97
+ const consentStorage: ConsentStorage = {
98
+ getItem: (key) => cache.get(key) ?? null,
99
+ setItem: (key, value) => {
100
+ cache.set(key, value);
101
+ return persist(key, value);
102
+ },
103
+ removeItem: (key) => {
104
+ cache.delete(key);
105
+ return unpersist(key);
106
+ },
107
+ };
108
+
109
+ await initRN({ dsn, consentRequired: true, consentStorage });
110
+ ```
111
+
112
+ See the [consent model](https://emitvision.com/docs/sdk-js#consent) for the
113
+ states and what is sent in each.
114
+
73
115
  ## Notes
74
116
 
75
117
  - Page-view auto-capture is disabled — there is no DOM in React Native.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { EmitVisionOptions, captureEvent, captureError, identify, group, consent, flush, setContext, setTags } from '@emit-vision/sdk-js';
2
- export { CaptureOptions, EmitVisionOptions, EmitVisionUser, captureError, captureEvent, consent, flush, group, identify, setContext, setTags } from '@emit-vision/sdk-js';
1
+ import { EmitVisionOptions, captureEvent, captureError, identify, group, consent, grantConsent, declineConsent, getConsentState, onConsentChange, flush, setContext, setTags } from '@emit-vision/sdk-js';
2
+ export { CaptureOptions, ConsentState, EmitVisionOptions, EmitVisionUser, captureError, captureEvent, consent, declineConsent, flush, getConsentState, grantConsent, group, identify, onConsentChange, setContext, setTags } from '@emit-vision/sdk-js';
3
3
 
4
4
  type AsyncStorageLike = {
5
5
  getItem(key: string): Promise<string | null>;
6
6
  setItem(key: string, value: string): Promise<void>;
7
+ removeItem?(key: string): Promise<void>;
7
8
  };
8
9
  type NetInfoState = {
9
10
  isConnected: boolean | null;
@@ -39,6 +40,10 @@ type EmitVisionRNApi = {
39
40
  identify: typeof identify;
40
41
  group: typeof group;
41
42
  consent: typeof consent;
43
+ grantConsent: typeof grantConsent;
44
+ declineConsent: typeof declineConsent;
45
+ getConsentState: typeof getConsentState;
46
+ onConsentChange: typeof onConsentChange;
42
47
  flush: typeof flush;
43
48
  setContext: typeof setContext;
44
49
  setTags: typeof setTags;
package/dist/index.js CHANGED
@@ -5,6 +5,10 @@ import {
5
5
  identify as identify2,
6
6
  group as group2,
7
7
  consent as consent2,
8
+ grantConsent as grantConsent2,
9
+ declineConsent as declineConsent2,
10
+ getConsentState as getConsentState2,
11
+ onConsentChange as onConsentChange2,
8
12
  flush as flush3,
9
13
  setContext as setContext2,
10
14
  setTags as setTags2
@@ -12,10 +16,34 @@ import {
12
16
 
13
17
  // src/client-rn.ts
14
18
  import {
19
+ consentStorageKey,
15
20
  init,
16
21
  flush,
17
22
  captureError
18
23
  } from "@emit-vision/sdk-js";
24
+
25
+ // src/consent-storage.ts
26
+ async function createConsentStorage(asyncStorage, key) {
27
+ const cache = /* @__PURE__ */ new Map();
28
+ try {
29
+ const stored = await asyncStorage.getItem(key);
30
+ if (stored) cache.set(key, stored);
31
+ } catch {
32
+ }
33
+ return {
34
+ getItem: (k) => cache.get(k) ?? null,
35
+ setItem: (k, v) => {
36
+ cache.set(k, v);
37
+ return asyncStorage.setItem(k, v);
38
+ },
39
+ removeItem: (k) => {
40
+ cache.delete(k);
41
+ return asyncStorage.removeItem?.(k);
42
+ }
43
+ };
44
+ }
45
+
46
+ // src/client-rn.ts
19
47
  var ANON_ID_KEY = "__ev_anon_id";
20
48
  async function getOrCreateAnonId(storage) {
21
49
  try {
@@ -31,9 +59,14 @@ async function getOrCreateAnonId(storage) {
31
59
  async function initRN(options) {
32
60
  const { asyncStorage, netInfo, appState, errorUtils, ...sdkOptions } = options;
33
61
  const sessionId = sdkOptions.sessionId ?? (asyncStorage ? await getOrCreateAnonId(asyncStorage) : void 0);
62
+ const consentStorage = sdkOptions.consentStorage ?? (asyncStorage ? await createConsentStorage(
63
+ asyncStorage,
64
+ consentStorageKey(sdkOptions.apiKey ?? "", sdkOptions.projectId)
65
+ ) : void 0);
34
66
  const client = init({
35
67
  ...sdkOptions,
36
68
  sessionId,
69
+ consentStorage,
37
70
  autoCapture: {
38
71
  ...sdkOptions.autoCapture,
39
72
  pageViews: false
@@ -89,6 +122,10 @@ import {
89
122
  identify,
90
123
  group,
91
124
  consent,
125
+ grantConsent,
126
+ declineConsent,
127
+ getConsentState,
128
+ onConsentChange,
92
129
  flush as flush2,
93
130
  setContext,
94
131
  setTags
@@ -99,6 +136,10 @@ var api = {
99
136
  identify,
100
137
  group,
101
138
  consent,
139
+ grantConsent,
140
+ declineConsent,
141
+ getConsentState,
142
+ onConsentChange,
102
143
  flush: flush2,
103
144
  setContext,
104
145
  setTags
@@ -110,10 +151,14 @@ export {
110
151
  captureError3 as captureError,
111
152
  captureEvent2 as captureEvent,
112
153
  consent2 as consent,
154
+ declineConsent2 as declineConsent,
113
155
  flush3 as flush,
156
+ getConsentState2 as getConsentState,
157
+ grantConsent2 as grantConsent,
114
158
  group2 as group,
115
159
  identify2 as identify,
116
160
  initRN,
161
+ onConsentChange2 as onConsentChange,
117
162
  setContext2 as setContext,
118
163
  setTags2 as setTags,
119
164
  useEmitVision
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emit-vision/sdk-react-native",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "React Native SDK for emit-vision analytics and error tracking.",
5
5
  "private": false,
6
6
  "license": "MIT",
@@ -39,7 +39,7 @@
39
39
  }
40
40
  },
41
41
  "dependencies": {
42
- "@emit-vision/sdk-js": "^0.4.3"
42
+ "@emit-vision/sdk-js": "^0.5.0"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "tsup --config tsup.config.ts",