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

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,99 +26,209 @@ 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
- import { init, ONBOARDING_EVENTS } from '@analyticscli/sdk';
25
-
26
- const analytics = init('<YOUR_APP_KEY>'); // short form
41
+ import { createAnalyticsContext } from '@analyticscli/sdk';
27
42
 
28
- analytics.trackOnboardingEvent(ONBOARDING_EVENTS.START, {
29
- onboardingFlowId: 'onboarding_v1',
43
+ const analytics = createAnalyticsContext({
44
+ client: {
45
+ apiKey: '<YOUR_APP_KEY>',
46
+ identityTrackingMode: 'consent_gated', // explicit host-app default
47
+ },
48
+ onboarding: {
49
+ onboardingFlowId: 'onboarding_v1',
50
+ onboardingFlowVersion: '1.0.0',
51
+ isNewUser: true,
52
+ },
30
53
  });
31
- ```
32
54
 
33
- `init(...)` accepts either:
55
+ analytics.onboarding.start();
56
+ analytics.onboarding.step('welcome', 0).view();
57
+ ```
34
58
 
35
- - `init('<YOUR_APP_KEY>')`
36
- - `init({ ...allOptionsOptional })`
59
+ `createAnalyticsContext(...)` gives you:
60
+ - `analytics.client` (raw `AnalyticsClient`)
61
+ - `analytics.onboarding` (pre-wired tracker instance)
62
+ - `analytics.paywall` (optional tracker instance)
63
+ - `analytics.consent.*` (collection + full-tracking controls)
64
+ - `analytics.user.*` (`set`, `clear`, `identify`)
37
65
 
38
- `initFromEnv()` remains available and resolves credentials from these env keys:
66
+ For host-app integration, prefer explicit client config with
67
+ `identityTrackingMode: 'consent_gated'` unless you intentionally need another mode.
39
68
 
40
- - `ANALYTICSCLI_WRITE_KEY`
41
- - `NEXT_PUBLIC_ANALYTICSCLI_WRITE_KEY`
42
- - `EXPO_PUBLIC_ANALYTICSCLI_WRITE_KEY`
43
- - `VITE_ANALYTICSCLI_WRITE_KEY`
69
+ Optional runtime collection pause/resume:
44
70
 
45
- Runtime-specific env helpers are also available:
71
+ ```ts
72
+ import { createAnalyticsContext } from '@analyticscli/sdk';
46
73
 
47
- - `@analyticscli/sdk` -> `initBrowserFromEnv(...)`
48
- - adds `PUBLIC_ANALYTICSCLI_WRITE_KEY` lookup for Astro/browser-first setups
49
- - `@analyticscli/sdk` -> `initReactNativeFromEnv(...)`
50
- - defaults to native-friendly env key lookup
51
- - optional compatibility subpaths:
52
- - `@analyticscli/sdk/browser`
53
- - `@analyticscli/sdk/react-native`
74
+ const analytics = createAnalyticsContext({
75
+ client: {
76
+ apiKey: '<YOUR_APP_KEY>',
77
+ identityTrackingMode: 'consent_gated',
78
+ },
79
+ });
80
+ analytics.consent.optOut(); // stop sending until optIn()
81
+ // ...
82
+ analytics.consent.optIn();
83
+ ```
54
84
 
55
- If config is missing, the client is a safe no-op (default behavior).
56
- When `apiKey` is missing, the SDK logs a console error and remains no-op.
57
- Use strict mode if you want hard failure:
85
+ Optional full-tracking consent gate (recommended default):
58
86
 
59
87
  ```ts
60
- const analytics = initFromEnv({
61
- missingConfigMode: 'throw',
88
+ import { createAnalyticsContext } from '@analyticscli/sdk';
89
+
90
+ const analytics = createAnalyticsContext({
91
+ client: {
92
+ apiKey: '<YOUR_APP_KEY>',
93
+ identityTrackingMode: 'consent_gated',
94
+ },
62
95
  });
96
+
97
+ // user accepts full tracking in your consent UI
98
+ analytics.consent.setFullTracking(true);
99
+
100
+ // user rejects full tracking but you still keep strict anonymous analytics
101
+ analytics.consent.setFullTracking(false);
63
102
  ```
64
103
 
104
+ If `apiKey` is missing, the SDK logs a console error and remains a safe no-op client.
105
+
65
106
  ## Optional Configuration
66
107
 
67
108
  ```ts
68
- import AsyncStorage from '@react-native-async-storage/async-storage';
69
109
  import * as Application from 'expo-application';
70
110
  import { Platform } from 'react-native';
71
- import { init } from '@analyticscli/sdk';
72
-
73
- 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,
86
- dedupeOnboardingStepViewsPerSession: true,
87
- storage: {
88
- getItem: (key) => AsyncStorage.getItem(key),
89
- setItem: (key, value) => AsyncStorage.setItem(key, value),
90
- removeItem: (key) => AsyncStorage.removeItem(key),
111
+ import { createAnalyticsContext } from '@analyticscli/sdk';
112
+
113
+ const analytics = createAnalyticsContext({
114
+ client: {
115
+ apiKey: process.env.EXPO_PUBLIC_ANALYTICSCLI_PUBLISHABLE_API_KEY,
116
+ debug: __DEV__,
117
+ platform: Platform.OS,
118
+ projectSurface: 'app',
119
+ appVersion: Application.nativeApplicationVersion,
120
+ initialConsentGranted: true,
121
+ identityTrackingMode: 'consent_gated',
122
+ initialFullTrackingConsentGranted: false,
123
+ dedupeOnboardingStepViewsPerSession: true,
124
+ },
125
+ onboarding: {
126
+ onboardingFlowId: 'onboarding_main',
127
+ onboardingFlowVersion: '1',
128
+ isNewUser: true,
129
+ stepCount: 7,
91
130
  },
92
131
  });
132
+ ```
133
+
134
+ The SDK normalizes React Native/Expo platform values to canonical ingest values
135
+ (`macos` -> `mac`, `win32` -> `windows`) and accepts `null` for optional
136
+ `appVersion` inputs.
137
+ Use `projectSurface` for product/channel separation (`landing`, `dashboard`, `app`)
138
+ without overloading runtime `platform` (`web`, `ios`, `android`, ...).
139
+
140
+ `dedupeOnboardingStepViewsPerSession` only dedupes duplicate
141
+ `onboarding:step_view` events for the same step in the same session (for
142
+ example, when React effects fire twice or the screen remounts). It does not
143
+ dedupe paywall events, purchase events, or `screen(...)` calls.
144
+
145
+ For paywall funnels with stable `source` + `paywallId`, create one tracker per
146
+ flow context and reuse it:
147
+
148
+ ```ts
149
+ const paywall = analytics.createPaywall({
150
+ source: 'onboarding',
151
+ paywallId: 'default_paywall',
152
+ offering: 'rc_main', // RevenueCat example
153
+ });
154
+
155
+ paywall.shown({ fromScreen: 'onboarding_offer' });
156
+ paywall.purchaseSuccess({ packageId: 'annual' });
157
+ ```
158
+
159
+ Do not create a new `createPaywall(...)` instance for every paywall callback/event.
160
+ If your paywall provider exposes it, pass `offering` in tracker defaults
161
+ (RevenueCat offering id, Adapty paywall/placement id, Superwall placement/paywall id).
162
+
163
+ For onboarding surveys, avoid repeating unchanged flow metadata at every callsite.
164
+ Create one onboarding tracker with defaults and emit minimal survey payloads:
165
+
166
+ ```ts
167
+ const onboarding = analytics.createOnboarding({
168
+ onboardingFlowId: 'onboarding_main',
169
+ onboardingFlowVersion: '1',
170
+ stepCount: 7,
171
+ isNewUser: true,
172
+ });
173
+
174
+ onboarding.step('budget-survey', 6).surveyResponse({
175
+ surveyKey: 'onboarding_main',
176
+ questionKey: 'budget',
177
+ answerType: 'single_choice',
178
+ responseKey: '100-500',
179
+ });
180
+ ```
181
+
182
+ For RevenueCat correlation, keep identity and paywall purchase metadata aligned:
93
183
 
94
- void analytics.ready();
184
+ ```ts
185
+ analytics.user.set(appUserId); // same id passed to Purchases.logIn(appUserId)
186
+ // in purchase callbacks, prefer provider-native ids
187
+ paywall.purchaseStarted({ packageId: packageBeingPurchased.identifier });
188
+ // on sign-out
189
+ analytics.user.clear();
95
190
  ```
96
191
 
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.
192
+ Identity tracking modes:
193
+ - `consent_gated` (default): starts strict (no persistent identity), enables persistence/linkage only after full-tracking consent is granted
194
+ - `always_on`: enables persistence/linkage immediately (`enableFullTrackingWithoutConsent: true` is a boolean shortcut)
195
+ - `strict`: keeps strict anonymous behavior permanently
196
+
197
+ Recommendation for global tenant apps:
198
+ - keep `consent_gated` as default, especially when EU/EEA/UK traffic is in scope
199
+
200
+ In strict phase (and in `strict` mode):
201
+ - no persistent SDK identity across app/browser restarts
202
+ - no cookie-domain identity continuity
203
+ - `analytics.user.identify(...)` / `analytics.user.set(...)` are ignored
204
+
205
+ `initialConsentGranted` is optional:
206
+ - default: `true` when `apiKey` is present
207
+ - you can still pause/resume collection at runtime with consent APIs when your app needs that
208
+
209
+ Runtime collection control APIs:
210
+ - `analytics.consent.get()` -> current in-memory consent
211
+ - `analytics.consent.getState()` -> `'granted' | 'denied' | 'unknown'`
212
+ - `analytics.consent.optIn()` / `analytics.consent.optOut()`
213
+ - `analytics.consent.set(true|false)`
214
+
215
+ Full-tracking control APIs:
216
+ - `analytics.consent.setFullTracking(true|false)`
217
+ - `analytics.consent.optInFullTracking()` / `analytics.consent.optOutFullTracking()`
218
+ - `analytics.consent.isFullTrackingEnabled()`
219
+
220
+ `analytics.ready()` / `analytics.client.ready()` do not "start" tracking. With default settings, tracking
221
+ starts on `createAnalyticsContext(...)`.
222
+
223
+ Use your project-specific publishable API key from the AnalyticsCLI dashboard in your workspace.
224
+ Only the publishable API key (`apiKey`) is needed for SDK setup calls.
99
225
  The SDK uses the default collector endpoint internally.
100
226
  In host apps, do not pass `endpoint` and do not add `ANALYTICSCLI_ENDPOINT` env vars.
101
227
 
102
- For browser subdomain continuity, set `cookieDomain` (for example `.analyticscli.com`).
228
+ Browser cookie-domain continuity is disabled while strict mode is active.
103
229
  For redirects across different domains, use a backend-issued short-lived handoff token rather than relying on third-party cookies.
104
230
 
105
231
  ## Releases
106
232
 
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`.
233
+ Use npm package versions and GitHub Releases in the public SDK repository as
234
+ the source for release history.