@funnelsgrove/analytics 0.1.38 → 0.1.40

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.
@@ -106,6 +106,10 @@ export type PublicAnalyticsSdkOptions = {
106
106
  flushIntervalMs: number;
107
107
  };
108
108
  export type PublicSdkAnalyticsEnvironment = 'test' | 'live';
109
+ type SecureBootstrapCrypto = Pick<Crypto, 'getRandomValues'> & {
110
+ randomUUID?: () => `${string}-${string}-${string}-${string}-${string}` | string;
111
+ };
112
+ export declare const createBootstrapIdempotencyKey: (secureCrypto?: SecureBootstrapCrypto) => string;
109
113
  export declare class PublicAnalyticsSdkService {
110
114
  private readonly options;
111
115
  private readonly eventQueue;
@@ -152,3 +156,4 @@ export declare class PublicAnalyticsSdkService {
152
156
  private sendBatch;
153
157
  }
154
158
  export declare const publicAnalyticsSdk: PublicAnalyticsSdkService;
159
+ export {};
@@ -155,6 +155,19 @@ const currentBrowserUserAgent = () => {
155
155
  const userAgent = typeof navigator.userAgent === 'string' ? navigator.userAgent.trim() : '';
156
156
  return userAgent || undefined;
157
157
  };
158
+ export const createBootstrapIdempotencyKey = (secureCrypto = globalThis.crypto) => {
159
+ if (typeof (secureCrypto === null || secureCrypto === void 0 ? void 0 : secureCrypto.randomUUID) === 'function') {
160
+ return secureCrypto.randomUUID();
161
+ }
162
+ if (typeof (secureCrypto === null || secureCrypto === void 0 ? void 0 : secureCrypto.getRandomValues) !== 'function') {
163
+ throw new Error('Secure random generation is unavailable');
164
+ }
165
+ const bytes = secureCrypto.getRandomValues(new Uint8Array(16));
166
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
167
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
168
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
169
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
170
+ };
158
171
  export class PublicAnalyticsSdkService {
159
172
  constructor(options = {}) {
160
173
  this.options = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options);
@@ -176,6 +189,7 @@ export class PublicAnalyticsSdkService {
176
189
  return userId;
177
190
  }
178
191
  try {
192
+ const idempotencyKey = createBootstrapIdempotencyKey();
179
193
  const response = await fetch(buildMainApiUrl('/sdk/public/users/bootstrap'), {
180
194
  method: 'POST',
181
195
  headers: buildSdkHeaders({
@@ -186,6 +200,7 @@ export class PublicAnalyticsSdkService {
186
200
  funnelId: FUNNEL_ID || undefined,
187
201
  funnelVersionId: FUNNEL_VERSION_ID || undefined,
188
202
  user_id: userId,
203
+ idempotencyKey,
189
204
  email: asString(input.email) || undefined,
190
205
  fullName: asString(input.fullName) || undefined,
191
206
  metadata: Object.assign({ environment: resolveAnalyticsEnvironment() }, asRecord(input.metadata)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/analytics",
3
- "version": "0.1.38",
3
+ "version": "0.1.40",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/The-Solid-Grove/funnelsgrove.git",
@@ -36,7 +36,7 @@
36
36
  "test:run": "vitest run --passWithNoTests"
37
37
  },
38
38
  "dependencies": {
39
- "@funnelsgrove/runtime": "0.1.61"
39
+ "@funnelsgrove/runtime": "0.1.62"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/node": "^20",