autotel-subscribers 10.0.0 → 12.0.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/dist/posthog.cjs CHANGED
@@ -3684,6 +3684,35 @@ var EventSubscriber = class {
3684
3684
  payload
3685
3685
  );
3686
3686
  }
3687
+ /**
3688
+ * Filter out undefined and null values from attributes
3689
+ *
3690
+ * This improves DX by allowing callers to pass objects with optional properties
3691
+ * without having to manually filter them first.
3692
+ *
3693
+ * @param attributes - Input attributes (may contain undefined/null)
3694
+ * @returns Filtered attributes with only defined values, or undefined if empty
3695
+ *
3696
+ * @example
3697
+ * ```typescript
3698
+ * const filtered = this.filterAttributes({
3699
+ * userId: user.id,
3700
+ * email: user.email, // might be undefined
3701
+ * plan: null, // will be filtered out
3702
+ * });
3703
+ * // Result: { userId: 'abc', email: 'test@example.com' } or { userId: 'abc' }
3704
+ * ```
3705
+ */
3706
+ filterAttributes(attributes) {
3707
+ if (!attributes) return void 0;
3708
+ const filtered = {};
3709
+ for (const [key, value] of Object.entries(attributes)) {
3710
+ if (value !== void 0 && value !== null) {
3711
+ filtered[key] = value;
3712
+ }
3713
+ }
3714
+ return Object.keys(filtered).length > 0 ? filtered : void 0;
3715
+ }
3687
3716
  /**
3688
3717
  * Track an event
3689
3718
  */
@@ -3741,6 +3770,31 @@ var EventSubscriber = class {
3741
3770
  };
3742
3771
  await this.send(payload);
3743
3772
  }
3773
+ /**
3774
+ * Track funnel progression with custom step names
3775
+ *
3776
+ * Unlike trackFunnelStep which uses FunnelStatus enum values,
3777
+ * this method allows any string as the step name for flexible funnel tracking.
3778
+ *
3779
+ * @param funnelName - Name of the funnel (e.g., "checkout", "onboarding")
3780
+ * @param stepName - Custom step name (e.g., "cart_viewed", "payment_entered")
3781
+ * @param stepNumber - Optional numeric position in the funnel
3782
+ * @param attributes - Optional event attributes
3783
+ */
3784
+ async trackFunnelProgression(funnelName, stepName, stepNumber, attributes) {
3785
+ if (!this.enabled) return;
3786
+ const payload = {
3787
+ type: "funnel",
3788
+ name: `${funnelName}.${stepName}`,
3789
+ funnel: funnelName,
3790
+ step: stepName,
3791
+ stepName,
3792
+ stepNumber,
3793
+ attributes,
3794
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
3795
+ };
3796
+ await this.send(payload);
3797
+ }
3744
3798
  /**
3745
3799
  * Flush pending requests and clean up
3746
3800
  *
@@ -3801,19 +3855,47 @@ var PostHogSubscriber = class extends EventSubscriber {
3801
3855
  posthog = null;
3802
3856
  config;
3803
3857
  initPromise = null;
3858
+ /** True when using browser's window.posthog (different API signature) */
3859
+ isBrowserClient = false;
3804
3860
  constructor(config) {
3805
3861
  super();
3806
- if (!config.apiKey && !config.client) {
3807
- throw new Error("PostHogSubscriber requires either apiKey or client to be provided");
3862
+ if (config.serverless) {
3863
+ config = {
3864
+ flushAt: 1,
3865
+ flushInterval: 0,
3866
+ requestTimeout: 3e3,
3867
+ ...config
3868
+ // User config overrides serverless defaults
3869
+ };
3870
+ }
3871
+ if (!config.apiKey && !config.client && !config.useGlobalClient) {
3872
+ throw new Error(
3873
+ "PostHogSubscriber requires either apiKey, client, or useGlobalClient to be provided"
3874
+ );
3808
3875
  }
3809
3876
  this.enabled = config.enabled ?? true;
3810
- this.config = config;
3877
+ this.config = {
3878
+ filterUndefinedValues: true,
3879
+ ...config
3880
+ };
3811
3881
  if (this.enabled) {
3812
3882
  this.initPromise = this.initialize();
3813
3883
  }
3814
3884
  }
3815
3885
  async initialize() {
3816
3886
  try {
3887
+ if (this.config.useGlobalClient) {
3888
+ const globalWindow = typeof globalThis === "undefined" ? void 0 : globalThis;
3889
+ if (globalWindow?.posthog) {
3890
+ this.posthog = globalWindow.posthog;
3891
+ this.isBrowserClient = true;
3892
+ this.setupErrorHandling();
3893
+ return;
3894
+ }
3895
+ throw new Error(
3896
+ "useGlobalClient enabled but window.posthog not found. Ensure PostHog script is loaded before initializing the subscriber."
3897
+ );
3898
+ }
3817
3899
  if (this.config.client) {
3818
3900
  this.posthog = this.config.client;
3819
3901
  this.setupErrorHandling();
@@ -3860,19 +3942,31 @@ var PostHogSubscriber = class extends EventSubscriber {
3860
3942
  */
3861
3943
  async sendToDestination(payload) {
3862
3944
  await this.ensureInitialized();
3863
- let properties = payload.attributes;
3945
+ const filteredAttributes = this.config.filterUndefinedValues === false ? payload.attributes : this.filterAttributes(payload.attributes);
3946
+ const properties = { ...filteredAttributes };
3864
3947
  if (payload.value !== void 0) {
3865
- properties = { ...payload.attributes, value: payload.value };
3948
+ properties.value = payload.value;
3866
3949
  }
3867
- const capturePayload = {
3868
- distinctId: this.extractDistinctId(payload.attributes),
3869
- event: payload.name,
3870
- properties
3871
- };
3872
- if (payload.attributes?.groups) {
3873
- capturePayload.groups = payload.attributes.groups;
3950
+ if (payload.stepNumber !== void 0) {
3951
+ properties.step_number = payload.stepNumber;
3952
+ }
3953
+ if (payload.stepName !== void 0) {
3954
+ properties.step_name = payload.stepName;
3955
+ }
3956
+ const distinctId = this.extractDistinctId(filteredAttributes);
3957
+ if (this.isBrowserClient) {
3958
+ this.posthog?.capture(payload.name, properties);
3959
+ } else {
3960
+ const capturePayload = {
3961
+ distinctId,
3962
+ event: payload.name,
3963
+ properties
3964
+ };
3965
+ if (filteredAttributes?.groups) {
3966
+ capturePayload.groups = filteredAttributes.groups;
3967
+ }
3968
+ this.posthog?.capture(capturePayload);
3874
3969
  }
3875
- this.posthog?.capture(capturePayload);
3876
3970
  }
3877
3971
  // Feature Flag Methods
3878
3972
  /**
@@ -4094,6 +4188,15 @@ var PostHogSubscriber = class extends EventSubscriber {
4094
4188
  */
4095
4189
  handleError(error, payload) {
4096
4190
  this.config.onError?.(error);
4191
+ if (this.config.onErrorWithContext) {
4192
+ this.config.onErrorWithContext({
4193
+ error,
4194
+ eventName: payload.name,
4195
+ eventType: payload.type,
4196
+ attributes: payload.attributes,
4197
+ subscriberName: this.name
4198
+ });
4199
+ }
4097
4200
  super.handleError(error, payload);
4098
4201
  }
4099
4202
  };