@posthog/browser-common 0.9.5 → 0.9.7

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.
Files changed (89) hide show
  1. package/dist/client.d.ts +6 -0
  2. package/dist/config.js +1 -1
  3. package/dist/config.mjs +1 -1
  4. package/dist/feature-flags.d.ts +1 -0
  5. package/dist/feature-flags.js +5 -0
  6. package/dist/feature-flags.mjs +5 -0
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.js +58 -10
  9. package/dist/index.mjs +1 -0
  10. package/dist/survey-action-matcher.d.ts +27 -0
  11. package/dist/survey-action-matcher.js +169 -0
  12. package/dist/survey-action-matcher.mjs +131 -0
  13. package/dist/survey-constants.d.ts +86 -0
  14. package/dist/survey-constants.js +144 -0
  15. package/dist/survey-constants.mjs +76 -0
  16. package/dist/survey-event-host.d.ts +15 -0
  17. package/dist/survey-event-host.js +18 -0
  18. package/dist/survey-event-host.mjs +1 -0
  19. package/dist/survey-event-receiver-base.d.ts +180 -0
  20. package/dist/survey-event-receiver-base.js +354 -0
  21. package/dist/survey-event-receiver-base.mjs +316 -0
  22. package/dist/survey-event-receiver.d.ts +36 -0
  23. package/dist/survey-event-receiver.js +133 -0
  24. package/dist/survey-event-receiver.mjs +95 -0
  25. package/dist/survey-render-context.d.ts +13 -0
  26. package/dist/survey-render-context.js +45 -0
  27. package/dist/survey-render-context.mjs +7 -0
  28. package/dist/surveys/components/BottomSection.d.ts +12 -0
  29. package/dist/surveys/components/BottomSection.js +82 -0
  30. package/dist/surveys/components/BottomSection.mjs +44 -0
  31. package/dist/surveys/components/ConfirmationMessage.d.ts +10 -0
  32. package/dist/surveys/components/ConfirmationMessage.js +92 -0
  33. package/dist/surveys/components/ConfirmationMessage.mjs +54 -0
  34. package/dist/surveys/components/IntroScreen.d.ts +10 -0
  35. package/dist/surveys/components/IntroScreen.js +78 -0
  36. package/dist/surveys/components/IntroScreen.mjs +40 -0
  37. package/dist/surveys/components/PostHogLogo.d.ts +5 -0
  38. package/dist/surveys/components/PostHogLogo.js +54 -0
  39. package/dist/surveys/components/PostHogLogo.mjs +16 -0
  40. package/dist/surveys/components/QuestionHeader.d.ts +10 -0
  41. package/dist/surveys/components/QuestionHeader.js +82 -0
  42. package/dist/surveys/components/QuestionHeader.mjs +41 -0
  43. package/dist/surveys/components/QuestionTypes.d.ts +31 -0
  44. package/dist/surveys/components/QuestionTypes.js +519 -0
  45. package/dist/surveys/components/QuestionTypes.mjs +469 -0
  46. package/dist/surveys/icons.d.ts +12 -0
  47. package/dist/surveys/icons.js +212 -0
  48. package/dist/surveys/icons.mjs +147 -0
  49. package/dist/surveys/survey-branching.d.ts +12 -0
  50. package/dist/surveys/survey-branching.js +101 -0
  51. package/dist/surveys/survey-branching.mjs +60 -0
  52. package/dist/surveys/survey-styles.d.ts +1 -0
  53. package/dist/surveys/survey-styles.js +40 -0
  54. package/dist/surveys/survey-styles.mjs +2 -0
  55. package/dist/surveys/survey-translations.d.ts +22 -0
  56. package/dist/surveys/survey-translations.js +68 -0
  57. package/dist/surveys/survey-translations.mjs +27 -0
  58. package/dist/surveys/survey-url-prefill.d.ts +36 -0
  59. package/dist/surveys/survey-url-prefill.js +162 -0
  60. package/dist/surveys/survey-url-prefill.mjs +118 -0
  61. package/dist/surveys/surveys-extension-utils.d.ts +133 -0
  62. package/dist/surveys/surveys-extension-utils.js +739 -0
  63. package/dist/surveys/surveys-extension-utils.mjs +608 -0
  64. package/dist/surveys-config.d.ts +60 -0
  65. package/dist/surveys-config.js +68 -0
  66. package/dist/surveys-config.mjs +9 -0
  67. package/dist/surveys-renderer.d.ts +248 -0
  68. package/dist/surveys-renderer.js +1299 -0
  69. package/dist/surveys-renderer.mjs +1230 -0
  70. package/dist/surveys.d.ts +117 -0
  71. package/dist/surveys.js +566 -0
  72. package/dist/surveys.mjs +495 -0
  73. package/dist/types/surveys.d.ts +83 -86
  74. package/dist/types/surveys.js +1 -127
  75. package/dist/types/surveys.mjs +1 -76
  76. package/dist/utils/autocapture-utils.js +8 -9
  77. package/dist/utils/autocapture-utils.mjs +8 -9
  78. package/dist/utils/matcher-utils.d.ts +1 -1
  79. package/dist/utils/matcher-utils.js +13 -10
  80. package/dist/utils/matcher-utils.mjs +10 -7
  81. package/dist/utils/survey-utils.d.ts +16 -0
  82. package/dist/utils/survey-utils.js +121 -0
  83. package/dist/utils/survey-utils.mjs +38 -0
  84. package/dist/utils/url-targeting-utils.d.ts +3 -1
  85. package/dist/utils/url-targeting-utils.js +1 -2
  86. package/dist/utils/url-targeting-utils.mjs +1 -2
  87. package/dist/utils/uuidv7.js +4 -4
  88. package/dist/utils/uuidv7.mjs +4 -4
  89. package/package.json +96 -2
package/dist/client.d.ts CHANGED
@@ -31,6 +31,8 @@ export interface CapturedEventInfo {
31
31
  }
32
32
  /** Per-call capture overrides, mirroring the client's public capture options. */
33
33
  export interface CaptureOptions {
34
+ /** Send immediately without batching, using best-effort unload-safe delivery. */
35
+ delivery?: 'unload';
34
36
  /** Override the event timestamp sent to PostHog. UTC is preferred; non-UTC input is converted to UTC. */
35
37
  timestamp?: Date;
36
38
  /** Override the event UUID used for de-duplication. */
@@ -102,6 +104,8 @@ export interface Client {
102
104
  readonly groups: DeepReadonly<Record<string, string>>;
103
105
  /** The current session, if one exists. Reading it need not create one. */
104
106
  readonly session: SessionContext | undefined;
107
+ /** Effective consent opt-out state. Cookieless capture may still be permitted while opted out. */
108
+ readonly isOptedOut: boolean;
105
109
  /** Whether the host currently permits data capture. */
106
110
  readonly canCapture: boolean;
107
111
  /** Synchronously admits an analytics event to the client's normal pipeline. */
@@ -112,6 +116,8 @@ export interface Client {
112
116
  getExtension<T extends Extension>(token: ExtensionToken<T>): T | undefined;
113
117
  /** Returns the extension registered under a stable name, or `undefined` when it is not installed. */
114
118
  getExtension<T extends Extension = Extension>(name: string): T | undefined;
119
+ /** Observes session IDs through the host's session notifications, without creating a session. May replay an existing session on subscription. */
120
+ readonly onSession: Listener<string>;
115
121
  /** Fires for every captured event through a deeply readonly view. */
116
122
  readonly onEvent: Listener<CapturedEventInfo>;
117
123
  /** Replays the latest available remote-config outcome and fires subsequent outcomes when the host makes them available. */
package/dist/config.js CHANGED
@@ -27,7 +27,7 @@ var __webpack_require__ = {};
27
27
  })();
28
28
  var __webpack_exports__ = {};
29
29
  __webpack_require__.r(__webpack_exports__);
30
- const packageVersion = "0.9.5";
30
+ const packageVersion = "0.9.7";
31
31
  const Config = {
32
32
  DEBUG: false,
33
33
  LIB_VERSION: packageVersion,
package/dist/config.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const packageVersion = "0.9.5";
1
+ const packageVersion = "0.9.7";
2
2
  const Config = {
3
3
  DEBUG: false,
4
4
  LIB_VERSION: packageVersion,
@@ -75,6 +75,7 @@ export declare class PostHogFeatureFlags implements Extension {
75
75
  private _reloadingDisabled;
76
76
  private _additionalReloadRequested;
77
77
  private _reloadDebouncer;
78
+ private _firingFeatureFlagsCallbacks;
78
79
  private _flagsLoadedFromRemote;
79
80
  private _staleCacheRefreshTriggered;
80
81
  private _consecutiveStatusZeroFailures;
@@ -206,6 +206,7 @@ class PostHogFeatureFlags {
206
206
  this._requestGeneration = 0;
207
207
  this._reloadingDisabled = false;
208
208
  this._additionalReloadRequested = false;
209
+ this._firingFeatureFlagsCallbacks = false;
209
210
  this._flagsLoadedFromRemote = false;
210
211
  this._staleCacheRefreshTriggered = false;
211
212
  this._consecutiveStatusZeroFailures = 0;
@@ -598,6 +599,7 @@ class PostHogFeatureFlags {
598
599
  reloadFeatureFlags() {
599
600
  if (this._reloadingDisabled || this._config.featureFlagsDisabled) return;
600
601
  if (this._hasStatusZeroCircuitBreakerTripped()) return;
602
+ if (this._firingFeatureFlagsCallbacks) this._logger.warn("reloadFeatureFlags() was called inside an onFeatureFlags callback. This callback also runs when flags change in other tabs, so this can cause a request loop.");
601
603
  if (this._requestInFlight) this._additionalReloadRequested = true;
602
604
  if (this._reloadDebouncer) return;
603
605
  this._reloadingHandlers.slice().forEach((handler)=>{
@@ -1114,6 +1116,8 @@ class PostHogFeatureFlags {
1114
1116
  _fireFeatureFlagsCallbacks(errorsLoading) {
1115
1117
  this._rebuildEventProperties();
1116
1118
  const { flags, flagVariants } = this._prepareFeatureFlagsForCallbacks();
1119
+ const wasFiring = this._firingFeatureFlagsCallbacks;
1120
+ this._firingFeatureFlagsCallbacks = true;
1117
1121
  this.featureFlagEventHandlers.forEach((handler)=>{
1118
1122
  try {
1119
1123
  handler(flags, flagVariants, {
@@ -1123,6 +1127,7 @@ class PostHogFeatureFlags {
1123
1127
  this._logger.error('Error while running feature flags callback', error);
1124
1128
  }
1125
1129
  });
1130
+ this._firingFeatureFlagsCallbacks = wasFiring;
1126
1131
  }
1127
1132
  setPersonPropertiesForFlags(properties, reloadFeatureFlags = true) {
1128
1133
  this._setPersonPropertiesForFlags(properties, reloadFeatureFlags);
@@ -170,6 +170,7 @@ class PostHogFeatureFlags {
170
170
  this._requestGeneration = 0;
171
171
  this._reloadingDisabled = false;
172
172
  this._additionalReloadRequested = false;
173
+ this._firingFeatureFlagsCallbacks = false;
173
174
  this._flagsLoadedFromRemote = false;
174
175
  this._staleCacheRefreshTriggered = false;
175
176
  this._consecutiveStatusZeroFailures = 0;
@@ -562,6 +563,7 @@ class PostHogFeatureFlags {
562
563
  reloadFeatureFlags() {
563
564
  if (this._reloadingDisabled || this._config.featureFlagsDisabled) return;
564
565
  if (this._hasStatusZeroCircuitBreakerTripped()) return;
566
+ if (this._firingFeatureFlagsCallbacks) this._logger.warn("reloadFeatureFlags() was called inside an onFeatureFlags callback. This callback also runs when flags change in other tabs, so this can cause a request loop.");
565
567
  if (this._requestInFlight) this._additionalReloadRequested = true;
566
568
  if (this._reloadDebouncer) return;
567
569
  this._reloadingHandlers.slice().forEach((handler)=>{
@@ -1078,6 +1080,8 @@ class PostHogFeatureFlags {
1078
1080
  _fireFeatureFlagsCallbacks(errorsLoading) {
1079
1081
  this._rebuildEventProperties();
1080
1082
  const { flags, flagVariants } = this._prepareFeatureFlagsForCallbacks();
1083
+ const wasFiring = this._firingFeatureFlagsCallbacks;
1084
+ this._firingFeatureFlagsCallbacks = true;
1081
1085
  this.featureFlagEventHandlers.forEach((handler)=>{
1082
1086
  try {
1083
1087
  handler(flags, flagVariants, {
@@ -1087,6 +1091,7 @@ class PostHogFeatureFlags {
1087
1091
  this._logger.error('Error while running feature flags callback', error);
1088
1092
  }
1089
1093
  });
1094
+ this._firingFeatureFlagsCallbacks = wasFiring;
1090
1095
  }
1091
1096
  setPersonPropertiesForFlags(properties, reloadFeatureFlags = true) {
1092
1097
  this._setPersonPropertiesForFlags(properties, reloadFeatureFlags);
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { SurveyEventType, SurveyWidgetType, SurveyPosition, SurveyTabPosition, SurveyType, SurveyQuestionType, SurveyQuestionBranchingType, SurveySchedule, SurveyEventName, SurveyEventProperties, DisplaySurveyType, } from './survey-constants';
1
2
  /**
2
3
  * Public surface for browser extensions shared across PostHog JavaScript SDK
3
4
  * clients.
package/dist/index.js CHANGED
@@ -9,6 +9,9 @@ var __webpack_modules__ = {
9
9
  "./pubsub" (module) {
10
10
  module.exports = require("./pubsub.js");
11
11
  },
12
+ "./survey-constants" (module) {
13
+ module.exports = require("./survey-constants.js");
14
+ },
12
15
  "./types" (module) {
13
16
  module.exports = require("./types/index.js");
14
17
  }
@@ -60,30 +63,75 @@ function __webpack_require__(moduleId) {
60
63
  var __webpack_exports__ = {};
61
64
  (()=>{
62
65
  __webpack_require__.r(__webpack_exports__);
63
- var _extension_tokens__rspack_import_0 = __webpack_require__("./extension-tokens");
64
- var _types__rspack_import_1 = __webpack_require__("./types");
66
+ var _survey_constants__rspack_import_0 = __webpack_require__("./survey-constants");
67
+ var _extension_tokens__rspack_import_1 = __webpack_require__("./extension-tokens");
68
+ var _types__rspack_import_2 = __webpack_require__("./types");
65
69
  var __rspack_reexport = {};
66
- for(const __rspack_import_key in _types__rspack_import_1)if ([
70
+ for(const __rspack_import_key in _types__rspack_import_2)if ([
71
+ "SurveyEventProperties",
72
+ "SurveyWidgetType",
67
73
  "Publisher",
74
+ "SurveyTabPosition",
75
+ "SurveyEventName",
76
+ "SurveyQuestionBranchingType",
77
+ "DisplaySurveyType",
78
+ "SurveySchedule",
79
+ "default",
80
+ "SurveyType",
81
+ "SurveyPosition",
68
82
  "createDisposable",
69
83
  "FeatureFlagsCommonExtension",
70
- "default"
71
- ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_types__rspack_import_1[__rspack_import_key];
84
+ "SurveyQuestionType",
85
+ "SurveyEventType"
86
+ ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_types__rspack_import_2[__rspack_import_key];
72
87
  __webpack_require__.d(__webpack_exports__, __rspack_reexport);
73
- var _disposable__rspack_import_2 = __webpack_require__("./disposable");
74
- var _pubsub__rspack_import_3 = __webpack_require__("./pubsub");
88
+ var _disposable__rspack_import_3 = __webpack_require__("./disposable");
89
+ var _pubsub__rspack_import_4 = __webpack_require__("./pubsub");
75
90
  __webpack_require__.d(__webpack_exports__, {
76
- FeatureFlagsCommonExtension: ()=>_extension_tokens__rspack_import_0.FeatureFlagsCommonExtension,
77
- Publisher: ()=>_pubsub__rspack_import_3.Publisher,
78
- createDisposable: ()=>_disposable__rspack_import_2.createDisposable
91
+ DisplaySurveyType: ()=>_survey_constants__rspack_import_0.DisplaySurveyType,
92
+ FeatureFlagsCommonExtension: ()=>_extension_tokens__rspack_import_1.FeatureFlagsCommonExtension,
93
+ Publisher: ()=>_pubsub__rspack_import_4.Publisher,
94
+ SurveyEventName: ()=>_survey_constants__rspack_import_0.SurveyEventName,
95
+ SurveyEventProperties: ()=>_survey_constants__rspack_import_0.SurveyEventProperties,
96
+ SurveyEventType: ()=>_survey_constants__rspack_import_0.SurveyEventType,
97
+ SurveyPosition: ()=>_survey_constants__rspack_import_0.SurveyPosition,
98
+ SurveyQuestionBranchingType: ()=>_survey_constants__rspack_import_0.SurveyQuestionBranchingType,
99
+ SurveyQuestionType: ()=>_survey_constants__rspack_import_0.SurveyQuestionType,
100
+ SurveySchedule: ()=>_survey_constants__rspack_import_0.SurveySchedule,
101
+ SurveyTabPosition: ()=>_survey_constants__rspack_import_0.SurveyTabPosition,
102
+ SurveyType: ()=>_survey_constants__rspack_import_0.SurveyType,
103
+ SurveyWidgetType: ()=>_survey_constants__rspack_import_0.SurveyWidgetType,
104
+ createDisposable: ()=>_disposable__rspack_import_3.createDisposable
79
105
  });
80
106
  })();
107
+ exports.DisplaySurveyType = __webpack_exports__.DisplaySurveyType;
81
108
  exports.FeatureFlagsCommonExtension = __webpack_exports__.FeatureFlagsCommonExtension;
82
109
  exports.Publisher = __webpack_exports__.Publisher;
110
+ exports.SurveyEventName = __webpack_exports__.SurveyEventName;
111
+ exports.SurveyEventProperties = __webpack_exports__.SurveyEventProperties;
112
+ exports.SurveyEventType = __webpack_exports__.SurveyEventType;
113
+ exports.SurveyPosition = __webpack_exports__.SurveyPosition;
114
+ exports.SurveyQuestionBranchingType = __webpack_exports__.SurveyQuestionBranchingType;
115
+ exports.SurveyQuestionType = __webpack_exports__.SurveyQuestionType;
116
+ exports.SurveySchedule = __webpack_exports__.SurveySchedule;
117
+ exports.SurveyTabPosition = __webpack_exports__.SurveyTabPosition;
118
+ exports.SurveyType = __webpack_exports__.SurveyType;
119
+ exports.SurveyWidgetType = __webpack_exports__.SurveyWidgetType;
83
120
  exports.createDisposable = __webpack_exports__.createDisposable;
84
121
  for(var __rspack_i in __webpack_exports__)if (-1 === [
122
+ "DisplaySurveyType",
85
123
  "FeatureFlagsCommonExtension",
86
124
  "Publisher",
125
+ "SurveyEventName",
126
+ "SurveyEventProperties",
127
+ "SurveyEventType",
128
+ "SurveyPosition",
129
+ "SurveyQuestionBranchingType",
130
+ "SurveyQuestionType",
131
+ "SurveySchedule",
132
+ "SurveyTabPosition",
133
+ "SurveyType",
134
+ "SurveyWidgetType",
87
135
  "createDisposable"
88
136
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
89
137
  Object.defineProperty(exports, '__esModule', {
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./types/index.mjs";
2
+ export { DisplaySurveyType, SurveyEventName, SurveyEventProperties, SurveyEventType, SurveyPosition, SurveyQuestionBranchingType, SurveyQuestionType, SurveySchedule, SurveyTabPosition, SurveyType, SurveyWidgetType } from "./survey-constants.mjs";
2
3
  export { FeatureFlagsCommonExtension } from "./extension-tokens.mjs";
3
4
  export { createDisposable } from "./disposable.mjs";
4
5
  export { Publisher } from "./pubsub.mjs";
@@ -0,0 +1,27 @@
1
+ import type { SurveyActionHost, SurveyCapturedEvent as CaptureResult } from './survey-event-host';
2
+ import type { SurveyActionType } from './types/surveys';
3
+ export declare class ActionMatcher {
4
+ private readonly _host?;
5
+ private readonly _actionRegistry;
6
+ private readonly _actionEvents;
7
+ private _debugEventEmitter;
8
+ private _captureHookUnsubscribe;
9
+ constructor(_host?: SurveyActionHost | undefined);
10
+ init(): void;
11
+ dispose(): void;
12
+ register(actions: SurveyActionType[]): void;
13
+ replace(actions: SurveyActionType[]): void;
14
+ on(eventName: string, eventPayload?: CaptureResult): void;
15
+ _addActionHook(callback: (actionName: string, eventPayload?: any) => void): void;
16
+ private _checkAction;
17
+ onAction(event: 'actionCaptured', cb: (...args: any[]) => void): () => void;
18
+ private _checkStep;
19
+ private _checkStepEvent;
20
+ private _checkStepUrl;
21
+ private _checkStepElement;
22
+ private _checkStepHref;
23
+ private _checkStepText;
24
+ private _checkStepSelector;
25
+ private _getElementsList;
26
+ private _checkStepProperties;
27
+ }
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, getters, values)=>{
5
+ var define = (defs, kind)=>{
6
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
+ enumerable: true,
8
+ [kind]: defs[key]
9
+ });
10
+ };
11
+ define(getters, "get");
12
+ define(values, "value");
13
+ };
14
+ })();
15
+ (()=>{
16
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
+ })();
18
+ (()=>{
19
+ __webpack_require__.r = (exports1)=>{
20
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
+ value: 'Module'
22
+ });
23
+ Object.defineProperty(exports1, '__esModule', {
24
+ value: true
25
+ });
26
+ };
27
+ })();
28
+ var __webpack_exports__ = {};
29
+ __webpack_require__.r(__webpack_exports__);
30
+ __webpack_require__.d(__webpack_exports__, {
31
+ ActionMatcher: ()=>ActionMatcher
32
+ });
33
+ const simple_event_emitter_js_namespaceObject = require("./utils/simple-event-emitter.js");
34
+ const core_namespaceObject = require("@posthog/core");
35
+ const surveys_namespaceObject = require("@posthog/core/surveys");
36
+ const elements_chain_utils_js_namespaceObject = require("./utils/elements-chain-utils.js");
37
+ class ActionMatcher {
38
+ constructor(_host){
39
+ this._host = _host;
40
+ this._actionRegistry = new Set();
41
+ this._actionEvents = new Set();
42
+ this._debugEventEmitter = new simple_event_emitter_js_namespaceObject.SimpleEventEmitter();
43
+ this._checkStep = (event, step)=>this._checkStepEvent(event, step) && this._checkStepUrl(event, step) && this._checkStepElement(event, step) && this._checkStepProperties(event, step);
44
+ this._checkStepEvent = (event, step)=>{
45
+ if (step?.event && event?.event !== step?.event) return false;
46
+ return true;
47
+ };
48
+ }
49
+ init() {
50
+ if (!(0, core_namespaceObject.isUndefined)(this._host?.subscribeCapture)) {
51
+ const matchEventToAction = (eventName, eventPayload)=>{
52
+ this.on(eventName, eventPayload);
53
+ };
54
+ this._captureHookUnsubscribe = this._host?.subscribeCapture(matchEventToAction);
55
+ }
56
+ }
57
+ dispose() {
58
+ this._captureHookUnsubscribe?.();
59
+ this._captureHookUnsubscribe = void 0;
60
+ this._host?.setElementSelectors?.(new Set(), this);
61
+ this._debugEventEmitter = new simple_event_emitter_js_namespaceObject.SimpleEventEmitter();
62
+ }
63
+ register(actions) {
64
+ if ((0, core_namespaceObject.isUndefined)(this._host?.subscribeCapture)) return;
65
+ actions.forEach((action)=>{
66
+ this._actionRegistry.add(action);
67
+ action.steps?.forEach((step)=>{
68
+ this._actionEvents.add(step?.event || '');
69
+ });
70
+ });
71
+ if (this._host?.setElementSelectors) {
72
+ const selectorsToWatch = new Set();
73
+ this._actionRegistry.forEach((action)=>{
74
+ action.steps?.forEach((step)=>{
75
+ if (step?.selector) selectorsToWatch.add(step.selector);
76
+ });
77
+ });
78
+ this._host.setElementSelectors(selectorsToWatch, this);
79
+ }
80
+ }
81
+ replace(actions) {
82
+ this._actionRegistry.clear();
83
+ this._actionEvents.clear();
84
+ this.register(actions);
85
+ }
86
+ on(eventName, eventPayload) {
87
+ if (null == eventPayload || 0 == eventName.length) return;
88
+ if (!this._actionEvents.has(eventName) && !this._actionEvents.has(eventPayload.event)) return;
89
+ this._actionRegistry.forEach((action)=>{
90
+ if (this._checkAction(eventPayload, action)) this._debugEventEmitter.emit('actionCaptured', action.name);
91
+ });
92
+ }
93
+ _addActionHook(callback) {
94
+ this.onAction('actionCaptured', (data)=>callback(data));
95
+ }
96
+ _checkAction(event, action) {
97
+ if (action?.steps == null) return false;
98
+ for (const step of action.steps)if (this._checkStep(event, step)) return true;
99
+ return false;
100
+ }
101
+ onAction(event, cb) {
102
+ return this._debugEventEmitter.on(event, cb);
103
+ }
104
+ _checkStepUrl(event, step) {
105
+ if (step?.url) {
106
+ const eventUrl = event?.properties?.$current_url;
107
+ if (!eventUrl || 'string' != typeof eventUrl) return false;
108
+ if (!(0, elements_chain_utils_js_namespaceObject.matchString)(eventUrl, step.url, step.url_matching || 'contains')) return false;
109
+ }
110
+ return true;
111
+ }
112
+ _checkStepElement(event, step) {
113
+ if (!this._checkStepHref(event, step)) return false;
114
+ if (!this._checkStepText(event, step)) return false;
115
+ if (!this._checkStepSelector(event, step)) return false;
116
+ return true;
117
+ }
118
+ _checkStepHref(event, step) {
119
+ if (!step?.href) return true;
120
+ const elements = this._getElementsList(event);
121
+ if (elements.length > 0) return elements.some((el)=>(0, elements_chain_utils_js_namespaceObject.matchString)(el.href, step.href, step.href_matching || 'exact'));
122
+ const chain = event?.properties?.$elements_chain || '';
123
+ if (chain) return (0, elements_chain_utils_js_namespaceObject.matchString)((0, elements_chain_utils_js_namespaceObject.extractHref)(chain), step.href, step.href_matching || 'exact');
124
+ return false;
125
+ }
126
+ _checkStepText(event, step) {
127
+ if (!step?.text) return true;
128
+ const elements = this._getElementsList(event);
129
+ if (elements.length > 0) return elements.some((el)=>(0, elements_chain_utils_js_namespaceObject.matchString)(el.text, step.text, step.text_matching || 'exact') || (0, elements_chain_utils_js_namespaceObject.matchString)(el.$el_text, step.text, step.text_matching || 'exact'));
130
+ const chain = event?.properties?.$elements_chain || '';
131
+ if (chain) return (0, elements_chain_utils_js_namespaceObject.matchTexts)((0, elements_chain_utils_js_namespaceObject.extractTexts)(chain), step.text, step.text_matching || 'exact');
132
+ return false;
133
+ }
134
+ _checkStepSelector(event, step) {
135
+ if (!step?.selector) return true;
136
+ const elementSelectors = event?.properties?.$element_selectors;
137
+ if (elementSelectors?.includes(step.selector)) return true;
138
+ const chain = event?.properties?.$elements_chain || '';
139
+ if (step.selector_regex && chain) try {
140
+ return new RegExp(step.selector_regex).test(chain);
141
+ } catch {}
142
+ return false;
143
+ }
144
+ _getElementsList(event) {
145
+ if (event?.properties?.$elements == null) return [];
146
+ return event?.properties.$elements;
147
+ }
148
+ _checkStepProperties(event, step) {
149
+ if (!step?.properties || 0 === step.properties.length) return true;
150
+ const propertyFilters = step.properties.reduce((acc, filter)=>{
151
+ const values = (0, core_namespaceObject.isArray)(filter.value) ? filter.value.map(String) : null != filter.value ? [
152
+ String(filter.value)
153
+ ] : [];
154
+ acc[filter.key] = {
155
+ values,
156
+ operator: filter.operator || 'exact'
157
+ };
158
+ return acc;
159
+ }, {});
160
+ return (0, surveys_namespaceObject.matchPropertyFilters)(propertyFilters, event?.properties);
161
+ }
162
+ }
163
+ exports.ActionMatcher = __webpack_exports__.ActionMatcher;
164
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
165
+ "ActionMatcher"
166
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
167
+ Object.defineProperty(exports, '__esModule', {
168
+ value: true
169
+ });
@@ -0,0 +1,131 @@
1
+ import { SimpleEventEmitter } from "./utils/simple-event-emitter.mjs";
2
+ import { isArray, isUndefined } from "@posthog/core";
3
+ import { matchPropertyFilters } from "@posthog/core/surveys";
4
+ import { extractHref, extractTexts, matchString, matchTexts } from "./utils/elements-chain-utils.mjs";
5
+ class ActionMatcher {
6
+ constructor(_host){
7
+ this._host = _host;
8
+ this._actionRegistry = new Set();
9
+ this._actionEvents = new Set();
10
+ this._debugEventEmitter = new SimpleEventEmitter();
11
+ this._checkStep = (event, step)=>this._checkStepEvent(event, step) && this._checkStepUrl(event, step) && this._checkStepElement(event, step) && this._checkStepProperties(event, step);
12
+ this._checkStepEvent = (event, step)=>{
13
+ if (step?.event && event?.event !== step?.event) return false;
14
+ return true;
15
+ };
16
+ }
17
+ init() {
18
+ if (!isUndefined(this._host?.subscribeCapture)) {
19
+ const matchEventToAction = (eventName, eventPayload)=>{
20
+ this.on(eventName, eventPayload);
21
+ };
22
+ this._captureHookUnsubscribe = this._host?.subscribeCapture(matchEventToAction);
23
+ }
24
+ }
25
+ dispose() {
26
+ this._captureHookUnsubscribe?.();
27
+ this._captureHookUnsubscribe = void 0;
28
+ this._host?.setElementSelectors?.(new Set(), this);
29
+ this._debugEventEmitter = new SimpleEventEmitter();
30
+ }
31
+ register(actions) {
32
+ if (isUndefined(this._host?.subscribeCapture)) return;
33
+ actions.forEach((action)=>{
34
+ this._actionRegistry.add(action);
35
+ action.steps?.forEach((step)=>{
36
+ this._actionEvents.add(step?.event || '');
37
+ });
38
+ });
39
+ if (this._host?.setElementSelectors) {
40
+ const selectorsToWatch = new Set();
41
+ this._actionRegistry.forEach((action)=>{
42
+ action.steps?.forEach((step)=>{
43
+ if (step?.selector) selectorsToWatch.add(step.selector);
44
+ });
45
+ });
46
+ this._host.setElementSelectors(selectorsToWatch, this);
47
+ }
48
+ }
49
+ replace(actions) {
50
+ this._actionRegistry.clear();
51
+ this._actionEvents.clear();
52
+ this.register(actions);
53
+ }
54
+ on(eventName, eventPayload) {
55
+ if (null == eventPayload || 0 == eventName.length) return;
56
+ if (!this._actionEvents.has(eventName) && !this._actionEvents.has(eventPayload.event)) return;
57
+ this._actionRegistry.forEach((action)=>{
58
+ if (this._checkAction(eventPayload, action)) this._debugEventEmitter.emit('actionCaptured', action.name);
59
+ });
60
+ }
61
+ _addActionHook(callback) {
62
+ this.onAction('actionCaptured', (data)=>callback(data));
63
+ }
64
+ _checkAction(event, action) {
65
+ if (action?.steps == null) return false;
66
+ for (const step of action.steps)if (this._checkStep(event, step)) return true;
67
+ return false;
68
+ }
69
+ onAction(event, cb) {
70
+ return this._debugEventEmitter.on(event, cb);
71
+ }
72
+ _checkStepUrl(event, step) {
73
+ if (step?.url) {
74
+ const eventUrl = event?.properties?.$current_url;
75
+ if (!eventUrl || 'string' != typeof eventUrl) return false;
76
+ if (!matchString(eventUrl, step.url, step.url_matching || 'contains')) return false;
77
+ }
78
+ return true;
79
+ }
80
+ _checkStepElement(event, step) {
81
+ if (!this._checkStepHref(event, step)) return false;
82
+ if (!this._checkStepText(event, step)) return false;
83
+ if (!this._checkStepSelector(event, step)) return false;
84
+ return true;
85
+ }
86
+ _checkStepHref(event, step) {
87
+ if (!step?.href) return true;
88
+ const elements = this._getElementsList(event);
89
+ if (elements.length > 0) return elements.some((el)=>matchString(el.href, step.href, step.href_matching || 'exact'));
90
+ const chain = event?.properties?.$elements_chain || '';
91
+ if (chain) return matchString(extractHref(chain), step.href, step.href_matching || 'exact');
92
+ return false;
93
+ }
94
+ _checkStepText(event, step) {
95
+ if (!step?.text) return true;
96
+ const elements = this._getElementsList(event);
97
+ if (elements.length > 0) return elements.some((el)=>matchString(el.text, step.text, step.text_matching || 'exact') || matchString(el.$el_text, step.text, step.text_matching || 'exact'));
98
+ const chain = event?.properties?.$elements_chain || '';
99
+ if (chain) return matchTexts(extractTexts(chain), step.text, step.text_matching || 'exact');
100
+ return false;
101
+ }
102
+ _checkStepSelector(event, step) {
103
+ if (!step?.selector) return true;
104
+ const elementSelectors = event?.properties?.$element_selectors;
105
+ if (elementSelectors?.includes(step.selector)) return true;
106
+ const chain = event?.properties?.$elements_chain || '';
107
+ if (step.selector_regex && chain) try {
108
+ return new RegExp(step.selector_regex).test(chain);
109
+ } catch {}
110
+ return false;
111
+ }
112
+ _getElementsList(event) {
113
+ if (event?.properties?.$elements == null) return [];
114
+ return event?.properties.$elements;
115
+ }
116
+ _checkStepProperties(event, step) {
117
+ if (!step?.properties || 0 === step.properties.length) return true;
118
+ const propertyFilters = step.properties.reduce((acc, filter)=>{
119
+ const values = isArray(filter.value) ? filter.value.map(String) : null != filter.value ? [
120
+ String(filter.value)
121
+ ] : [];
122
+ acc[filter.key] = {
123
+ values,
124
+ operator: filter.operator || 'exact'
125
+ };
126
+ return acc;
127
+ }, {});
128
+ return matchPropertyFilters(propertyFilters, event?.properties);
129
+ }
130
+ }
131
+ export { ActionMatcher };
@@ -0,0 +1,86 @@
1
+ export declare const SurveyEventType: {
2
+ readonly Activation: 'events';
3
+ readonly Cancellation: 'cancelEvents';
4
+ };
5
+ export declare const SurveyWidgetType: {
6
+ readonly Button: 'button';
7
+ readonly Tab: 'tab';
8
+ readonly Selector: 'selector';
9
+ };
10
+ export declare const SurveyPosition: {
11
+ readonly TopLeft: 'top_left';
12
+ readonly TopRight: 'top_right';
13
+ readonly TopCenter: 'top_center';
14
+ readonly MiddleLeft: 'middle_left';
15
+ readonly MiddleRight: 'middle_right';
16
+ readonly MiddleCenter: 'middle_center';
17
+ readonly Left: 'left';
18
+ readonly Center: 'center';
19
+ readonly Right: 'right';
20
+ readonly NextToTrigger: 'next_to_trigger';
21
+ };
22
+ export declare const SurveyTabPosition: {
23
+ readonly Top: 'top';
24
+ readonly Left: 'left';
25
+ readonly Right: 'right';
26
+ readonly Bottom: 'bottom';
27
+ };
28
+ export declare const SurveyType: {
29
+ readonly Popover: 'popover';
30
+ readonly API: 'api';
31
+ readonly Widget: 'widget';
32
+ readonly ExternalSurvey: 'external_survey';
33
+ };
34
+ export declare const SurveyQuestionType: {
35
+ readonly Open: 'open';
36
+ readonly MultipleChoice: 'multiple_choice';
37
+ readonly SingleChoice: 'single_choice';
38
+ readonly Rating: 'rating';
39
+ readonly Link: 'link';
40
+ };
41
+ export declare const SurveyQuestionBranchingType: {
42
+ readonly NextQuestion: 'next_question';
43
+ readonly End: 'end';
44
+ readonly ResponseBased: 'response_based';
45
+ readonly SpecificQuestion: 'specific_question';
46
+ };
47
+ export declare const SurveySchedule: {
48
+ readonly Once: 'once';
49
+ readonly Recurring: 'recurring';
50
+ readonly Always: 'always';
51
+ };
52
+ export declare const SurveyEventName: {
53
+ readonly SHOWN: 'survey shown';
54
+ readonly DISMISSED: 'survey dismissed';
55
+ readonly SENT: 'survey sent';
56
+ readonly ABANDONED: 'survey abandoned';
57
+ };
58
+ export declare const SurveyEventProperties: {
59
+ readonly SURVEY_ID: '$survey_id';
60
+ readonly SURVEY_NAME: '$survey_name';
61
+ readonly SURVEY_RESPONSE: '$survey_response';
62
+ readonly SURVEY_ITERATION: '$survey_iteration';
63
+ readonly SURVEY_ITERATION_START_DATE: '$survey_iteration_start_date';
64
+ readonly SURVEY_PARTIALLY_COMPLETED: '$survey_partially_completed';
65
+ readonly SURVEY_SUBMISSION_ID: '$survey_submission_id';
66
+ readonly SURVEY_QUESTIONS: '$survey_questions';
67
+ readonly SURVEY_COMPLETED: '$survey_completed';
68
+ readonly PRODUCT_TOUR_ID: '$product_tour_id';
69
+ readonly SURVEY_LAST_SEEN_DATE: '$survey_last_seen_date';
70
+ readonly SURVEY_LANGUAGE: '$survey_language';
71
+ };
72
+ export declare const DisplaySurveyType: {
73
+ readonly Popover: 'popover';
74
+ readonly Inline: 'inline';
75
+ };
76
+ export type SurveyEventType = (typeof SurveyEventType)[keyof typeof SurveyEventType];
77
+ export type SurveyWidgetType = (typeof SurveyWidgetType)[keyof typeof SurveyWidgetType];
78
+ export type SurveyPosition = (typeof SurveyPosition)[keyof typeof SurveyPosition];
79
+ export type SurveyTabPosition = (typeof SurveyTabPosition)[keyof typeof SurveyTabPosition];
80
+ export type SurveyType = (typeof SurveyType)[keyof typeof SurveyType];
81
+ export type SurveyQuestionType = (typeof SurveyQuestionType)[keyof typeof SurveyQuestionType];
82
+ export type SurveyQuestionBranchingType = (typeof SurveyQuestionBranchingType)[keyof typeof SurveyQuestionBranchingType];
83
+ export type SurveySchedule = (typeof SurveySchedule)[keyof typeof SurveySchedule];
84
+ export type SurveyEventName = (typeof SurveyEventName)[keyof typeof SurveyEventName];
85
+ export type SurveyEventProperties = (typeof SurveyEventProperties)[keyof typeof SurveyEventProperties];
86
+ export type DisplaySurveyType = (typeof DisplaySurveyType)[keyof typeof DisplaySurveyType];