@analyticscli/sdk 0.1.0-preview.4 → 0.1.0-preview.5

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
@@ -1,6 +1,14 @@
1
1
  # @analyticscli/sdk
2
2
 
3
- TypeScript SDK for tenant developers sending onboarding, paywall, purchase, and survey analytics events to the AnalyticsCLI ingest API.
3
+ TypeScript-first SDK for tenant developers sending onboarding, paywall, purchase, and survey analytics events to the AnalyticsCLI ingest API.
4
+
5
+ Using a coding agent: you can let it handle SDK integration and instrumentation end-to-end with the AnalyticsCLI skills repo:
6
+ https://github.com/Wotaso/analyticscli-skills
7
+
8
+ Use the same package in:
9
+ - React Native / Expo apps
10
+ - Browser React apps
11
+ - plain JavaScript and TypeScript codebases
4
12
 
5
13
  Current npm release channel: preview / experimental beta.
6
14
  If no stable release exists yet, `latest` points to the newest preview.
@@ -18,12 +26,24 @@ When a stable release becomes available, install without a tag:
18
26
  npm install @analyticscli/sdk
19
27
  ```
20
28
 
29
+ ## Dashboard Credentials
30
+
31
+ Before integrating, collect required values in [dash.analyticscli.com](https://dash.analyticscli.com):
32
+
33
+ - Select the target project.
34
+ - Open **API Keys** and copy the publishable ingest API key for SDK `apiKey`.
35
+ - If you validate with CLI, create/copy a CLI `readonly_token` in the same **API Keys** area.
36
+ - Optional for CLI verification: set a default project once with `analyticscli projects select` (arrow-key picker), or pass `--project <project_id>` per command.
37
+
21
38
  ## Usage (Low Boilerplate)
22
39
 
23
40
  ```ts
24
41
  import { init, ONBOARDING_EVENTS } from '@analyticscli/sdk';
25
42
 
26
- const analytics = init('<YOUR_APP_KEY>'); // short form
43
+ const analytics = init({
44
+ apiKey: '<YOUR_APP_KEY>',
45
+ identityTrackingMode: 'consent_gated', // explicit host-app default
46
+ });
27
47
 
28
48
  analytics.trackOnboardingEvent(ONBOARDING_EVENTS.START, {
29
49
  onboardingFlowId: 'onboarding_v1',
@@ -35,17 +55,51 @@ analytics.trackOnboardingEvent(ONBOARDING_EVENTS.START, {
35
55
  - `init('<YOUR_APP_KEY>')`
36
56
  - `init({ ...allOptionsOptional })`
37
57
 
58
+ For host-app integration, prefer object init with an explicit
59
+ `identityTrackingMode: 'consent_gated'` unless you intentionally need another mode.
60
+
61
+ Optional runtime collection pause/resume:
62
+
63
+ ```ts
64
+ import { init } from '@analyticscli/sdk';
65
+
66
+ const analytics = init({
67
+ apiKey: '<YOUR_APP_KEY>',
68
+ identityTrackingMode: 'consent_gated',
69
+ });
70
+ analytics.optOut(); // stop sending until optIn()
71
+ // ...
72
+ analytics.optIn();
73
+ ```
74
+
75
+ Optional full-tracking consent gate (recommended default):
76
+
77
+ ```ts
78
+ import { init } from '@analyticscli/sdk';
79
+
80
+ const analytics = init({
81
+ apiKey: '<YOUR_APP_KEY>',
82
+ identityTrackingMode: 'consent_gated',
83
+ });
84
+
85
+ // user accepts full tracking in your consent UI
86
+ analytics.setFullTrackingConsent(true);
87
+
88
+ // user rejects full tracking but you still keep strict anonymous analytics
89
+ analytics.setFullTrackingConsent(false);
90
+ ```
91
+
38
92
  `initFromEnv()` remains available and resolves credentials from these env keys:
39
93
 
40
- - `ANALYTICSCLI_WRITE_KEY`
41
- - `NEXT_PUBLIC_ANALYTICSCLI_WRITE_KEY`
42
- - `EXPO_PUBLIC_ANALYTICSCLI_WRITE_KEY`
43
- - `VITE_ANALYTICSCLI_WRITE_KEY`
94
+ - `ANALYTICSCLI_PUBLISHABLE_API_KEY`
95
+ - `NEXT_PUBLIC_ANALYTICSCLI_PUBLISHABLE_API_KEY`
96
+ - `EXPO_PUBLIC_ANALYTICSCLI_PUBLISHABLE_API_KEY`
97
+ - `VITE_ANALYTICSCLI_PUBLISHABLE_API_KEY`
44
98
 
45
99
  Runtime-specific env helpers are also available:
46
100
 
47
101
  - `@analyticscli/sdk` -> `initBrowserFromEnv(...)`
48
- - adds `PUBLIC_ANALYTICSCLI_WRITE_KEY` lookup for Astro/browser-first setups
102
+ - adds `PUBLIC_ANALYTICSCLI_PUBLISHABLE_API_KEY` lookup for Astro/browser-first setups
49
103
  - `@analyticscli/sdk` -> `initReactNativeFromEnv(...)`
50
104
  - defaults to native-friendly env key lookup
51
105
  - optional compatibility subpaths:
@@ -65,52 +119,121 @@ const analytics = initFromEnv({
65
119
  ## Optional Configuration
66
120
 
67
121
  ```ts
68
- import AsyncStorage from '@react-native-async-storage/async-storage';
69
122
  import * as Application from 'expo-application';
70
123
  import { Platform } from 'react-native';
71
124
  import { init } from '@analyticscli/sdk';
72
125
 
73
126
  const analytics = init({
74
- apiKey: process.env.EXPO_PUBLIC_ANALYTICSCLI_WRITE_KEY,
75
- debug: typeof __DEV__ === 'boolean' ? __DEV__ : false,
76
- platform:
77
- Platform.OS === 'ios' ||
78
- Platform.OS === 'android' ||
79
- Platform.OS === 'windows' ||
80
- Platform.OS === 'macos'
81
- ? Platform.OS === 'macos'
82
- ? 'mac'
83
- : Platform.OS
84
- : undefined,
85
- appVersion: Application.nativeApplicationVersion ?? undefined,
127
+ apiKey: process.env.EXPO_PUBLIC_ANALYTICSCLI_PUBLISHABLE_API_KEY,
128
+ debug: __DEV__,
129
+ platform: Platform.OS,
130
+ projectSurface: 'app',
131
+ appVersion: Application.nativeApplicationVersion,
132
+ initialConsentGranted: true,
133
+ identityTrackingMode: 'consent_gated',
134
+ initialFullTrackingConsentGranted: false,
86
135
  dedupeOnboardingStepViewsPerSession: true,
87
- storage: {
88
- getItem: (key) => AsyncStorage.getItem(key),
89
- setItem: (key, value) => AsyncStorage.setItem(key, value),
90
- removeItem: (key) => AsyncStorage.removeItem(key),
91
- },
136
+ });
137
+ ```
138
+
139
+ The SDK normalizes React Native/Expo platform values to canonical ingest values
140
+ (`macos` -> `mac`, `win32` -> `windows`) and accepts `null` for optional
141
+ `appVersion` inputs.
142
+ Use `projectSurface` for product/channel separation (`landing`, `dashboard`, `app`)
143
+ without overloading runtime `platform` (`web`, `ios`, `android`, ...).
144
+
145
+ `dedupeOnboardingStepViewsPerSession` only dedupes duplicate
146
+ `onboarding:step_view` events for the same step in the same session (for
147
+ example, when React effects fire twice or the screen remounts). It does not
148
+ dedupe paywall events, purchase events, or `screen(...)` calls.
149
+
150
+ For paywall funnels with stable `source` + `paywallId`, create one tracker per
151
+ flow context and reuse it:
152
+
153
+ ```ts
154
+ const paywall = analytics.createPaywallTracker({
155
+ source: 'onboarding',
156
+ paywallId: 'default_paywall',
157
+ offering: 'rc_main', // RevenueCat example
158
+ });
159
+
160
+ paywall.shown({ fromScreen: 'onboarding_offer' });
161
+ paywall.purchaseSuccess({ packageId: 'annual' });
162
+ ```
163
+
164
+ Do not create a new `createPaywallTracker(...)` instance for every paywall callback/event.
165
+ If your paywall provider exposes it, pass `offering` in tracker defaults
166
+ (RevenueCat offering id, Adapty paywall/placement id, Superwall placement/paywall id).
167
+
168
+ For onboarding surveys, avoid repeating unchanged flow metadata at every callsite.
169
+ Create one onboarding tracker with defaults and emit minimal survey payloads:
170
+
171
+ ```ts
172
+ const onboarding = analytics.createOnboardingTracker({
173
+ onboardingFlowId: 'onboarding_main',
174
+ onboardingFlowVersion: '1',
175
+ stepCount: 7,
176
+ isNewUser: true,
92
177
  });
93
178
 
94
- void analytics.ready();
179
+ onboarding.step('budget-survey', 6).surveyResponse({
180
+ surveyKey: 'onboarding_main',
181
+ questionKey: 'budget',
182
+ answerType: 'single_choice',
183
+ responseKey: '100-500',
184
+ });
95
185
  ```
96
186
 
97
- Use your project-specific write key from the AnalyticsCLI dashboard in your workspace.
98
- Only the write key (`apiKey`) is needed for SDK init calls.
187
+ For RevenueCat correlation, keep identity and paywall purchase metadata aligned:
188
+
189
+ ```ts
190
+ analytics.setUser(appUserId); // same id passed to Purchases.logIn(appUserId)
191
+ // in purchase callbacks, prefer provider-native ids
192
+ paywall.purchaseStarted({ packageId: packageBeingPurchased.identifier });
193
+ // on sign-out
194
+ analytics.clearUser();
195
+ ```
196
+
197
+ Identity tracking modes:
198
+ - `consent_gated` (default): starts strict (no persistent identity), enables persistence/linkage only after `setFullTrackingConsent(true)`
199
+ - `always_on`: enables persistence/linkage immediately (`enableFullTrackingWithoutConsent: true` is a boolean shortcut)
200
+ - `strict`: keeps strict anonymous behavior permanently
201
+
202
+ Recommendation for global tenant apps:
203
+ - keep `consent_gated` as default, especially when EU/EEA/UK traffic is in scope
204
+
205
+ In strict phase (and in `strict` mode):
206
+ - no persistent SDK identity across app/browser restarts
207
+ - no cookie-domain identity continuity
208
+ - `identify()` / `setUser(...)` are ignored
209
+
210
+ `initialConsentGranted` is optional:
211
+ - default: `true` when `apiKey` is present
212
+ - you can still pause/resume collection at runtime with consent APIs when your app needs that
213
+
214
+ Runtime collection control APIs:
215
+ - `analytics.getConsent()` -> current in-memory consent
216
+ - `analytics.getConsentState()` -> `'granted' | 'denied' | 'unknown'`
217
+ - `analytics.optIn()` / `analytics.optOut()`
218
+ - `analytics.setConsent(true|false)`
219
+
220
+ Full-tracking control APIs:
221
+ - `analytics.setFullTrackingConsent(true|false)`
222
+ - `analytics.optInFullTracking()` / `analytics.optOutFullTracking()`
223
+ - `analytics.isFullTrackingEnabled()`
224
+
225
+ `analytics.ready()` does not "start" tracking. With default settings, tracking
226
+ starts on `init(...)`.
227
+
228
+ Use your project-specific publishable API key from the AnalyticsCLI dashboard in your workspace.
229
+ Only the publishable API key (`apiKey`) is needed for SDK init calls.
99
230
  The SDK uses the default collector endpoint internally.
100
231
  In host apps, do not pass `endpoint` and do not add `ANALYTICSCLI_ENDPOINT` env vars.
101
232
 
102
- For browser subdomain continuity, set `cookieDomain` (for example `.analyticscli.com`).
233
+ Browser cookie-domain continuity is disabled while strict mode is active.
103
234
  For redirects across different domains, use a backend-issued short-lived handoff token rather than relying on third-party cookies.
104
235
 
105
236
  ## Releases
106
237
 
107
- Versioning is managed in the private monorepo via Changesets.
108
- Every SDK change should include a changeset entry (`pnpm changeset`), and CI creates
109
- the release version PR (`chore(release): version sdk-ts`) automatically on `main`.
110
-
111
- After that release PR is merged, the public mirror repository can run `Release to npm`.
112
- Each successful run creates or updates the matching GitHub Release
113
- (`v<package.json version>`) and links to the published npm version.
114
-
115
- Source of truth for this package is the private monorepo path `packages/sdk-ts`.
116
- Public mirror source prefix: `packages/sdk-ts`.
238
+ Use npm package versions and GitHub Releases in the public SDK repository as
239
+ the source for release history.