@growth-loop/sdk 0.1.1 → 0.1.3

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.
@@ -1,4 +1,4 @@
1
- import { Properties, EventContext, IngestPayload } from '@growth/shared';
1
+ import { Properties, EventContext, EventSource, IngestPayload } from '@growth/shared';
2
2
 
3
3
  interface Transport {
4
4
  send(url: string, payload: IngestPayload, headers: Record<string, string>): Promise<void>;
@@ -9,6 +9,10 @@ interface GrowthOptions {
9
9
  flushInterval?: number;
10
10
  batchSize?: number;
11
11
  defaultContext?: EventContext;
12
+ /** Where events originate. Browser/web by default; createServerClient overrides to 'server'. */
13
+ source?: EventSource;
14
+ /** Pre-existing session id (e.g. from cookie/sessionStorage). Browser client wires this automatically. */
15
+ sessionId?: string;
12
16
  transport?: Transport;
13
17
  onError?: (error: Error) => void;
14
18
  debug?: boolean;
@@ -30,6 +34,7 @@ interface GrowthClient {
30
34
  group(groupType: string, groupKey: string, properties?: Properties): void;
31
35
  setContext(context: EventContext): void;
32
36
  setDistinctId(id: string): void;
37
+ setSessionId(id: string): void;
33
38
  flush(): Promise<void>;
34
39
  shutdown(): Promise<void>;
35
40
  }
@@ -41,12 +46,15 @@ declare class Growth implements GrowthClient {
41
46
  private readonly queue;
42
47
  private readonly onError;
43
48
  private readonly debug;
49
+ private readonly source;
44
50
  private context;
45
51
  private distinctId;
52
+ private sessionId;
46
53
  private flushTimer;
47
54
  private closed;
48
55
  constructor(options: GrowthOptions);
49
56
  setDistinctId(id: string): void;
57
+ setSessionId(id: string): void;
50
58
  setContext(context: EventContext): void;
51
59
  track(name: string, properties?: Properties, options?: TrackOptions): void;
52
60
  identify(options: IdentifyOptions): void;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { G as GrowthClient, T as TrackOptions } from './client-CC-8sBba.js';
2
- export { a as Growth, b as GrowthOptions, I as IdentifyOptions, c as Transport, d as createClient } from './client-CC-8sBba.js';
1
+ import { G as GrowthClient, T as TrackOptions } from './client-CUopXrmX.js';
2
+ export { a as Growth, b as GrowthOptions, I as IdentifyOptions, c as Transport, d as createClient } from './client-CUopXrmX.js';
3
3
  import { Properties } from '@growth/shared';
4
4
  export { EventContext, Properties, TrackEvent } from '@growth/shared';
5
5
 
@@ -43,6 +43,28 @@ declare function step(growth: GrowthClient, funnelStep: string, properties?: Pro
43
43
  * <button onClick={growth.button('login_clicked', onLogin)}>Login</button>
44
44
  */
45
45
  declare function button<TArgs extends unknown[], TRet>(growth: GrowthClient, name: string, handler?: (...args: TArgs) => TRet, properties?: Properties): (...args: TArgs) => TRet | undefined;
46
+ interface RevenueOptions extends TrackOptions {
47
+ currency?: string;
48
+ plan?: string;
49
+ interval?: 'month' | 'year' | 'one_time';
50
+ properties?: Properties;
51
+ }
52
+ /**
53
+ * Emit a revenue event with the cents amount normalized as `amount_cents`.
54
+ *
55
+ * The API maps `amount_cents` straight into ClickHouse's `revenue_cents` column
56
+ * for any event whose name is in the spec-cased list (`subscription_created`,
57
+ * `subscription_canceled`, `purchase_completed`, etc.). Calling `growth.track()`
58
+ * directly works too, but founders mis-name the field as `amount`, `price`, or
59
+ * `total` more often than not — using this helper prevents MRR breakage.
60
+ *
61
+ * growth.revenue('subscription_created', 2900, {
62
+ * currency: 'usd', plan: 'pro', interval: 'month', distinctId: userId,
63
+ * });
64
+ *
65
+ * growth.revenue('refund_issued', -2900, { distinctId: userId });
66
+ */
67
+ declare function revenue(growth: GrowthClient, event: string, amountCents: number, options?: RevenueOptions): void;
46
68
  /**
47
69
  * Adds the helpers above as methods on a Growth client instance. Call once
48
70
  * after constructing the client and the API surface becomes:
@@ -55,7 +77,8 @@ interface GrowthInstrumented extends GrowthClient {
55
77
  span: <TArgs extends unknown[], TRet>(name: string, fn: (...args: TArgs) => TRet | Promise<TRet>, options?: SpanOptions) => (...args: TArgs) => Promise<TRet>;
56
78
  step: (funnelStep: string, properties?: Properties, options?: TrackOptions) => void;
57
79
  button: <TArgs extends unknown[], TRet>(name: string, handler?: (...args: TArgs) => TRet, properties?: Properties) => (...args: TArgs) => TRet | undefined;
80
+ revenue: (event: string, amountCents: number, options?: RevenueOptions) => void;
58
81
  }
59
82
  declare function instrument(growth: GrowthClient): GrowthInstrumented;
60
83
 
61
- export { GrowthClient, type GrowthInstrumented, type SpanOptions, TrackOptions, button, instrument, span, step };
84
+ export { GrowthClient, type GrowthInstrumented, type RevenueOptions, type SpanOptions, TrackOptions, button, instrument, revenue, span, step };
package/dist/index.js CHANGED
@@ -4206,7 +4206,7 @@ var Queue = class {
4206
4206
 
4207
4207
  // src/version.ts
4208
4208
  var SDK_NAME = "growth-js";
4209
- var SDK_VERSION = "0.0.1";
4209
+ var SDK_VERSION = "0.1.3";
4210
4210
 
4211
4211
  // src/client.ts
4212
4212
  var DEFAULT_HOST = "https://api.growth-loop.dev";
@@ -4222,8 +4222,10 @@ var Growth = class {
4222
4222
  queue;
4223
4223
  onError;
4224
4224
  debug;
4225
+ source;
4225
4226
  context;
4226
4227
  distinctId;
4228
+ sessionId;
4227
4229
  flushTimer = null;
4228
4230
  closed = false;
4229
4231
  constructor(options) {
@@ -4235,6 +4237,8 @@ var Growth = class {
4235
4237
  this.transport = options.transport ?? fetchTransport;
4236
4238
  this.context = options.defaultContext ?? {};
4237
4239
  this.distinctId = generateId();
4240
+ this.sessionId = options.sessionId;
4241
+ this.source = options.source ?? "web";
4238
4242
  this.onError = options.onError ?? (() => {
4239
4243
  });
4240
4244
  this.debug = options.debug ?? false;
@@ -4255,6 +4259,9 @@ var Growth = class {
4255
4259
  setDistinctId(id) {
4256
4260
  this.distinctId = id;
4257
4261
  }
4262
+ setSessionId(id) {
4263
+ this.sessionId = id;
4264
+ }
4258
4265
  setContext(context) {
4259
4266
  this.context = { ...this.context, ...context };
4260
4267
  }
@@ -4263,8 +4270,9 @@ var Growth = class {
4263
4270
  const event = {
4264
4271
  name,
4265
4272
  distinct_id: options?.distinctId ?? this.distinctId,
4273
+ ...this.sessionId ? { session_id: this.sessionId } : {},
4266
4274
  timestamp: (options?.timestamp ?? /* @__PURE__ */ new Date()).toISOString(),
4267
- source: "web",
4275
+ source: this.source,
4268
4276
  properties,
4269
4277
  context: { ...this.context, ...options?.context }
4270
4278
  };
@@ -4368,14 +4376,29 @@ function button(growth, name, handler, properties) {
4368
4376
  return handler?.(...args);
4369
4377
  };
4370
4378
  }
4379
+ function revenue(growth, event, amountCents, options) {
4380
+ const props = {
4381
+ amount_cents: amountCents,
4382
+ currency: options?.currency ?? "usd"
4383
+ };
4384
+ if (options?.plan) props.plan = options.plan;
4385
+ if (options?.interval) props.interval = options.interval;
4386
+ if (options?.properties) Object.assign(props, options.properties);
4387
+ growth.track(event, props, {
4388
+ distinctId: options?.distinctId,
4389
+ timestamp: options?.timestamp,
4390
+ context: options?.context
4391
+ });
4392
+ }
4371
4393
  function instrument(growth) {
4372
4394
  const g = growth;
4373
4395
  g.span = (name, fn, options) => span(growth, name, fn, options);
4374
4396
  g.step = (funnelStep, properties, options) => step(growth, funnelStep, properties, options);
4375
4397
  g.button = (name, handler, properties) => button(growth, name, handler, properties);
4398
+ g.revenue = (event, amountCents, options) => revenue(growth, event, amountCents, options);
4376
4399
  return g;
4377
4400
  }
4378
4401
 
4379
- export { Growth, button, createClient, instrument, span, step };
4402
+ export { Growth, button, createClient, instrument, revenue, span, step };
4380
4403
  //# sourceMappingURL=index.js.map
4381
4404
  //# sourceMappingURL=index.js.map