posthog-js 1.311.0 → 1.312.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/array.full.es5.js +1 -1
- package/dist/array.full.es5.js.map +1 -1
- package/dist/array.full.js +1 -1
- package/dist/array.full.js.map +1 -1
- package/dist/array.full.no-external.js +1 -1
- package/dist/array.full.no-external.js.map +1 -1
- package/dist/array.js +1 -1
- package/dist/array.js.map +1 -1
- package/dist/array.no-external.js +1 -1
- package/dist/array.no-external.js.map +1 -1
- package/dist/customizations.full.js +1 -1
- package/dist/lazy-recorder.js +1 -1
- package/dist/main.js +1 -1
- package/dist/main.js.map +1 -1
- package/dist/module.d.ts +4 -0
- package/dist/module.full.d.ts +4 -0
- package/dist/module.full.js +1 -1
- package/dist/module.full.js.map +1 -1
- package/dist/module.full.no-external.d.ts +4 -0
- package/dist/module.full.no-external.js +1 -1
- package/dist/module.full.no-external.js.map +1 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/dist/module.no-external.d.ts +4 -0
- package/dist/module.no-external.js +1 -1
- package/dist/module.no-external.js.map +1 -1
- package/dist/posthog-recorder.js +1 -1
- package/dist/src/page-view.d.ts +4 -0
- package/dist/surveys-preview.d.ts +4 -0
- package/lib/package.json +1 -1
- package/lib/src/page-view.d.ts +4 -0
- package/lib/src/page-view.js +29 -0
- package/lib/src/page-view.js.map +1 -1
- package/lib/src/posthog-core.js +8 -5
- package/lib/src/posthog-core.js.map +1 -1
- package/package.json +1 -1
package/dist/src/page-view.d.ts
CHANGED
|
@@ -20,7 +20,11 @@ export declare class PageViewManager {
|
|
|
20
20
|
pathname: string | undefined;
|
|
21
21
|
};
|
|
22
22
|
_instance: PostHog;
|
|
23
|
+
private _unsubscribeSessionId?;
|
|
23
24
|
constructor(instance: PostHog);
|
|
25
|
+
private _setupSessionRotationHandler;
|
|
26
|
+
private _onSessionIdChange;
|
|
27
|
+
destroy(): void;
|
|
24
28
|
doPageView(timestamp: Date, pageViewId?: string): PageViewEventProperties;
|
|
25
29
|
doPageLeave(timestamp: Date): PageViewEventProperties;
|
|
26
30
|
doEvent(): PageViewEventProperties;
|
|
@@ -2757,7 +2757,11 @@ declare class PageViewManager {
|
|
|
2757
2757
|
pathname: string | undefined;
|
|
2758
2758
|
};
|
|
2759
2759
|
_instance: PostHog;
|
|
2760
|
+
private _unsubscribeSessionId?;
|
|
2760
2761
|
constructor(instance: PostHog);
|
|
2762
|
+
private _setupSessionRotationHandler;
|
|
2763
|
+
private _onSessionIdChange;
|
|
2764
|
+
destroy(): void;
|
|
2761
2765
|
doPageView(timestamp: Date, pageViewId?: string): PageViewEventProperties;
|
|
2762
2766
|
doPageLeave(timestamp: Date): PageViewEventProperties;
|
|
2763
2767
|
doEvent(): PageViewEventProperties;
|
package/lib/package.json
CHANGED
package/lib/src/page-view.d.ts
CHANGED
|
@@ -20,7 +20,11 @@ export declare class PageViewManager {
|
|
|
20
20
|
pathname: string | undefined;
|
|
21
21
|
};
|
|
22
22
|
_instance: PostHog;
|
|
23
|
+
private _unsubscribeSessionId?;
|
|
23
24
|
constructor(instance: PostHog);
|
|
25
|
+
private _setupSessionRotationHandler;
|
|
26
|
+
private _onSessionIdChange;
|
|
27
|
+
destroy(): void;
|
|
24
28
|
doPageView(timestamp: Date, pageViewId?: string): PageViewEventProperties;
|
|
25
29
|
doPageLeave(timestamp: Date): PageViewEventProperties;
|
|
26
30
|
doEvent(): PageViewEventProperties;
|
package/lib/src/page-view.js
CHANGED
|
@@ -14,8 +14,37 @@ var logger_1 = require("./utils/logger");
|
|
|
14
14
|
// event name is $pageview or $pageleave and where $prev_pageview_id matches the original pageview event's id.
|
|
15
15
|
var PageViewManager = /** @class */ (function () {
|
|
16
16
|
function PageViewManager(instance) {
|
|
17
|
+
var _this = this;
|
|
18
|
+
this._onSessionIdChange = function (sessionId, _windowId, changeReason) {
|
|
19
|
+
// Only act on actual session rotations, not initial session creation
|
|
20
|
+
if (!changeReason) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Clear state when session changes for any of these reasons:
|
|
24
|
+
// - noSessionId: after posthog.reset() or forced idle reset
|
|
25
|
+
// - activityTimeout: 30 min idle (default, configurable up to 10 hours)
|
|
26
|
+
// - sessionPastMaximumLength: 24 hour max session
|
|
27
|
+
if (changeReason.noSessionId || changeReason.activityTimeout || changeReason.sessionPastMaximumLength) {
|
|
28
|
+
logger_1.logger.info('[PageViewManager] Session rotated, clearing pageview state', {
|
|
29
|
+
sessionId: sessionId,
|
|
30
|
+
changeReason: changeReason,
|
|
31
|
+
});
|
|
32
|
+
_this._currentPageview = undefined;
|
|
33
|
+
_this._instance.scrollManager.resetContext();
|
|
34
|
+
}
|
|
35
|
+
};
|
|
17
36
|
this._instance = instance;
|
|
37
|
+
this._setupSessionRotationHandler();
|
|
18
38
|
}
|
|
39
|
+
PageViewManager.prototype._setupSessionRotationHandler = function () {
|
|
40
|
+
var _a;
|
|
41
|
+
this._unsubscribeSessionId = (_a = this._instance.sessionManager) === null || _a === void 0 ? void 0 : _a.onSessionId(this._onSessionIdChange);
|
|
42
|
+
};
|
|
43
|
+
PageViewManager.prototype.destroy = function () {
|
|
44
|
+
var _a;
|
|
45
|
+
(_a = this._unsubscribeSessionId) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
46
|
+
this._unsubscribeSessionId = undefined;
|
|
47
|
+
};
|
|
19
48
|
PageViewManager.prototype.doPageView = function (timestamp, pageViewId) {
|
|
20
49
|
var _a;
|
|
21
50
|
var response = this._previousPageViewProperties(timestamp, pageViewId);
|
package/lib/src/page-view.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page-view.js","sourceRoot":"","sources":["../../src/page-view.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAExC,sCAAyD;AACzD,iCAAgC;AAChC,yCAAuC;AAiBvC,uHAAuH;AACvH,sHAAsH;AACtH,sHAAsH;AACtH,uHAAuH;AACvH,4GAA4G;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e,EAAE,CAAC,EAAE,CAAC,EAAE,eAAM,CAAC,CAAA;gBACvF,IAAM,qBAAqB,GACvB,gBAAgB,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,mBAAY,EAAC,YAAY,GAAG,gBAAgB,EAAE,CAAC,EAAE,CAAC,EAAE,eAAM,CAAC,CAAA;gBAC3F,IAAM,oBAAoB,GACtB,gBAAgB,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,mBAAY,EAAC,WAAW,GAAG,gBAAgB,EAAE,CAAC,EAAE,CAAC,EAAE,eAAM,CAAC,CAAA;gBAE1F,UAAU,GAAG,IAAA,cAAM,EAAC,UAAU,EAAE;oBAC5B,0BAA0B,EAAE,WAAW;oBACvC,qCAAqC,EAAE,oBAAoB;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;IACL,sBAAC;AAAD,CAAC,AA7FD,IA6FC;AA7FY,0CAAe","sourcesContent":["import { window } from './utils/globals'\nimport { PostHog } from './posthog-core'\nimport { clampToRange, isUndefined } from '@posthog/core'\nimport { extend } from './utils'\nimport { logger } from './utils/logger'\n\ninterface PageViewEventProperties {\n $pageview_id?: string\n $prev_pageview_id?: string\n $prev_pageview_pathname?: string\n $prev_pageview_duration?: number // seconds\n $prev_pageview_last_scroll?: number\n $prev_pageview_last_scroll_percentage?: number\n $prev_pageview_max_scroll?: number\n $prev_pageview_max_scroll_percentage?: number\n $prev_pageview_last_content?: number\n $prev_pageview_last_content_percentage?: number\n $prev_pageview_max_content?: number\n $prev_pageview_max_content_percentage?: number\n}\n\n// This keeps track of the PageView state (such as the previous PageView's path, timestamp, id, and scroll properties).\n// We store the state in memory, which means that for non-SPA sites, the state will be lost on page reload. This means\n// that non-SPA sites should always send a $pageleave event on any navigation, before the page unloads. For SPA sites,\n// they only need to send a $pageleave event when the user navigates away from the site, as the information is not lost\n// on an internal navigation, and is included as the $prev_pageview_ properties in the next $pageview event.\n\n// Practically, this means that to find the scroll properties for a given pageview, you need to find the event where\n// event name is $pageview or $pageleave and where $prev_pageview_id matches the original pageview event's id.\n\nexport class PageViewManager {\n _currentPageview?: { timestamp: Date; pageViewId: string | undefined; pathname: string | undefined }\n _instance: PostHog\n\n constructor(instance: PostHog) {\n this._instance = instance\n }\n\n doPageView(timestamp: Date, pageViewId?: string): PageViewEventProperties {\n const response = this._previousPageViewProperties(timestamp, pageViewId)\n\n // On a pageview we reset the contexts\n this._currentPageview = { pathname: window?.location.pathname ?? '', pageViewId, timestamp }\n this._instance.scrollManager.resetContext()\n\n return response\n }\n\n doPageLeave(timestamp: Date): PageViewEventProperties {\n return this._previousPageViewProperties(timestamp, this._currentPageview?.pageViewId)\n }\n\n doEvent(): PageViewEventProperties {\n return { $pageview_id: this._currentPageview?.pageViewId }\n }\n\n private _previousPageViewProperties(timestamp: Date, pageviewId: string | undefined): PageViewEventProperties {\n const previousPageView = this._currentPageview\n\n if (!previousPageView) {\n return { $pageview_id: pageviewId }\n }\n\n let properties: PageViewEventProperties = {\n $pageview_id: pageviewId,\n $prev_pageview_id: previousPageView.pageViewId,\n }\n\n const scrollContext = this._instance.scrollManager.getContext()\n\n if (scrollContext && !this._instance.config.disable_scroll_properties) {\n let { maxScrollHeight, lastScrollY, maxScrollY, maxContentHeight, lastContentY, maxContentY } =\n scrollContext\n\n if (\n !isUndefined(maxScrollHeight) &&\n !isUndefined(lastScrollY) &&\n !isUndefined(maxScrollY) &&\n !isUndefined(maxContentHeight) &&\n !isUndefined(lastContentY) &&\n !isUndefined(maxContentY)\n ) {\n // Use ceil, so that e.g. scrolling 999.5px of a 1000px page is considered 100% scrolled\n maxScrollHeight = Math.ceil(maxScrollHeight)\n lastScrollY = Math.ceil(lastScrollY)\n maxScrollY = Math.ceil(maxScrollY)\n maxContentHeight = Math.ceil(maxContentHeight)\n lastContentY = Math.ceil(lastContentY)\n maxContentY = Math.ceil(maxContentY)\n\n // if the maximum scroll height is near 0, then the percentage is 1\n const lastScrollPercentage =\n maxScrollHeight <= 1 ? 1 : clampToRange(lastScrollY / maxScrollHeight, 0, 1, logger)\n const maxScrollPercentage =\n maxScrollHeight <= 1 ? 1 : clampToRange(maxScrollY / maxScrollHeight, 0, 1, logger)\n const lastContentPercentage =\n maxContentHeight <= 1 ? 1 : clampToRange(lastContentY / maxContentHeight, 0, 1, logger)\n const maxContentPercentage =\n maxContentHeight <= 1 ? 1 : clampToRange(maxContentY / maxContentHeight, 0, 1, logger)\n\n properties = extend(properties, {\n $prev_pageview_last_scroll: lastScrollY,\n $prev_pageview_last_scroll_percentage: lastScrollPercentage,\n $prev_pageview_max_scroll: maxScrollY,\n $prev_pageview_max_scroll_percentage: maxScrollPercentage,\n $prev_pageview_last_content: lastContentY,\n $prev_pageview_last_content_percentage: lastContentPercentage,\n $prev_pageview_max_content: maxContentY,\n $prev_pageview_max_content_percentage: maxContentPercentage,\n })\n }\n }\n\n if (previousPageView.pathname) {\n properties.$prev_pageview_pathname = previousPageView.pathname\n }\n if (previousPageView.timestamp) {\n // Use seconds, for consistency with our other duration-related properties like $duration\n properties.$prev_pageview_duration = (timestamp.getTime() - previousPageView.timestamp.getTime()) / 1000\n }\n\n return properties\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"page-view.js","sourceRoot":"","sources":["../../src/page-view.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAExC,sCAAyD;AACzD,iCAAgC;AAChC,yCAAuC;AAkBvC,uHAAuH;AACvH,sHAAsH;AACtH,sHAAsH;AACtH,uHAAuH;AACvH,4GAA4G;AAE5G,oHAAoH;AACpH,8GAA8G;AAE9G;IAKI,yBAAY,QAAiB;QAA7B,iBAGC;QAMO,uBAAkB,GAA6B,UAAC,SAAS,EAAE,SAAS,EAAE,YAAY;YACtF,qEAAqE;YACrE,IAAI,CAAC,YAAY,EAAE,CAAC;gBAChB,OAAM;YACV,CAAC;YAED,6DAA6D;YAC7D,4DAA4D;YAC5D,wEAAwE;YACxE,kDAAkD;YAClD,IAAI,YAAY,CAAC,WAAW,IAAI,YAAY,CAAC,eAAe,IAAI,YAAY,CAAC,wBAAwB,EAAE,CAAC;gBACpG,eAAM,CAAC,IAAI,CAAC,4DAA4D,EAAE;oBACtE,SAAS,WAAA;oBACT,YAAY,cAAA;iBACf,CAAC,CAAA;gBACF,KAAI,CAAC,gBAAgB,GAAG,SAAS,CAAA;gBACjC,KAAI,CAAC,SAAS,CAAC,aAAa,CAAC,YAAY,EAAE,CAAA;YAC/C,CAAC;QACL,CAAC,CAAA;QA1BG,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;QACzB,IAAI,CAAC,4BAA4B,EAAE,CAAA;IACvC,CAAC;IAEO,sDAA4B,GAApC;;QACI,IAAI,CAAC,qBAAqB,GAAG,MAAA,IAAI,CAAC,SAAS,CAAC,cAAc,0CAAE,WAAW,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAA;IACpG,CAAC;IAsBD,iCAAO,GAAP;;QACI,MAAA,IAAI,CAAC,qBAAqB,oDAAI,CAAA;QAC9B,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAA;IAC1C,CAAC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AAC,CAAC,CAAC,CAAC,CAAC,IAAA,mBAAY,EAAC,YAAY,GAAG,gBAAgB,EAAE,CAAC,EAAE,CAAC,EAAE,eAAM,CAAC,CAAA;gBAC3F,IAAM,oBAAoB,GACtB,gBAAgB,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,mBAAY,EAAC,WAAW,GAAG,gBAAgB,EAAE,CAAC,EAAE,CAAC,EAAE,eAAM,CAAC,CAAA;gBAE1F,UAAU,GAAG,IAAA,cAAM,EAAC,UAAU,EAAE;oBAC5B,0BAA0B,EAAE,WAAW;oBACvC,qCAAqC,EAAE,oBAAoB;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;IACL,sBAAC;AAAD,CAAC,AA5HD,IA4HC;AA5HY,0CAAe","sourcesContent":["import { window } from './utils/globals'\nimport { PostHog } from './posthog-core'\nimport { clampToRange, isUndefined } from '@posthog/core'\nimport { extend } from './utils'\nimport { logger } from './utils/logger'\nimport { SessionIdChangedCallback } from './types'\n\ninterface PageViewEventProperties {\n $pageview_id?: string\n $prev_pageview_id?: string\n $prev_pageview_pathname?: string\n $prev_pageview_duration?: number // seconds\n $prev_pageview_last_scroll?: number\n $prev_pageview_last_scroll_percentage?: number\n $prev_pageview_max_scroll?: number\n $prev_pageview_max_scroll_percentage?: number\n $prev_pageview_last_content?: number\n $prev_pageview_last_content_percentage?: number\n $prev_pageview_max_content?: number\n $prev_pageview_max_content_percentage?: number\n}\n\n// This keeps track of the PageView state (such as the previous PageView's path, timestamp, id, and scroll properties).\n// We store the state in memory, which means that for non-SPA sites, the state will be lost on page reload. This means\n// that non-SPA sites should always send a $pageleave event on any navigation, before the page unloads. For SPA sites,\n// they only need to send a $pageleave event when the user navigates away from the site, as the information is not lost\n// on an internal navigation, and is included as the $prev_pageview_ properties in the next $pageview event.\n\n// Practically, this means that to find the scroll properties for a given pageview, you need to find the event where\n// event name is $pageview or $pageleave and where $prev_pageview_id matches the original pageview event's id.\n\nexport class PageViewManager {\n _currentPageview?: { timestamp: Date; pageViewId: string | undefined; pathname: string | undefined }\n _instance: PostHog\n private _unsubscribeSessionId?: () => void\n\n constructor(instance: PostHog) {\n this._instance = instance\n this._setupSessionRotationHandler()\n }\n\n private _setupSessionRotationHandler(): void {\n this._unsubscribeSessionId = this._instance.sessionManager?.onSessionId(this._onSessionIdChange)\n }\n\n private _onSessionIdChange: SessionIdChangedCallback = (sessionId, _windowId, changeReason) => {\n // Only act on actual session rotations, not initial session creation\n if (!changeReason) {\n return\n }\n\n // Clear state when session changes for any of these reasons:\n // - noSessionId: after posthog.reset() or forced idle reset\n // - activityTimeout: 30 min idle (default, configurable up to 10 hours)\n // - sessionPastMaximumLength: 24 hour max session\n if (changeReason.noSessionId || changeReason.activityTimeout || changeReason.sessionPastMaximumLength) {\n logger.info('[PageViewManager] Session rotated, clearing pageview state', {\n sessionId,\n changeReason,\n })\n this._currentPageview = undefined\n this._instance.scrollManager.resetContext()\n }\n }\n\n destroy(): void {\n this._unsubscribeSessionId?.()\n this._unsubscribeSessionId = undefined\n }\n\n doPageView(timestamp: Date, pageViewId?: string): PageViewEventProperties {\n const response = this._previousPageViewProperties(timestamp, pageViewId)\n\n // On a pageview we reset the contexts\n this._currentPageview = { pathname: window?.location.pathname ?? '', pageViewId, timestamp }\n this._instance.scrollManager.resetContext()\n\n return response\n }\n\n doPageLeave(timestamp: Date): PageViewEventProperties {\n return this._previousPageViewProperties(timestamp, this._currentPageview?.pageViewId)\n }\n\n doEvent(): PageViewEventProperties {\n return { $pageview_id: this._currentPageview?.pageViewId }\n }\n\n private _previousPageViewProperties(timestamp: Date, pageviewId: string | undefined): PageViewEventProperties {\n const previousPageView = this._currentPageview\n\n if (!previousPageView) {\n return { $pageview_id: pageviewId }\n }\n\n let properties: PageViewEventProperties = {\n $pageview_id: pageviewId,\n $prev_pageview_id: previousPageView.pageViewId,\n }\n\n const scrollContext = this._instance.scrollManager.getContext()\n\n if (scrollContext && !this._instance.config.disable_scroll_properties) {\n let { maxScrollHeight, lastScrollY, maxScrollY, maxContentHeight, lastContentY, maxContentY } =\n scrollContext\n\n if (\n !isUndefined(maxScrollHeight) &&\n !isUndefined(lastScrollY) &&\n !isUndefined(maxScrollY) &&\n !isUndefined(maxContentHeight) &&\n !isUndefined(lastContentY) &&\n !isUndefined(maxContentY)\n ) {\n // Use ceil, so that e.g. scrolling 999.5px of a 1000px page is considered 100% scrolled\n maxScrollHeight = Math.ceil(maxScrollHeight)\n lastScrollY = Math.ceil(lastScrollY)\n maxScrollY = Math.ceil(maxScrollY)\n maxContentHeight = Math.ceil(maxContentHeight)\n lastContentY = Math.ceil(lastContentY)\n maxContentY = Math.ceil(maxContentY)\n\n // if the maximum scroll height is near 0, then the percentage is 1\n const lastScrollPercentage =\n maxScrollHeight <= 1 ? 1 : clampToRange(lastScrollY / maxScrollHeight, 0, 1, logger)\n const maxScrollPercentage =\n maxScrollHeight <= 1 ? 1 : clampToRange(maxScrollY / maxScrollHeight, 0, 1, logger)\n const lastContentPercentage =\n maxContentHeight <= 1 ? 1 : clampToRange(lastContentY / maxContentHeight, 0, 1, logger)\n const maxContentPercentage =\n maxContentHeight <= 1 ? 1 : clampToRange(maxContentY / maxContentHeight, 0, 1, logger)\n\n properties = extend(properties, {\n $prev_pageview_last_scroll: lastScrollY,\n $prev_pageview_last_scroll_percentage: lastScrollPercentage,\n $prev_pageview_max_scroll: maxScrollY,\n $prev_pageview_max_scroll_percentage: maxScrollPercentage,\n $prev_pageview_last_content: lastContentY,\n $prev_pageview_last_content_percentage: lastContentPercentage,\n $prev_pageview_max_content: maxContentY,\n $prev_pageview_max_content_percentage: maxContentPercentage,\n })\n }\n }\n\n if (previousPageView.pathname) {\n properties.$prev_pageview_pathname = previousPageView.pathname\n }\n if (previousPageView.timestamp) {\n // Use seconds, for consistency with our other duration-related properties like $duration\n properties.$prev_pageview_duration = (timestamp.getTime() - previousPageView.timestamp.getTime()) / 1000\n }\n\n return properties\n }\n}\n"]}
|
package/lib/src/posthog-core.js
CHANGED
|
@@ -2768,7 +2768,7 @@ var PostHog = /** @class */ (function () {
|
|
|
2768
2768
|
* @param {Object} [config.capture_properties] Set of properties to be captured along with the opt-in action
|
|
2769
2769
|
*/
|
|
2770
2770
|
PostHog.prototype.opt_in_capturing = function (options) {
|
|
2771
|
-
var _a, _b, _c;
|
|
2771
|
+
var _a, _b, _c, _d;
|
|
2772
2772
|
if (this.config.cookieless_mode === 'always') {
|
|
2773
2773
|
logger_1.logger.warn('Consent opt in/out is not valid with cookieless_mode="always" and will be ignored');
|
|
2774
2774
|
return;
|
|
@@ -2778,7 +2778,9 @@ var PostHog = /** @class */ (function () {
|
|
|
2778
2778
|
// we need to reset the instance to ensure that there is no leaking of state or data between the cookieless and regular events
|
|
2779
2779
|
this.reset(true);
|
|
2780
2780
|
(_a = this.sessionManager) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
2781
|
+
(_b = this.pageViewManager) === null || _b === void 0 ? void 0 : _b.destroy();
|
|
2781
2782
|
this.sessionManager = new sessionid_1.SessionIdManager(this);
|
|
2783
|
+
this.pageViewManager = new page_view_1.PageViewManager(this);
|
|
2782
2784
|
if (this.persistence) {
|
|
2783
2785
|
this.sessionPropsManager = new session_props_1.SessionPropsManager(this, this.sessionManager, this.persistence);
|
|
2784
2786
|
}
|
|
@@ -2791,14 +2793,14 @@ var PostHog = /** @class */ (function () {
|
|
|
2791
2793
|
this._start_queue_if_opted_in();
|
|
2792
2794
|
// Restart session recording if it should now be enabled
|
|
2793
2795
|
// (this handles the case where opt_out_capturing_by_default or cookieless_mode prevented it from starting)
|
|
2794
|
-
(
|
|
2796
|
+
(_c = this.sessionRecording) === null || _c === void 0 ? void 0 : _c.startIfEnabledOrStop();
|
|
2795
2797
|
// Reinitialize surveys if we're in cookieless mode and just opted in
|
|
2796
2798
|
if (this.config.cookieless_mode == 'on_reject') {
|
|
2797
2799
|
this.surveys.loadIfEnabled();
|
|
2798
2800
|
}
|
|
2799
2801
|
// Don't capture if captureEventName is null or false
|
|
2800
2802
|
if ((0, core_1.isUndefined)(options === null || options === void 0 ? void 0 : options.captureEventName) || (options === null || options === void 0 ? void 0 : options.captureEventName)) {
|
|
2801
|
-
this.capture((
|
|
2803
|
+
this.capture((_d = options === null || options === void 0 ? void 0 : options.captureEventName) !== null && _d !== void 0 ? _d : '$opt_in', options === null || options === void 0 ? void 0 : options.captureProperties, { send_instantly: true });
|
|
2802
2804
|
}
|
|
2803
2805
|
if (this.config.capture_pageview) {
|
|
2804
2806
|
this._captureInitialPageview();
|
|
@@ -2822,7 +2824,7 @@ var PostHog = /** @class */ (function () {
|
|
|
2822
2824
|
* @public
|
|
2823
2825
|
*/
|
|
2824
2826
|
PostHog.prototype.opt_out_capturing = function () {
|
|
2825
|
-
var _a, _b;
|
|
2827
|
+
var _a, _b, _c;
|
|
2826
2828
|
if (this.config.cookieless_mode === 'always') {
|
|
2827
2829
|
logger_1.logger.warn('Consent opt in/out is not valid with cookieless_mode="always" and will be ignored');
|
|
2828
2830
|
return;
|
|
@@ -2840,9 +2842,10 @@ var PostHog = /** @class */ (function () {
|
|
|
2840
2842
|
$device_id: null,
|
|
2841
2843
|
});
|
|
2842
2844
|
(_a = this.sessionManager) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
2845
|
+
(_b = this.pageViewManager) === null || _b === void 0 ? void 0 : _b.destroy();
|
|
2843
2846
|
this.sessionManager = undefined;
|
|
2844
2847
|
this.sessionPropsManager = undefined;
|
|
2845
|
-
(
|
|
2848
|
+
(_c = this.sessionRecording) === null || _c === void 0 ? void 0 : _c.stopRecording();
|
|
2846
2849
|
this.sessionRecording = undefined;
|
|
2847
2850
|
this._captureInitialPageview();
|
|
2848
2851
|
}
|