@leanbase.com/js 0.1.3 → 0.2.0-alpha.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.
Files changed (137) hide show
  1. package/LICENSE +37 -0
  2. package/dist/autocapture-utils.d.ts +17 -0
  3. package/dist/autocapture.d.ts +35 -0
  4. package/dist/config.d.ts +5 -0
  5. package/dist/constants.d.ts +54 -0
  6. package/dist/entrypoints/main.cjs.d.ts +4 -0
  7. package/dist/entrypoints/module.es.d.ts +4 -0
  8. package/dist/extensions/rageclick.d.ts +9 -0
  9. package/dist/iife.d.ts +19 -0
  10. package/dist/index.d.ts +2 -779
  11. package/dist/leanbase-logger.d.ts +6 -0
  12. package/dist/leanbase-persistence.d.ts +64 -0
  13. package/dist/leanbase.d.ts +49 -0
  14. package/dist/leanbase.iife.js +1 -4747
  15. package/dist/leanbase.iife.js.map +1 -1
  16. package/dist/main.js +2 -0
  17. package/dist/main.js.map +1 -0
  18. package/dist/module.d.ts +780 -0
  19. package/dist/module.js +2 -0
  20. package/dist/module.js.map +1 -0
  21. package/dist/page-view.d.ts +29 -0
  22. package/dist/scroll-manager.d.ts +21 -0
  23. package/dist/session-props.d.ts +32 -0
  24. package/dist/sessionid.d.ts +50 -0
  25. package/dist/storage.d.ts +24 -0
  26. package/{src/types.ts → dist/types.d.ts} +145 -235
  27. package/dist/utils/blocked-uas.d.ts +17 -0
  28. package/dist/utils/element-utils.d.ts +5 -0
  29. package/dist/utils/event-utils.d.ts +22 -0
  30. package/dist/utils/index.d.ts +44 -0
  31. package/dist/utils/request-utils.d.ts +12 -0
  32. package/dist/utils/simple-event-emitter.d.ts +6 -0
  33. package/dist/utils/user-agent-utils.d.ts +18 -0
  34. package/dist/uuidv7.d.ts +43 -0
  35. package/dist/version.d.ts +1 -0
  36. package/lib/autocapture-utils.d.ts +17 -0
  37. package/{src/autocapture-utils.ts → lib/autocapture-utils.js} +196 -280
  38. package/lib/autocapture-utils.js.map +1 -0
  39. package/lib/autocapture.d.ts +35 -0
  40. package/lib/autocapture.js +311 -0
  41. package/lib/autocapture.js.map +1 -0
  42. package/lib/config.d.ts +5 -0
  43. package/lib/config.js +7 -0
  44. package/lib/config.js.map +1 -0
  45. package/lib/constants.d.ts +54 -0
  46. package/{src/constants.ts → lib/constants.js} +50 -57
  47. package/lib/constants.js.map +1 -0
  48. package/lib/entrypoints/main.cjs.d.ts +4 -0
  49. package/lib/entrypoints/main.cjs.js +3 -0
  50. package/lib/entrypoints/main.cjs.js.map +1 -0
  51. package/lib/entrypoints/module.es.d.ts +4 -0
  52. package/lib/entrypoints/module.es.js +3 -0
  53. package/lib/entrypoints/module.es.js.map +1 -0
  54. package/lib/extensions/rageclick.d.ts +9 -0
  55. package/lib/extensions/rageclick.js +27 -0
  56. package/lib/extensions/rageclick.js.map +1 -0
  57. package/lib/iife.d.ts +19 -0
  58. package/lib/iife.js +67 -0
  59. package/lib/iife.js.map +1 -0
  60. package/{src/index.ts → lib/index.d.ts} +2 -2
  61. package/lib/index.js +2 -0
  62. package/lib/index.js.map +1 -0
  63. package/lib/leanbase-logger.d.ts +6 -0
  64. package/lib/leanbase-logger.js +25 -0
  65. package/lib/leanbase-logger.js.map +1 -0
  66. package/lib/leanbase-persistence.d.ts +64 -0
  67. package/lib/leanbase-persistence.js +287 -0
  68. package/lib/leanbase-persistence.js.map +1 -0
  69. package/lib/leanbase.d.ts +49 -0
  70. package/lib/leanbase.js +294 -0
  71. package/lib/leanbase.js.map +1 -0
  72. package/lib/page-view.d.ts +29 -0
  73. package/lib/page-view.js +81 -0
  74. package/lib/page-view.js.map +1 -0
  75. package/lib/scroll-manager.d.ts +21 -0
  76. package/lib/scroll-manager.js +79 -0
  77. package/lib/scroll-manager.js.map +1 -0
  78. package/lib/session-props.d.ts +32 -0
  79. package/lib/session-props.js +73 -0
  80. package/lib/session-props.js.map +1 -0
  81. package/lib/sessionid.d.ts +50 -0
  82. package/{src/sessionid.ts → lib/sessionid.js} +128 -204
  83. package/lib/sessionid.js.map +1 -0
  84. package/lib/storage.d.ts +24 -0
  85. package/{src/storage.ts → lib/storage.js} +182 -225
  86. package/lib/storage.js.map +1 -0
  87. package/lib/types.d.ts +544 -0
  88. package/lib/types.js +7 -0
  89. package/lib/types.js.map +1 -0
  90. package/lib/utils/blocked-uas.d.ts +17 -0
  91. package/{src/utils/blocked-uas.ts → lib/utils/blocked-uas.js} +19 -48
  92. package/lib/utils/blocked-uas.js.map +1 -0
  93. package/lib/utils/element-utils.d.ts +5 -0
  94. package/{src/utils/element-utils.ts → lib/utils/element-utils.js} +13 -17
  95. package/lib/utils/element-utils.js.map +1 -0
  96. package/lib/utils/event-utils.d.ts +22 -0
  97. package/lib/utils/event-utils.js +258 -0
  98. package/lib/utils/event-utils.js.map +1 -0
  99. package/lib/utils/index.d.ts +44 -0
  100. package/lib/utils/index.js +183 -0
  101. package/lib/utils/index.js.map +1 -0
  102. package/lib/utils/request-utils.d.ts +12 -0
  103. package/lib/utils/request-utils.js +107 -0
  104. package/lib/utils/request-utils.js.map +1 -0
  105. package/lib/utils/simple-event-emitter.d.ts +6 -0
  106. package/lib/utils/simple-event-emitter.js +24 -0
  107. package/lib/utils/simple-event-emitter.js.map +1 -0
  108. package/lib/utils/user-agent-utils.d.ts +18 -0
  109. package/lib/utils/user-agent-utils.js +369 -0
  110. package/lib/utils/user-agent-utils.js.map +1 -0
  111. package/lib/uuidv7.d.ts +43 -0
  112. package/{src/uuidv7.ts → lib/uuidv7.js} +103 -131
  113. package/lib/uuidv7.js.map +1 -0
  114. package/lib/version.d.ts +1 -0
  115. package/lib/version.js +2 -0
  116. package/lib/version.js.map +1 -0
  117. package/package.json +56 -45
  118. package/dist/index.cjs +0 -3034
  119. package/dist/index.cjs.map +0 -1
  120. package/dist/index.mjs +0 -3032
  121. package/dist/index.mjs.map +0 -1
  122. package/src/autocapture.ts +0 -415
  123. package/src/config.ts +0 -8
  124. package/src/extensions/rageclick.ts +0 -34
  125. package/src/iife.ts +0 -87
  126. package/src/leanbase-logger.ts +0 -26
  127. package/src/leanbase-persistence.ts +0 -374
  128. package/src/leanbase.ts +0 -424
  129. package/src/page-view.ts +0 -124
  130. package/src/scroll-manager.ts +0 -103
  131. package/src/session-props.ts +0 -114
  132. package/src/utils/event-utils.ts +0 -304
  133. package/src/utils/index.ts +0 -222
  134. package/src/utils/request-utils.ts +0 -128
  135. package/src/utils/simple-event-emitter.ts +0 -27
  136. package/src/utils/user-agent-utils.ts +0 -357
  137. package/src/version.ts +0 -1
@@ -0,0 +1,294 @@
1
+ import { PostHogCore, getFetch, isEmptyObject, isEmptyString, isNumber, isObject, isString, isUndefined, } from '@posthog/core';
2
+ import { LeanbasePersistence } from './leanbase-persistence';
3
+ import { addEventListener, copyAndTruncateStrings, document, extend, isCrossDomainCookie, navigator, userAgent, } from './utils';
4
+ import Config from './config';
5
+ import { Autocapture } from './autocapture';
6
+ import { logger } from './leanbase-logger';
7
+ import { COOKIELESS_MODE_FLAG_PROPERTY, USER_STATE } from './constants';
8
+ import { getEventProperties } from './utils/event-utils';
9
+ import { uuidv7 } from './uuidv7';
10
+ import { PageViewManager } from './page-view';
11
+ import { ScrollManager } from './scroll-manager';
12
+ import { isLikelyBot } from './utils/blocked-uas';
13
+ const defaultConfig = () => ({
14
+ host: 'https://i.leanbase.co',
15
+ token: '',
16
+ autocapture: true,
17
+ rageclick: true,
18
+ persistence: 'localStorage+cookie',
19
+ capture_pageview: true,
20
+ capture_pageleave: 'if_capture_pageview',
21
+ persistence_name: '',
22
+ mask_all_element_attributes: false,
23
+ cookie_expiration: 365,
24
+ cross_subdomain_cookie: isCrossDomainCookie(document?.location),
25
+ custom_campaign_params: [],
26
+ custom_personal_data_properties: [],
27
+ disable_persistence: false,
28
+ mask_personal_data_properties: false,
29
+ secure_cookie: window?.location?.protocol === 'https:',
30
+ mask_all_text: false,
31
+ bootstrap: {},
32
+ session_idle_timeout_seconds: 30 * 60,
33
+ save_campaign_params: true,
34
+ save_referrer: true,
35
+ opt_out_useragent_filter: false,
36
+ properties_string_max_length: 65535,
37
+ loaded: () => { },
38
+ });
39
+ export class Leanbase extends PostHogCore {
40
+ constructor(token, config) {
41
+ const mergedConfig = extend(defaultConfig(), config || {}, {
42
+ token,
43
+ });
44
+ super(token, mergedConfig);
45
+ this.personProcessingSetOncePropertiesSent = false;
46
+ this.isLoaded = false;
47
+ this.config = mergedConfig;
48
+ this.visibilityStateListener = null;
49
+ this.initialPageviewCaptured = false;
50
+ this.scrollManager = new ScrollManager(this);
51
+ this.pageViewManager = new PageViewManager(this);
52
+ this.init(token, mergedConfig);
53
+ }
54
+ init(token, config) {
55
+ this.setConfig(extend(defaultConfig(), config, {
56
+ token,
57
+ }));
58
+ this.isLoaded = true;
59
+ this.persistence = new LeanbasePersistence(this.config);
60
+ this.replayAutocapture = new Autocapture(this);
61
+ this.replayAutocapture.startIfEnabled();
62
+ if (this.config.preloadFeatureFlags !== false) {
63
+ this.reloadFeatureFlags();
64
+ }
65
+ this.config.loaded?.(this);
66
+ if (this.config.capture_pageview) {
67
+ setTimeout(() => {
68
+ if (this.config.cookieless_mode === 'always') {
69
+ this.captureInitialPageview();
70
+ }
71
+ }, 1);
72
+ }
73
+ addEventListener(document, 'DOMContentLoaded', () => {
74
+ this.loadRemoteConfig();
75
+ });
76
+ addEventListener(window, 'onpagehide' in self ? 'pagehide' : 'unload', this.capturePageLeave.bind(this), {
77
+ passive: false,
78
+ });
79
+ }
80
+ captureInitialPageview() {
81
+ if (!document) {
82
+ return;
83
+ }
84
+ if (document.visibilityState !== 'visible') {
85
+ if (!this.visibilityStateListener) {
86
+ this.visibilityStateListener = this.captureInitialPageview.bind(this);
87
+ addEventListener(document, 'visibilitychange', this.visibilityStateListener);
88
+ }
89
+ return;
90
+ }
91
+ if (!this.initialPageviewCaptured) {
92
+ this.initialPageviewCaptured = true;
93
+ this.capture('$pageview', { title: document.title });
94
+ if (this.visibilityStateListener) {
95
+ document.removeEventListener('visibilitychange', this.visibilityStateListener);
96
+ this.visibilityStateListener = null;
97
+ }
98
+ }
99
+ }
100
+ capturePageLeave() {
101
+ const { capture_pageleave, capture_pageview } = this.config;
102
+ if (capture_pageleave === true ||
103
+ (capture_pageleave === 'if_capture_pageview' &&
104
+ (capture_pageview === true || capture_pageview === 'history_change'))) {
105
+ this.capture('$pageleave');
106
+ }
107
+ }
108
+ async loadRemoteConfig() {
109
+ if (!this.isRemoteConfigLoaded) {
110
+ const remoteConfig = await this.reloadRemoteConfigAsync();
111
+ if (remoteConfig) {
112
+ this.onRemoteConfig(remoteConfig);
113
+ }
114
+ }
115
+ }
116
+ onRemoteConfig(config) {
117
+ if (!(document && document.body)) {
118
+ setTimeout(() => {
119
+ this.onRemoteConfig(config);
120
+ }, 500);
121
+ return;
122
+ }
123
+ this.isRemoteConfigLoaded = true;
124
+ this.replayAutocapture?.onRemoteConfig(config);
125
+ }
126
+ fetch(url, options) {
127
+ const fetchFn = getFetch();
128
+ if (!fetchFn) {
129
+ return Promise.reject(new Error('Fetch API is not available in this environment.'));
130
+ }
131
+ return fetchFn(url, options);
132
+ }
133
+ setConfig(config) {
134
+ const oldConfig = { ...this.config };
135
+ if (isObject(config)) {
136
+ extend(this.config, config);
137
+ this.persistence?.update_config(this.config, oldConfig);
138
+ this.replayAutocapture?.startIfEnabled();
139
+ }
140
+ const isTempStorage = this.config.persistence === 'sessionStorage' || this.config.persistence === 'memory';
141
+ this.sessionPersistence = isTempStorage
142
+ ? this.persistence
143
+ : new LeanbasePersistence({ ...this.config, persistence: 'sessionStorage' });
144
+ }
145
+ getLibraryId() {
146
+ return 'leanbase';
147
+ }
148
+ getLibraryVersion() {
149
+ return Config.LIB_VERSION;
150
+ }
151
+ getCustomUserAgent() {
152
+ return;
153
+ }
154
+ getPersistedProperty(key) {
155
+ return this.persistence?.get_property(key);
156
+ }
157
+ setPersistedProperty(key, value) {
158
+ this.persistence?.set_property(key, value);
159
+ }
160
+ calculateEventProperties(eventName, eventProperties, timestamp, uuid, readOnly) {
161
+ if (!this.persistence || !this.sessionPersistence) {
162
+ return eventProperties;
163
+ }
164
+ timestamp = timestamp || new Date();
165
+ const startTimestamp = readOnly ? undefined : this.persistence?.remove_event_timer(eventName);
166
+ let properties = { ...eventProperties };
167
+ properties['token'] = this.config.token;
168
+ if (this.config.cookieless_mode == 'always' || this.config.cookieless_mode == 'on_reject') {
169
+ properties[COOKIELESS_MODE_FLAG_PROPERTY] = true;
170
+ }
171
+ if (eventName === '$snapshot') {
172
+ const persistenceProps = { ...this.persistence.properties() };
173
+ properties['distinct_id'] = persistenceProps.distinct_id;
174
+ if (!(isString(properties['distinct_id']) || isNumber(properties['distinct_id'])) ||
175
+ isEmptyString(properties['distinct_id'])) {
176
+ logger.error('Invalid distinct_id for replay event. This indicates a bug in your implementation');
177
+ }
178
+ return properties;
179
+ }
180
+ const infoProperties = getEventProperties(this.config.mask_personal_data_properties, this.config.custom_personal_data_properties);
181
+ if (this.sessionManager) {
182
+ const { sessionId, windowId } = this.sessionManager.checkAndGetSessionAndWindowId(readOnly, timestamp.getTime());
183
+ properties['$session_id'] = sessionId;
184
+ properties['$window_id'] = windowId;
185
+ }
186
+ if (this.sessionPropsManager) {
187
+ extend(properties, this.sessionPropsManager.getSessionProps());
188
+ }
189
+ let pageviewProperties = this.pageViewManager.doEvent();
190
+ if (eventName === '$pageview' && !readOnly) {
191
+ pageviewProperties = this.pageViewManager.doPageView(timestamp, uuid);
192
+ }
193
+ if (eventName === '$pageleave' && !readOnly) {
194
+ pageviewProperties = this.pageViewManager.doPageLeave(timestamp);
195
+ }
196
+ properties = extend(properties, pageviewProperties);
197
+ if (eventName === '$pageview' && document) {
198
+ properties['title'] = document.title;
199
+ }
200
+ if (!isUndefined(startTimestamp)) {
201
+ const duration_in_ms = timestamp.getTime() - startTimestamp;
202
+ properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3));
203
+ }
204
+ if (userAgent && this.config.opt_out_useragent_filter) {
205
+ properties['$browser_type'] = isLikelyBot(navigator, []) ? 'bot' : 'browser';
206
+ }
207
+ properties = extend({}, infoProperties, this.persistence.properties(), this.sessionPersistence.properties(), properties);
208
+ properties['$is_identified'] = this.isIdentified();
209
+ return properties;
210
+ }
211
+ isIdentified() {
212
+ return (this.persistence?.get_property(USER_STATE) === 'identified' ||
213
+ this.sessionPersistence?.get_property(USER_STATE) === 'identified');
214
+ }
215
+ /**
216
+ * Add additional set_once properties to the event when creating a person profile. This allows us to create the
217
+ * profile with mostly-accurate properties, despite earlier events not setting them. We do this by storing them in
218
+ * persistence.
219
+ * @param dataSetOnce
220
+ */
221
+ calculateSetOnceProperties(dataSetOnce) {
222
+ if (!this.persistence) {
223
+ return dataSetOnce;
224
+ }
225
+ if (this.personProcessingSetOncePropertiesSent) {
226
+ return dataSetOnce;
227
+ }
228
+ const initialProps = this.persistence.get_initial_props();
229
+ const sessionProps = this.sessionPropsManager?.getSetOnceProps();
230
+ const setOnceProperties = extend({}, initialProps, sessionProps || {}, dataSetOnce || {});
231
+ this.personProcessingSetOncePropertiesSent = true;
232
+ if (isEmptyObject(setOnceProperties)) {
233
+ return undefined;
234
+ }
235
+ return setOnceProperties;
236
+ }
237
+ capture(event, properties, options) {
238
+ if (!this.isLoaded || !this.sessionPersistence || !this.persistence) {
239
+ return;
240
+ }
241
+ if (isUndefined(event) || !isString(event)) {
242
+ logger.error('No event name provided to posthog.capture');
243
+ return;
244
+ }
245
+ if (properties?.$current_url && !isString(properties?.$current_url)) {
246
+ logger.error('Invalid `$current_url` property provided to `posthog.capture`. Input must be a string. Ignoring provided value.');
247
+ delete properties?.$current_url;
248
+ }
249
+ this.sessionPersistence.update_search_keyword();
250
+ if (this.config.save_campaign_params) {
251
+ this.sessionPersistence.update_campaign_params();
252
+ }
253
+ if (this.config.save_referrer) {
254
+ this.sessionPersistence.update_referrer_info();
255
+ }
256
+ if (this.config.save_campaign_params || this.config.save_referrer) {
257
+ this.persistence.set_initial_person_info();
258
+ }
259
+ const systemTime = new Date();
260
+ const timestamp = options?.timestamp || systemTime;
261
+ const uuid = uuidv7();
262
+ let data = {
263
+ uuid,
264
+ event,
265
+ properties: this.calculateEventProperties(event, properties || {}, timestamp, uuid),
266
+ };
267
+ const setProperties = options?.$set;
268
+ if (setProperties) {
269
+ data.$set = options?.$set;
270
+ }
271
+ const setOnceProperties = this.calculateSetOnceProperties(options?.$set_once);
272
+ if (setOnceProperties) {
273
+ data.$set_once = setOnceProperties;
274
+ }
275
+ data = copyAndTruncateStrings(data, options?._noTruncate ? null : this.config.properties_string_max_length);
276
+ data.timestamp = timestamp;
277
+ if (!isUndefined(options?.timestamp)) {
278
+ data.properties['$event_time_override_provided'] = true;
279
+ data.properties['$event_time_override_system_time'] = systemTime;
280
+ }
281
+ const finalSet = { ...data.properties['$set'], ...data['$set'] };
282
+ if (!isEmptyObject(finalSet)) {
283
+ this.setPersonPropertiesForFlags(finalSet);
284
+ }
285
+ super.capture(data.event, data.properties, options);
286
+ }
287
+ identify(distinctId, properties, options) {
288
+ super.identify(distinctId, properties, options);
289
+ }
290
+ destroy() {
291
+ this.persistence?.clear();
292
+ }
293
+ }
294
+ //# sourceMappingURL=leanbase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"leanbase.js","sourceRoot":"","sources":["../src/leanbase.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,QAAQ,EACR,aAAa,EACb,aAAa,EACb,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,WAAW,GACd,MAAM,eAAe,CAAA;AActB,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAC5D,OAAO,EACH,gBAAgB,EAChB,sBAAsB,EACtB,QAAQ,EACR,MAAM,EACN,mBAAmB,EACnB,SAAS,EACT,SAAS,GACZ,MAAM,SAAS,CAAA;AAChB,OAAO,MAAM,MAAM,UAAU,CAAA;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,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@@
1
+ import { Leanbase } from './leanbase';
2
+ interface PageViewEventProperties {
3
+ $pageview_id?: string;
4
+ $prev_pageview_id?: string;
5
+ $prev_pageview_pathname?: string;
6
+ $prev_pageview_duration?: number;
7
+ $prev_pageview_last_scroll?: number;
8
+ $prev_pageview_last_scroll_percentage?: number;
9
+ $prev_pageview_max_scroll?: number;
10
+ $prev_pageview_max_scroll_percentage?: number;
11
+ $prev_pageview_last_content?: number;
12
+ $prev_pageview_last_content_percentage?: number;
13
+ $prev_pageview_max_content?: number;
14
+ $prev_pageview_max_content_percentage?: number;
15
+ }
16
+ export declare class PageViewManager {
17
+ _currentPageview?: {
18
+ timestamp: Date;
19
+ pageViewId: string | undefined;
20
+ pathname: string | undefined;
21
+ };
22
+ _instance: Leanbase;
23
+ constructor(instance: Leanbase);
24
+ doPageView(timestamp: Date, pageViewId?: string): PageViewEventProperties;
25
+ doPageLeave(timestamp: Date): PageViewEventProperties;
26
+ doEvent(): PageViewEventProperties;
27
+ private _previousPageViewProperties;
28
+ }
29
+ export {};
@@ -0,0 +1,81 @@
1
+ import { window } from './utils';
2
+ import { clampToRange, isUndefined } from '@posthog/core';
3
+ import { extend } from './utils';
4
+ import { logger } from './leanbase-logger';
5
+ // This keeps track of the PageView state (such as the previous PageView's path, timestamp, id, and scroll properties).
6
+ // We store the state in memory, which means that for non-SPA sites, the state will be lost on page reload. This means
7
+ // that non-SPA sites should always send a $pageleave event on any navigation, before the page unloads. For SPA sites,
8
+ // they only need to send a $pageleave event when the user navigates away from the site, as the information is not lost
9
+ // on an internal navigation, and is included as the $prev_pageview_ properties in the next $pageview event.
10
+ // Practically, this means that to find the scroll properties for a given pageview, you need to find the event where
11
+ // event name is $pageview or $pageleave and where $prev_pageview_id matches the original pageview event's id.
12
+ export class PageViewManager {
13
+ constructor(instance) {
14
+ this._instance = instance;
15
+ }
16
+ doPageView(timestamp, pageViewId) {
17
+ const response = this._previousPageViewProperties(timestamp, pageViewId);
18
+ // On a pageview we reset the contexts
19
+ this._currentPageview = { pathname: window?.location.pathname ?? '', pageViewId, timestamp };
20
+ this._instance.scrollManager.resetContext();
21
+ return response;
22
+ }
23
+ doPageLeave(timestamp) {
24
+ return this._previousPageViewProperties(timestamp, this._currentPageview?.pageViewId);
25
+ }
26
+ doEvent() {
27
+ return { $pageview_id: this._currentPageview?.pageViewId };
28
+ }
29
+ _previousPageViewProperties(timestamp, pageviewId) {
30
+ const previousPageView = this._currentPageview;
31
+ if (!previousPageView) {
32
+ return { $pageview_id: pageviewId };
33
+ }
34
+ let properties = {
35
+ $pageview_id: pageviewId,
36
+ $prev_pageview_id: previousPageView.pageViewId,
37
+ };
38
+ const scrollContext = this._instance.scrollManager.getContext();
39
+ if (scrollContext && !this._instance.config.disable_scroll_properties) {
40
+ let { maxScrollHeight, lastScrollY, maxScrollY, maxContentHeight, lastContentY, maxContentY } = scrollContext;
41
+ if (!isUndefined(maxScrollHeight) &&
42
+ !isUndefined(lastScrollY) &&
43
+ !isUndefined(maxScrollY) &&
44
+ !isUndefined(maxContentHeight) &&
45
+ !isUndefined(lastContentY) &&
46
+ !isUndefined(maxContentY)) {
47
+ // Use ceil, so that e.g. scrolling 999.5px of a 1000px page is considered 100% scrolled
48
+ maxScrollHeight = Math.ceil(maxScrollHeight);
49
+ lastScrollY = Math.ceil(lastScrollY);
50
+ maxScrollY = Math.ceil(maxScrollY);
51
+ maxContentHeight = Math.ceil(maxContentHeight);
52
+ lastContentY = Math.ceil(lastContentY);
53
+ maxContentY = Math.ceil(maxContentY);
54
+ // if the maximum scroll height is near 0, then the percentage is 1
55
+ const lastScrollPercentage = maxScrollHeight <= 1 ? 1 : clampToRange(lastScrollY / maxScrollHeight, 0, 1, logger);
56
+ const maxScrollPercentage = maxScrollHeight <= 1 ? 1 : clampToRange(maxScrollY / maxScrollHeight, 0, 1, logger);
57
+ const lastContentPercentage = maxContentHeight <= 1 ? 1 : clampToRange(lastContentY / maxContentHeight, 0, 1, logger);
58
+ const maxContentPercentage = maxContentHeight <= 1 ? 1 : clampToRange(maxContentY / maxContentHeight, 0, 1, logger);
59
+ properties = extend(properties, {
60
+ $prev_pageview_last_scroll: lastScrollY,
61
+ $prev_pageview_last_scroll_percentage: lastScrollPercentage,
62
+ $prev_pageview_max_scroll: maxScrollY,
63
+ $prev_pageview_max_scroll_percentage: maxScrollPercentage,
64
+ $prev_pageview_last_content: lastContentY,
65
+ $prev_pageview_last_content_percentage: lastContentPercentage,
66
+ $prev_pageview_max_content: maxContentY,
67
+ $prev_pageview_max_content_percentage: maxContentPercentage,
68
+ });
69
+ }
70
+ }
71
+ if (previousPageView.pathname) {
72
+ properties.$prev_pageview_pathname = previousPageView.pathname;
73
+ }
74
+ if (previousPageView.timestamp) {
75
+ // Use seconds, for consistency with our other duration-related properties like $duration
76
+ properties.$prev_pageview_duration = (timestamp.getTime() - previousPageView.timestamp.getTime()) / 1000;
77
+ }
78
+ return properties;
79
+ }
80
+ }
81
+ //# sourceMappingURL=page-view.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-view.js","sourceRoot":"","sources":["../src/page-view.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAEhC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAU,MAAM,eAAe,CAAA;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAiB1C,uHAAuH;AACvH,sHAAsH;AACtH,sHAAsH;AACtH,uHAAuH;AACvH,4GAA4G;AAE5G,oHAAoH;AACpH,8GAA8G;AAE9G,MAAM,OAAO,eAAe;IAIxB,YAAY,QAAkB;QAC1B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;IAC7B,CAAC;IAED,UAAU,CAAC,SAAe,EAAE,UAAmB;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,2BAA2B,CAAC,SAAS,EAAE,UAAU,CAAC,CAAA;QAExE,sCAAs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AoB;oBAC3D,yBAAyB,EAAE,UAAU;oBACrC,oCAAoC,EAAE,mBAAmB;oBACzD,2BAA2B,EAAE,YAAY;oBACzC,sCAAsC,EAAE,qBAAqB;oBAC7D,0BAA0B,EAAE,WAAW;oBACvC,qCAAqC,EAAE,oBAAoB;iBAC9D,CAAC,CAAA;YACN,CAAC;QACL,CAAC;QAED,IAAI,gBAAgB,CAAC,QAAQ,EAAE,CAAC;YAC5B,UAAU,CAAC,uBAAuB,GAAG,gBAAgB,CAAC,QAAQ,CAAA;QAClE,CAAC;QACD,IAAI,gBAAgB,CAAC,SAAS,EAAE,CAAC;YAC7B,yFAAyF;YACzF,UAAU,CAAC,uBAAuB,GAAG,CAAC,SAAS,CAAC,OAAO,EAAE,GAAG,gBAAgB,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI,CAAA;QAC5G,CAAC;QAED,OAAO,UAAU,CAAA;IACrB,CAAC;CACJ"}
@@ -0,0 +1,21 @@
1
+ import { Leanbase } from './leanbase';
2
+ export interface ScrollContext {
3
+ maxScrollHeight?: number;
4
+ maxScrollY?: number;
5
+ lastScrollY?: number;
6
+ maxContentHeight?: number;
7
+ maxContentY?: number;
8
+ lastContentY?: number;
9
+ }
10
+ export declare class ScrollManager {
11
+ private _instance;
12
+ private _context;
13
+ constructor(_instance: Leanbase);
14
+ getContext(): ScrollContext | undefined;
15
+ resetContext(): ScrollContext | undefined;
16
+ private _updateScrollData;
17
+ startMeasuringScrollPosition(): void;
18
+ scrollElement(): Element | undefined;
19
+ scrollY(): number;
20
+ scrollX(): number;
21
+ }
@@ -0,0 +1,79 @@
1
+ import { window } from './utils';
2
+ import { addEventListener } from './utils';
3
+ import { isArray } from '@posthog/core';
4
+ // This class is responsible for tracking scroll events and maintaining the scroll context
5
+ export class ScrollManager {
6
+ constructor(_instance) {
7
+ this._instance = _instance;
8
+ this._updateScrollData = () => {
9
+ if (!this._context) {
10
+ this._context = {};
11
+ }
12
+ const el = this.scrollElement();
13
+ const scrollY = this.scrollY();
14
+ const scrollHeight = el ? Math.max(0, el.scrollHeight - el.clientHeight) : 0;
15
+ const contentY = scrollY + (el?.clientHeight || 0);
16
+ const contentHeight = el?.scrollHeight || 0;
17
+ this._context.lastScrollY = Math.ceil(scrollY);
18
+ this._context.maxScrollY = Math.max(scrollY, this._context.maxScrollY ?? 0);
19
+ this._context.maxScrollHeight = Math.max(scrollHeight, this._context.maxScrollHeight ?? 0);
20
+ this._context.lastContentY = contentY;
21
+ this._context.maxContentY = Math.max(contentY, this._context.maxContentY ?? 0);
22
+ this._context.maxContentHeight = Math.max(contentHeight, this._context.maxContentHeight ?? 0);
23
+ };
24
+ }
25
+ getContext() {
26
+ return this._context;
27
+ }
28
+ resetContext() {
29
+ const ctx = this._context;
30
+ // update the scroll properties for the new page, but wait until the next tick
31
+ // of the event loop
32
+ setTimeout(this._updateScrollData, 0);
33
+ return ctx;
34
+ }
35
+ // `capture: true` is required to get scroll events for other scrollable elements
36
+ // on the page, not just the window
37
+ // see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture
38
+ startMeasuringScrollPosition() {
39
+ addEventListener(window, 'scroll', this._updateScrollData, { capture: true });
40
+ addEventListener(window, 'scrollend', this._updateScrollData, { capture: true });
41
+ addEventListener(window, 'resize', this._updateScrollData);
42
+ }
43
+ scrollElement() {
44
+ if (this._instance.config.scroll_root_selector) {
45
+ const selectors = isArray(this._instance.config.scroll_root_selector)
46
+ ? this._instance.config.scroll_root_selector
47
+ : [this._instance.config.scroll_root_selector];
48
+ for (const selector of selectors) {
49
+ const element = window?.document.querySelector(selector);
50
+ if (element) {
51
+ return element;
52
+ }
53
+ }
54
+ return undefined;
55
+ }
56
+ else {
57
+ return window?.document.documentElement;
58
+ }
59
+ }
60
+ scrollY() {
61
+ if (this._instance.config.scroll_root_selector) {
62
+ const element = this.scrollElement();
63
+ return (element && element.scrollTop) || 0;
64
+ }
65
+ else {
66
+ return window ? window.scrollY || window.pageYOffset || window.document.documentElement.scrollTop || 0 : 0;
67
+ }
68
+ }
69
+ scrollX() {
70
+ if (this._instance.config.scroll_root_selector) {
71
+ const element = this.scrollElement();
72
+ return (element && element.scrollLeft) || 0;
73
+ }
74
+ else {
75
+ return window ? window.scrollX || window.pageXOffset || window.document.documentElement.scrollLeft || 0 : 0;
76
+ }
77
+ }
78
+ }
79
+ //# sourceMappingURL=scroll-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scroll-manager.js","sourceRoot":"","sources":["../src/scroll-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAEhC,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAevC,0FAA0F;AAC1F,MAAM,OAAO,aAAa;IAGtB,YAAoB,SAAmB;QAAnB,cAAS,GAAT,SAAS,CAAU;QAgB/B,sBAAiB,GAAG,GAAG,EAAE;YAC7B,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACjB,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAA;YACtB,CAAC;YAED,MAAM,EAAE,GAAG,IAAI,CAAC,aAAa,EAAE,CAAA;YAE/B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;YAC9B,MAAM,YAAY,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,YAAY,GAAG,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;YAC5E,MAAM,QAAQ,GAAG,OAAO,GAAG,CAAC,EAAE,EAAE,YAAY,IAAI,CAAC,CAAC,CAAA;YAClD,MAAM,aAAa,GAAG,EAAE,EAAE,YAAY,IAAI,CAAC,CAAA;YAE3C,IAAI,CAAC,QAAQ,CAAC,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;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EAAE,CAAA;YACpC,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QAC/C,CAAC;aAAM,CAAC;YACJ,OAAO,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,WAAW,IAAI,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC/G,CAAC;IACL,CAAC;CACJ"}
@@ -0,0 +1,32 @@
1
+ import type { SessionIdManager } from './sessionid';
2
+ import type { LeanbasePersistence } from './leanbase-persistence';
3
+ import type { Leanbase } from './leanbase';
4
+ interface LegacySessionSourceProps {
5
+ initialPathName: string;
6
+ referringDomain: string;
7
+ utm_medium?: string;
8
+ utm_source?: string;
9
+ utm_campaign?: string;
10
+ utm_content?: string;
11
+ utm_term?: string;
12
+ }
13
+ interface CurrentSessionSourceProps {
14
+ r: string;
15
+ u: string | undefined;
16
+ }
17
+ interface StoredSessionSourceProps {
18
+ sessionId: string;
19
+ props: LegacySessionSourceProps | CurrentSessionSourceProps;
20
+ }
21
+ export declare class SessionPropsManager {
22
+ private readonly _instance;
23
+ private readonly _sessionIdManager;
24
+ private readonly _persistence;
25
+ private readonly _sessionSourceParamGenerator;
26
+ constructor(instance: Leanbase, sessionIdManager: SessionIdManager, persistence: LeanbasePersistence, sessionSourceParamGenerator?: (instance?: Leanbase) => LegacySessionSourceProps | CurrentSessionSourceProps);
27
+ _getStored(): StoredSessionSourceProps | undefined;
28
+ _onSessionIdCallback: (sessionId: string) => void;
29
+ getSetOnceProps(): Record<string, any>;
30
+ getSessionProps(): Record<string, any>;
31
+ }
32
+ export {};
@@ -0,0 +1,73 @@
1
+ /* Store some session-level attribution-related properties in the persistence layer
2
+ *
3
+ * These have the same lifespan as a session_id, meaning that if the session_id changes, these properties will be reset.
4
+ *
5
+ * We only store the entry URL and referrer, and derive many props (such as utm tags) from those.
6
+ *
7
+ * Given that the cookie is limited to 4K bytes, we don't want to store too much data, so we chose not to store device
8
+ * properties (such as browser, OS, etc) here, as usually getting the current value of those from event properties is
9
+ * sufficient.
10
+ */
11
+ import { getPersonInfo, getPersonPropsFromInfo } from './utils/event-utils';
12
+ import { CLIENT_SESSION_PROPS } from './constants';
13
+ import { each, stripEmptyProperties } from './utils';
14
+ import { stripLeadingDollar } from '@posthog/core';
15
+ const generateSessionSourceParams = (posthog) => {
16
+ return getPersonInfo(posthog?.config.mask_personal_data_properties, posthog?.config.custom_personal_data_properties);
17
+ };
18
+ export class SessionPropsManager {
19
+ constructor(instance, sessionIdManager, persistence, sessionSourceParamGenerator) {
20
+ this._onSessionIdCallback = (sessionId) => {
21
+ const stored = this._getStored();
22
+ if (stored && stored.sessionId === sessionId) {
23
+ return;
24
+ }
25
+ const newProps = {
26
+ sessionId,
27
+ props: this._sessionSourceParamGenerator(this._instance),
28
+ };
29
+ this._persistence.register({ [CLIENT_SESSION_PROPS]: newProps });
30
+ };
31
+ this._instance = instance;
32
+ this._sessionIdManager = sessionIdManager;
33
+ this._persistence = persistence;
34
+ this._sessionSourceParamGenerator = sessionSourceParamGenerator || generateSessionSourceParams;
35
+ this._sessionIdManager.onSessionId(this._onSessionIdCallback);
36
+ }
37
+ _getStored() {
38
+ return this._persistence.props[CLIENT_SESSION_PROPS];
39
+ }
40
+ getSetOnceProps() {
41
+ const p = this._getStored()?.props;
42
+ if (!p) {
43
+ return {};
44
+ }
45
+ if ('r' in p) {
46
+ return getPersonPropsFromInfo(p);
47
+ }
48
+ else {
49
+ return {
50
+ $referring_domain: p.referringDomain,
51
+ $pathname: p.initialPathName,
52
+ utm_source: p.utm_source,
53
+ utm_campaign: p.utm_campaign,
54
+ utm_medium: p.utm_medium,
55
+ utm_content: p.utm_content,
56
+ utm_term: p.utm_term,
57
+ };
58
+ }
59
+ }
60
+ getSessionProps() {
61
+ // it's the same props, but don't include null for unset properties, and add a prefix
62
+ const p = {};
63
+ each(stripEmptyProperties(this.getSetOnceProps()), (v, k) => {
64
+ if (k === '$current_url') {
65
+ // $session_entry_current_url would be a weird name, call it $session_entry_url instead
66
+ k = 'url';
67
+ }
68
+ p[`$session_entry_${stripLeadingDollar(k)}`] = v;
69
+ });
70
+ return p;
71
+ }
72
+ }
73
+ //# sourceMappingURL=session-props.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-props.js","sourceRoot":"","sources":["../src/session-props.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAA;AAG3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAElD,OAAO,EAAE,IAAI,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAsBlD,MAAM,2BAA2B,GAAG,CAAC,OAAkB,EAAwD,EAAE;IAC7G,OAAO,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,6BAA6B,EAAE,OAAO,EAAE,MAAM,CAAC,+BAA+B,CAAC,CAAA;AACxH,CAAC,CAAA;AAED,MAAM,OAAO,mBAAmB;IAQ5B,YACI,QAAkB,EAClB,gBAAkC,EAClC,WAAgC,EAChC,2BAA2G;QAc/G,yBAAoB,GAAG,CAAC,SAAiB,EAAE,EAAE;YACzC,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;YAChC,IAAI,MAAM,IAAI,MAAM,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;gBAC3C,OAAM;YACV,CAAC;YAED,MAAM,QAAQ,GAA6B;gBACvC,SAAS;gBACT,KAAK,EAAE,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,SAAS,CAAC;aAC3D,CAAA;YACD,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,oBAAoB,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAA;QACpE,CAAC,CAAA;Q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