@grafana/faro-web-sdk 1.2.8 → 1.2.9

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 (84) hide show
  1. package/dist/bundle/faro-web-sdk.iife.js +1 -1
  2. package/dist/bundle/types/index.d.ts +1 -0
  3. package/dist/bundle/types/instrumentations/console/instrumentation.d.ts +1 -1
  4. package/dist/bundle/types/instrumentations/errors/instrumentation.d.ts +1 -1
  5. package/dist/bundle/types/instrumentations/index.d.ts +1 -0
  6. package/dist/bundle/types/instrumentations/session/index.d.ts +2 -1
  7. package/dist/bundle/types/instrumentations/session/instrumentation.d.ts +1 -2
  8. package/dist/bundle/types/instrumentations/session/sessionManager/PersistentSessionsManager.d.ts +1 -2
  9. package/dist/bundle/types/instrumentations/session/sessionManager/VolatileSessionManager.d.ts +1 -2
  10. package/dist/bundle/types/instrumentations/session/sessionManager/index.d.ts +2 -1
  11. package/dist/bundle/types/instrumentations/session/sessionManager/sampling.d.ts +1 -0
  12. package/dist/bundle/types/instrumentations/session/sessionManager/sessionConstants.d.ts +2 -5
  13. package/dist/bundle/types/instrumentations/session/sessionManager/sessionManagerUtils.d.ts +5 -1
  14. package/dist/bundle/types/instrumentations/session/sessionManager/types.d.ts +1 -0
  15. package/dist/bundle/types/instrumentations/view/instrumentation.d.ts +1 -1
  16. package/dist/bundle/types/instrumentations/webVitals/instrumentation.d.ts +1 -1
  17. package/dist/bundle/types/transports/console/transport.d.ts +1 -1
  18. package/dist/bundle/types/transports/fetch/transport.d.ts +1 -1
  19. package/dist/bundle/types/utils/index.d.ts +2 -2
  20. package/dist/cjs/index.js +9 -1
  21. package/dist/cjs/index.js.map +1 -1
  22. package/dist/cjs/instrumentations/index.js +9 -1
  23. package/dist/cjs/instrumentations/index.js.map +1 -1
  24. package/dist/cjs/instrumentations/session/index.js +12 -16
  25. package/dist/cjs/instrumentations/session/index.js.map +1 -1
  26. package/dist/cjs/instrumentations/session/instrumentation.js +43 -30
  27. package/dist/cjs/instrumentations/session/instrumentation.js.map +1 -1
  28. package/dist/cjs/instrumentations/session/sessionManager/PersistentSessionsManager.js +3 -4
  29. package/dist/cjs/instrumentations/session/sessionManager/PersistentSessionsManager.js.map +1 -1
  30. package/dist/cjs/instrumentations/session/sessionManager/VolatileSessionManager.js +3 -4
  31. package/dist/cjs/instrumentations/session/sessionManager/VolatileSessionManager.js.map +1 -1
  32. package/dist/cjs/instrumentations/session/sessionManager/index.js +11 -16
  33. package/dist/cjs/instrumentations/session/sessionManager/index.js.map +1 -1
  34. package/dist/cjs/instrumentations/session/sessionManager/sampling.js +17 -0
  35. package/dist/cjs/instrumentations/session/sessionManager/sampling.js.map +1 -0
  36. package/dist/cjs/instrumentations/session/sessionManager/sessionConstants.js +2 -2
  37. package/dist/cjs/instrumentations/session/sessionManager/sessionConstants.js.map +1 -1
  38. package/dist/cjs/instrumentations/session/sessionManager/sessionManagerUtils.js +8 -8
  39. package/dist/cjs/instrumentations/session/sessionManager/sessionManagerUtils.js.map +1 -1
  40. package/dist/cjs/instrumentations/session/sessionManager/types.js.map +1 -1
  41. package/dist/cjs/utils/index.js +11 -16
  42. package/dist/cjs/utils/index.js.map +1 -1
  43. package/dist/esm/index.js +1 -0
  44. package/dist/esm/index.js.map +1 -1
  45. package/dist/esm/instrumentations/index.js +1 -0
  46. package/dist/esm/instrumentations/index.js.map +1 -1
  47. package/dist/esm/instrumentations/session/index.js +1 -1
  48. package/dist/esm/instrumentations/session/index.js.map +1 -1
  49. package/dist/esm/instrumentations/session/instrumentation.js +34 -23
  50. package/dist/esm/instrumentations/session/instrumentation.js.map +1 -1
  51. package/dist/esm/instrumentations/session/sessionManager/PersistentSessionsManager.js +3 -4
  52. package/dist/esm/instrumentations/session/sessionManager/PersistentSessionsManager.js.map +1 -1
  53. package/dist/esm/instrumentations/session/sessionManager/VolatileSessionManager.js +3 -4
  54. package/dist/esm/instrumentations/session/sessionManager/VolatileSessionManager.js.map +1 -1
  55. package/dist/esm/instrumentations/session/sessionManager/index.js +2 -1
  56. package/dist/esm/instrumentations/session/sessionManager/index.js.map +1 -1
  57. package/dist/esm/instrumentations/session/sessionManager/sampling.js +13 -0
  58. package/dist/esm/instrumentations/session/sessionManager/sampling.js.map +1 -0
  59. package/dist/esm/instrumentations/session/sessionManager/sessionConstants.js +2 -2
  60. package/dist/esm/instrumentations/session/sessionManager/sessionConstants.js.map +1 -1
  61. package/dist/esm/instrumentations/session/sessionManager/sessionManagerUtils.js +7 -8
  62. package/dist/esm/instrumentations/session/sessionManager/sessionManagerUtils.js.map +1 -1
  63. package/dist/esm/instrumentations/session/sessionManager/types.js.map +1 -1
  64. package/dist/esm/utils/index.js +2 -2
  65. package/dist/esm/utils/index.js.map +1 -1
  66. package/dist/types/index.d.ts +1 -0
  67. package/dist/types/instrumentations/console/instrumentation.d.ts +1 -1
  68. package/dist/types/instrumentations/errors/instrumentation.d.ts +1 -1
  69. package/dist/types/instrumentations/index.d.ts +1 -0
  70. package/dist/types/instrumentations/session/index.d.ts +2 -1
  71. package/dist/types/instrumentations/session/instrumentation.d.ts +1 -2
  72. package/dist/types/instrumentations/session/sessionManager/PersistentSessionsManager.d.ts +1 -2
  73. package/dist/types/instrumentations/session/sessionManager/VolatileSessionManager.d.ts +1 -2
  74. package/dist/types/instrumentations/session/sessionManager/index.d.ts +2 -1
  75. package/dist/types/instrumentations/session/sessionManager/sampling.d.ts +1 -0
  76. package/dist/types/instrumentations/session/sessionManager/sessionConstants.d.ts +2 -5
  77. package/dist/types/instrumentations/session/sessionManager/sessionManagerUtils.d.ts +5 -1
  78. package/dist/types/instrumentations/session/sessionManager/types.d.ts +1 -0
  79. package/dist/types/instrumentations/view/instrumentation.d.ts +1 -1
  80. package/dist/types/instrumentations/webVitals/instrumentation.d.ts +1 -1
  81. package/dist/types/transports/console/transport.d.ts +1 -1
  82. package/dist/types/transports/fetch/transport.d.ts +1 -1
  83. package/dist/types/utils/index.d.ts +2 -2
  84. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/instrumentations/session/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE3D,cAAc,kBAAkB,CAAC","sourcesContent":["export { SessionInstrumentation } from './instrumentation';\n\nexport * from './sessionManager';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/instrumentations/session/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAE3D,OAAO,EACL,4BAA4B,EAC5B,mCAAmC,EACnC,yBAAyB,EACzB,uBAAuB,EACvB,uBAAuB,EACvB,WAAW,EACX,oBAAoB,EACpB,uBAAuB,EACvB,4BAA4B,EAC5B,SAAS,GACV,MAAM,kBAAkB,CAAC","sourcesContent":["export { SessionInstrumentation } from './instrumentation';\n\nexport {\n MAX_SESSION_PERSISTENCE_TIME,\n MAX_SESSION_PERSISTENCE_TIME_BUFFER,\n PersistentSessionsManager,\n SESSION_EXPIRATION_TIME,\n SESSION_INACTIVITY_TIME,\n STORAGE_KEY,\n STORAGE_UPDATE_DELAY,\n VolatileSessionsManager,\n defaultSessionTrackingConfig,\n isSampled,\n} from './sessionManager';\n\nexport type { FaroUserSession } from './sessionManager';\n"]}
@@ -1,8 +1,19 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
1
12
  import { BaseInstrumentation, dateNow, EVENT_SESSION_EXTEND, EVENT_SESSION_RESUME, EVENT_SESSION_START, VERSION, } from '@grafana/faro-core';
2
13
  import { createSession } from '../../metas';
3
- import { isLocalStorageAvailable, isSessionStorageAvailable } from '../../utils/webStorage';
14
+ import { isSampled } from './sessionManager';
4
15
  import { PersistentSessionsManager } from './sessionManager/PersistentSessionsManager';
5
- import { isUserSessionValid } from './sessionManager/sessionManagerUtils';
16
+ import { createUserSessionObject, isUserSessionValid } from './sessionManager/sessionManagerUtils';
6
17
  import { VolatileSessionsManager } from './sessionManager/VolatileSessionManager';
7
18
  export class SessionInstrumentation extends BaseInstrumentation {
8
19
  constructor() {
@@ -25,16 +36,6 @@ export class SessionInstrumentation extends BaseInstrumentation {
25
36
  this.api.pushEvent(EVENT_SESSION_START, {}, undefined, { skipDedupe: true });
26
37
  }
27
38
  }
28
- getSessionManagerInstanceByConfiguredStrategy(initialSessionId) {
29
- var _a;
30
- if (((_a = this.config.sessionTracking) === null || _a === void 0 ? void 0 : _a.persistent) && isLocalStorageAvailable) {
31
- return new PersistentSessionsManager(initialSessionId);
32
- }
33
- if (isSessionStorageAvailable) {
34
- return new VolatileSessionsManager(initialSessionId);
35
- }
36
- return null;
37
- }
38
39
  createInitialSessionMeta(sessionsConfig) {
39
40
  var _a, _b, _c;
40
41
  const sessionManager = sessionsConfig.persistent ? PersistentSessionsManager : VolatileSessionsManager;
@@ -51,7 +52,7 @@ export class SessionInstrumentation extends BaseInstrumentation {
51
52
  let sessionAttributes = (_b = sessionsConfig.session) === null || _b === void 0 ? void 0 : _b.attributes;
52
53
  if (isUserSessionValid(userSession)) {
53
54
  sessionId = userSession === null || userSession === void 0 ? void 0 : userSession.sessionId;
54
- sessionAttributes = (_c = userSession === null || userSession === void 0 ? void 0 : userSession.sessionMeta) === null || _c === void 0 ? void 0 : _c.attributes;
55
+ sessionAttributes = Object.assign(Object.assign({}, (_c = userSession === null || userSession === void 0 ? void 0 : userSession.sessionMeta) === null || _c === void 0 ? void 0 : _c.attributes), { isSampled: userSession.isSampled.toString() });
55
56
  this.api.pushEvent(EVENT_SESSION_RESUME, {}, undefined, { skipDedupe: true });
56
57
  }
57
58
  else {
@@ -60,11 +61,8 @@ export class SessionInstrumentation extends BaseInstrumentation {
60
61
  }
61
62
  const sessionMeta = {
62
63
  id: sessionId,
64
+ attributes: Object.assign({ isSampled: isSampled().toString() }, sessionAttributes),
63
65
  };
64
- if (sessionAttributes) {
65
- sessionMeta.attributes = sessionAttributes;
66
- }
67
- this.notifiedSession = sessionMeta;
68
66
  return sessionMeta;
69
67
  }
70
68
  initialize() {
@@ -72,15 +70,28 @@ export class SessionInstrumentation extends BaseInstrumentation {
72
70
  this.logDebug('init session instrumentation');
73
71
  const sessionTracking = this.config.sessionTracking;
74
72
  if (sessionTracking === null || sessionTracking === void 0 ? void 0 : sessionTracking.enabled) {
73
+ const SessionManager = ((_a = this.config.sessionTracking) === null || _a === void 0 ? void 0 : _a.persistent)
74
+ ? PersistentSessionsManager
75
+ : VolatileSessionsManager;
75
76
  const initialSessionMeta = this.createInitialSessionMeta(sessionTracking);
77
+ SessionManager.storeUserSession(createUserSessionObject({
78
+ sessionId: initialSessionMeta.id,
79
+ isSampled: Boolean(initialSessionMeta.attributes['isSampled']),
80
+ }));
81
+ this.notifiedSession = initialSessionMeta;
76
82
  this.api.setSession(initialSessionMeta);
77
- const sessionManager = this.getSessionManagerInstanceByConfiguredStrategy((_a = this.metas.value.session) === null || _a === void 0 ? void 0 : _a.id);
78
- if (sessionManager != null) {
79
- (_b = this.transports) === null || _b === void 0 ? void 0 : _b.addBeforeSendHooks(...this.transports.getBeforeSendHooks(), (item) => {
80
- sessionManager === null || sessionManager === void 0 ? void 0 : sessionManager.updateSession();
83
+ const { updateSession } = new SessionManager();
84
+ (_b = this.transports) === null || _b === void 0 ? void 0 : _b.addBeforeSendHooks((item) => {
85
+ var _a;
86
+ updateSession();
87
+ const attributes = (_a = item.meta.session) === null || _a === void 0 ? void 0 : _a.attributes;
88
+ if (attributes && Boolean(attributes === null || attributes === void 0 ? void 0 : attributes['isSampled'])) {
89
+ const { isSampled: _ } = attributes, restAttributes = __rest(attributes, ["isSampled"]);
90
+ item.meta.session = Object.assign(Object.assign({}, item.meta.session), { attributes: restAttributes });
81
91
  return item;
82
- });
83
- }
92
+ }
93
+ return null;
94
+ });
84
95
  }
85
96
  else {
86
97
  this.sendSessionStartEvent(this.metas.value);
@@ -1 +1 @@
1
- {"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../../../src/instrumentations/session/instrumentation.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,OAAO,EACP,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EAGnB,OAAO,GACR,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AAG5F,OAAO,EAAE,yBAAyB,EAAE,MAAM,4CAA4C,CAAC;AACvF,OAAO,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAElF,MAAM,OAAO,sBAAuB,SAAQ,mBAAmB;IAA/D;;QACW,SAAI,GAAG,+CAA+C,CAAC;QACvD,YAAO,GAAG,OAAO,CAAC;IAoG7B,CAAC;IA9FS,qBAAqB,CAAC,IAAU;;QACtC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAE7B,IAAI,OAAO,IAAI,OAAO,CAAC,EAAE,MAAK,MAAA,IAAI,CAAC,eAAe,0CAAE,EAAE,CAAA,EAAE;YACtD,IAAI,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC,eAAe,CAAC,EAAE,MAAK,MAAA,OAAO,CAAC,UAAU,0CAAG,iBAAiB,CAAC,CAAA,EAAE;gBAC/F,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,oBAAoB,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,UAAU,E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YAExG,IAAI,cAAc,IAAI,IAAI,EAAE;gBAC1B,MAAA,IAAI,CAAC,UAAU,0CAAE,kBAAkB,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,kBAAkB,EAAE,EAAE,CAAC,IAAS,EAAE,EAAE;oBACzF,cAAc,aAAd,cAAc,uBAAd,cAAc,CAAE,aAAa,EAAE,CAAC;oBAChC,OAAO,IAAI,CAAC;gBACd,CAAC,CAAC,CAAC;aACJ;SACF;aAAM;YACL,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC9C;QAED,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAChE,CAAC;CACF","sourcesContent":["import {\n BaseInstrumentation,\n dateNow,\n EVENT_SESSION_EXTEND,\n EVENT_SESSION_RESUME,\n EVENT_SESSION_START,\n Meta,\n MetaSession,\n VERSION,\n} from '@grafana/faro-core';\nimport type { Config } from '@grafana/faro-core';\n\nimport { createSession } from '../../metas';\nimport { isLocalStorageAvailable, isSessionStorageAvailable } from '../../utils/webStorage';\n\nimport type { FaroUserSession } from './sessionManager';\nimport { PersistentSessionsManager } from './sessionManager/PersistentSessionsManager';\nimport { isUserSessionValid } from './sessionManager/sessionManagerUtils';\nimport { VolatileSessionsManager } from './sessionManager/VolatileSessionManager';\n\nexport class SessionInstrumentation extends BaseInstrumentation {\n readonly name = '@grafana/faro-web-sdk:instrumentation-session';\n readonly version = VERSION;\n\n // previously notified session, to ensure we don't send session start\n // event twice for the same session\n private notifiedSession: MetaSession | undefined;\n\n private sendSessionStartEvent(meta: Meta): void {\n const session = meta.session;\n\n if (session && session.id !== this.notifiedSession?.id) {\n if (this.notifiedSession && this.notifiedSession.id === session.attributes?.['previousSession']) {\n this.api.pushEvent(EVENT_SESSION_EXTEND, {}, undefined, { skipDedupe: true });\n this.notifiedSession = session;\n return;\n }\n\n this.notifiedSession = session;\n // no need to add attributes and session id, they are included as part of meta\n // automatically\n this.api.pushEvent(EVENT_SESSION_START, {}, undefined, { skipDedupe: true });\n }\n }\n\n private getSessionManagerInstanceByConfiguredStrategy(\n initialSessionId?: string\n ): PersistentSessionsManager | VolatileSessionsManager | null {\n if (this.config.sessionTracking?.persistent && isLocalStorageAvailable) {\n return new PersistentSessionsManager(initialSessionId);\n }\n\n if (isSessionStorageAvailable) {\n return new VolatileSessionsManager(initialSessionId);\n }\n\n return null;\n }\n\n private createInitialSessionMeta(sessionsConfig: Required<Config>['sessionTracking']): MetaSession {\n const sessionManager = sessionsConfig.persistent ? PersistentSessionsManager : VolatileSessionsManager;\n\n let userSession: FaroUserSession | null = sessionManager.fetchUserSession();\n\n if (sessionsConfig.persistent && sessionsConfig.maxSessionPersistenceTime && userSession) {\n const now = dateNow();\n const shouldClearPersistentSession = userSession.lastActivity < now - sessionsConfig.maxSessionPersistenceTime;\n\n if (shouldClearPersistentSession) {\n PersistentSessionsManager.removeUserSession();\n userSession = null;\n }\n }\n\n let sessionId = sessionsConfig.session?.id;\n let sessionAttributes = sessionsConfig.session?.attributes;\n\n if (isUserSessionValid(userSession)) {\n sessionId = userSession?.sessionId;\n sessionAttributes = userSession?.sessionMeta?.attributes;\n this.api.pushEvent(EVENT_SESSION_RESUME, {}, undefined, { skipDedupe: true });\n } else {\n sessionId = sessionId ?? createSession().id;\n this.api.pushEvent(EVENT_SESSION_START, {}, undefined, { skipDedupe: true });\n }\n\n const sessionMeta: MetaSession = {\n id: sessionId,\n };\n\n if (sessionAttributes) {\n sessionMeta.attributes = sessionAttributes;\n }\n\n this.notifiedSession = sessionMeta;\n\n return sessionMeta;\n }\n\n initialize() {\n this.logDebug('init session instrumentation');\n\n const sessionTracking = this.config.sessionTracking;\n\n if (sessionTracking?.enabled) {\n const initialSessionMeta = this.createInitialSessionMeta(sessionTracking);\n this.api.setSession(initialSessionMeta);\n\n const sessionManager = this.getSessionManagerInstanceByConfiguredStrategy(this.metas.value.session?.id);\n\n if (sessionManager != null) {\n this.transports?.addBeforeSendHooks(...this.transports.getBeforeSendHooks(), (item: any) => {\n sessionManager?.updateSession();\n return item;\n });\n }\n } else {\n this.sendSessionStartEvent(this.metas.value);\n }\n\n this.metas.addListener(this.sendSessionStartEvent.bind(this));\n }\n}\n"]}
1
+ {"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../../../src/instrumentations/session/instrumentation.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EACL,mBAAmB,EACnB,OAAO,EACP,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EAGnB,OAAO,GACR,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAwB,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EAAE,yBAAyB,EAAE,MAAM,4CAA4C,CAAC;AACvF,OAAO,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAC;AACnG,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAElF,MAAM,OAAO,sBAAuB,SAAQ,mBAAmB;IAA/D;;QACW,SAAI,GAAG,+CAA+C,CAAC;QACvD,YAAO,GAAG,OAAO,CAAC;IAkH7B,CAAC;IA5GS,qBAAqB,CAAC,IAAU;;QACtC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;QAE7B,IAAI,OAAO,IAAI,OAAO,CAAC,EAAE,MAAK,MAAA,IAAI,CAAC,eAAe,0CAAE,EAAE,CAAA,EAAE;YACtD,IAAI,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC,eAAe,CAAC,EAAE,MAAK,MAAA,OAAO,CAAC,UAAU,0CAAG,iBAAiB,CAAC,CAAA,EAAE;gBAC/F,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,oBAAoB,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,UAAU,EAAE,IAAI,E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AG,IAAI,cAAc,EAAE,CAAC;YAE/C,MAAA,IAAI,CAAC,UAAU,0CAAE,kBAAkB,CAAC,CAAC,IAAI,EAAE,EAAE;;gBAC3C,aAAa,EAAE,CAAC;gBAEhB,MAAM,UAAU,GAAG,MAAA,IAAI,CAAC,IAAI,CAAC,OAAO,0CAAE,UAAU,CAAC;gBAEjD,IAAI,UAAU,IAAI,OAAO,CAAC,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAG,WAAW,CAAC,CAAC,EAAE;oBACpD,MAAM,EAAE,SAAS,EAAE,CAAC,KAAwB,UAAU,EAA7B,cAAc,UAAK,UAAU,EAAhD,aAAmC,CAAa,CAAC;oBAEvD,IAAI,CAAC,IAAI,CAAC,OAAO,mCACZ,IAAI,CAAC,IAAI,CAAC,OAAO,KACpB,UAAU,EAAE,cAAc,GAC3B,CAAC;oBAEF,OAAO,IAAI,CAAC;iBACb;gBAED,OAAO,IAAI,CAAC;YACd,CAAC,CAAC,CAAC;SACJ;aAAM;YACL,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;SAC9C;QAED,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAChE,CAAC;CACF","sourcesContent":["import {\n BaseInstrumentation,\n dateNow,\n EVENT_SESSION_EXTEND,\n EVENT_SESSION_RESUME,\n EVENT_SESSION_START,\n Meta,\n MetaSession,\n VERSION,\n} from '@grafana/faro-core';\nimport type { Config } from '@grafana/faro-core';\n\nimport { createSession } from '../../metas';\n\nimport { type FaroUserSession, isSampled } from './sessionManager';\nimport { PersistentSessionsManager } from './sessionManager/PersistentSessionsManager';\nimport { createUserSessionObject, isUserSessionValid } from './sessionManager/sessionManagerUtils';\nimport { VolatileSessionsManager } from './sessionManager/VolatileSessionManager';\n\nexport class SessionInstrumentation extends BaseInstrumentation {\n readonly name = '@grafana/faro-web-sdk:instrumentation-session';\n readonly version = VERSION;\n\n // previously notified session, to ensure we don't send session start\n // event twice for the same session\n private notifiedSession: MetaSession | undefined;\n\n private sendSessionStartEvent(meta: Meta): void {\n const session = meta.session;\n\n if (session && session.id !== this.notifiedSession?.id) {\n if (this.notifiedSession && this.notifiedSession.id === session.attributes?.['previousSession']) {\n this.api.pushEvent(EVENT_SESSION_EXTEND, {}, undefined, { skipDedupe: true });\n this.notifiedSession = session;\n return;\n }\n\n this.notifiedSession = session;\n // no need to add attributes and session id, they are included as part of meta\n // automatically\n this.api.pushEvent(EVENT_SESSION_START, {}, undefined, { skipDedupe: true });\n }\n }\n\n private createInitialSessionMeta(sessionsConfig: Required<Config>['sessionTracking']): MetaSession {\n const sessionManager = sessionsConfig.persistent ? PersistentSessionsManager : VolatileSessionsManager;\n\n let userSession: FaroUserSession | null = sessionManager.fetchUserSession();\n\n if (sessionsConfig.persistent && sessionsConfig.maxSessionPersistenceTime && userSession) {\n const now = dateNow();\n const shouldClearPersistentSession = userSession.lastActivity < now - sessionsConfig.maxSessionPersistenceTime;\n\n if (shouldClearPersistentSession) {\n PersistentSessionsManager.removeUserSession();\n userSession = null;\n }\n }\n\n let sessionId = sessionsConfig.session?.id;\n let sessionAttributes = sessionsConfig.session?.attributes;\n\n if (isUserSessionValid(userSession)) {\n sessionId = userSession?.sessionId;\n sessionAttributes = {\n ...userSession?.sessionMeta?.attributes,\n isSampled: userSession!.isSampled.toString(),\n };\n this.api.pushEvent(EVENT_SESSION_RESUME, {}, undefined, { skipDedupe: true });\n } else {\n sessionId = sessionId ?? createSession().id;\n this.api.pushEvent(EVENT_SESSION_START, {}, undefined, { skipDedupe: true });\n }\n\n const sessionMeta: MetaSession = {\n id: sessionId,\n attributes: {\n isSampled: isSampled().toString(),\n // We do not want to recalculate the sampling decision on each init phase.\n // If session from web-storage has a isSampled attribute we will use that instead.\n ...sessionAttributes,\n },\n };\n\n return sessionMeta;\n }\n\n initialize() {\n this.logDebug('init session instrumentation');\n\n const sessionTracking = this.config.sessionTracking;\n\n if (sessionTracking?.enabled) {\n const SessionManager = this.config.sessionTracking?.persistent\n ? PersistentSessionsManager\n : VolatileSessionsManager;\n\n const initialSessionMeta = this.createInitialSessionMeta(sessionTracking);\n\n SessionManager.storeUserSession(\n createUserSessionObject({\n sessionId: initialSessionMeta.id,\n isSampled: Boolean(initialSessionMeta.attributes!['isSampled']),\n })\n );\n\n this.notifiedSession = initialSessionMeta;\n this.api.setSession(initialSessionMeta);\n\n const { updateSession } = new SessionManager();\n\n this.transports?.addBeforeSendHooks((item) => {\n updateSession();\n\n const attributes = item.meta.session?.attributes;\n\n if (attributes && Boolean(attributes?.['isSampled'])) {\n const { isSampled: _, ...restAttributes } = attributes;\n\n item.meta.session = {\n ...item.meta.session,\n attributes: restAttributes,\n };\n\n return item;\n }\n\n return null;\n });\n } else {\n this.sendSessionStartEvent(this.metas.value);\n }\n\n this.metas.addListener(this.sendSessionStartEvent.bind(this));\n }\n}\n"]}
@@ -1,11 +1,11 @@
1
1
  import { faro } from '@grafana/faro-core';
2
2
  import { throttle } from '../../../utils';
3
3
  import { getItem, removeItem, setItem, webStorageType } from '../../../utils/webStorage';
4
+ import { isSampled } from './sampling';
4
5
  import { STORAGE_KEY, STORAGE_UPDATE_DELAY } from './sessionConstants';
5
6
  import { addSessionMetadataToNextSession, createUserSessionObject, getUserSessionUpdater } from './sessionManagerUtils';
6
7
  export class PersistentSessionsManager {
7
- constructor(initialSessionId) {
8
- this.initialSessionId = initialSessionId;
8
+ constructor() {
9
9
  this.updateSession = throttle(() => this.updateUserSession(), STORAGE_UPDATE_DELAY);
10
10
  this.updateUserSession = getUserSessionUpdater({
11
11
  fetchUserSession: PersistentSessionsManager.fetchUserSession,
@@ -27,7 +27,6 @@ export class PersistentSessionsManager {
27
27
  return null;
28
28
  }
29
29
  init() {
30
- PersistentSessionsManager.storeUserSession(createUserSessionObject(this.initialSessionId));
31
30
  document.addEventListener('visibilitychange', () => {
32
31
  if (document.visibilityState === 'visible') {
33
32
  this.updateSession();
@@ -49,7 +48,7 @@ export class PersistentSessionsManager {
49
48
  const session = meta.session;
50
49
  const sessionFromLocalStorage = PersistentSessionsManager.fetchUserSession();
51
50
  if (session && session.id !== (sessionFromLocalStorage === null || sessionFromLocalStorage === void 0 ? void 0 : sessionFromLocalStorage.sessionId)) {
52
- const userSession = addSessionMetadataToNextSession(createUserSessionObject(session.id), sessionFromLocalStorage);
51
+ const userSession = addSessionMetadataToNextSession(createUserSessionObject({ sessionId: session.id, isSampled: isSampled() }), sessionFromLocalStorage);
53
52
  PersistentSessionsManager.storeUserSession(userSession);
54
53
  faro.api.setSession(userSession.sessionMeta);
55
54
  }
@@ -1 +1 @@
1
- {"version":3,"file":"PersistentSessionsManager.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/PersistentSessionsManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAG1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEzF,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AACvE,OAAO,EAAE,+BAA+B,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAGxH,MAAM,OAAO,yBAAyB;IAIpC,YAAoB,gBAAyB;QAAzB,qBAAgB,GAAhB,gBAAgB,CAAS;QA2B7C,kBAAa,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,EAAE,EAAE,oBAAoB,CAAC,CAAC;QA1B7E,IAAI,CAAC,iBAAiB,GAAG,qBAAqB,CAAC;YAC7C,gBAAgB,EAAE,yBAAyB,CAAC,gBAAgB;YAC5D,gBAAgB,EAAE,yBAAyB,CAAC,gBAAgB;SAC7D,CAAC,CAAC;QAE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C,GAAG,0CAAE,UAAU,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;aAC9C;QACH,CAAC,CAAC,CAAC;QAEH,iGAAiG;QACjG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,8BAA8B,CAAC,IAAU;YACvE,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;YAC7B,MAAM,uBAAuB,GAAG,yBAAyB,CAAC,gBAAgB,EAAE,CAAC;YAE7E,IAAI,OAAO,IAAI,OAAO,CAAC,EAAE,MAAK,uBAAuB,aAAvB,uBAAuB,uBAAvB,uBAAuB,CAAE,SAAS,CAAA,EAAE;gBAChE,MAAM,WAAW,GAAG,+BAA+B,CACjD,uBAAuB,CAAC,OAAO,CAAC,EAAE,CAAC,EACnC,uBAAuB,CACxB,CAAC;gBAEF,yBAAyB,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;gBACxD,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;aAC9C;QACH,CAAC,CAAC,CAAC;IACL,CAAC;;AArEc,0CAAgB,GAAG,cAAc,CAAC,KAAK,CAAC","sourcesContent":["import { faro } from '@grafana/faro-core';\nimport type { Meta } from '@grafana/faro-core';\n\nimport { throttle } from '../../../utils';\nimport { getItem, removeItem, setItem, webStorageType } from '../../../utils/webStorage';\n\nimport { STORAGE_KEY, STORAGE_UPDATE_DELAY } from './sessionConstants';\nimport { addSessionMetadataToNextSession, createUserSessionObject, getUserSessionUpdater } from './sessionManagerUtils';\nimport type { FaroUserSession } from './types';\n\nexport class PersistentSessionsManager {\n private static storageTypeLocal = webStorageType.local;\n private updateUserSession: ReturnType<typeof getUserSessionUpdater>;\n\n constructor(private initialSessionId?: string) {\n this.updateUserSession = getUserSessionUpdater({\n fetchUserSession: PersistentSessionsManager.fetchUserSession,\n storeUserSession: PersistentSessionsManager.storeUserSession,\n });\n\n this.init();\n }\n\n static removeUserSession() {\n removeItem(STORAGE_KEY, PersistentSessionsManager.storageTypeLocal);\n }\n\n static storeUserSession(session: FaroUserSession): void {\n setItem(STORAGE_KEY, JSON.stringify(session), PersistentSessionsManager.storageTypeLocal);\n }\n\n static fetchUserSession(): FaroUserSession | null {\n const storedSession = getItem(STORAGE_KEY, PersistentSessionsManager.storageTypeLocal);\n\n if (storedSession) {\n return JSON.parse(storedSession) as FaroUserSession;\n }\n\n return null;\n }\n\n updateSession = throttle(() => this.updateUserSession(), STORAGE_UPDATE_DELAY);\n\n private init(): void {\n PersistentSessionsManager.storeUserSession(createUserSessionObject(this.initialSessionId));\n\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n this.updateSession();\n }\n });\n\n window.addEventListener('storage', (event: StorageEvent) => {\n if (event.key !== STORAGE_KEY) {\n return;\n }\n\n const newSession: FaroUserSession = JSON.parse(event.newValue ?? '');\n const previousSession: FaroUserSession = JSON.parse(event.oldValue ?? '');\n\n if (newSession.sessionId !== previousSession.sessionId) {\n faro.api?.setSession(newSession.sessionMeta);\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the local storage session\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n const sessionFromLocalStorage = PersistentSessionsManager.fetchUserSession();\n\n if (session && session.id !== sessionFromLocalStorage?.sessionId) {\n const userSession = addSessionMetadataToNextSession(\n createUserSessionObject(session.id),\n sessionFromLocalStorage\n );\n\n PersistentSessionsManager.storeUserSession(userSession);\n faro.api.setSession(userSession.sessionMeta);\n }\n });\n }\n}\n"]}
1
+ {"version":3,"file":"PersistentSessionsManager.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/PersistentSessionsManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAG1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,2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ta } from '@grafana/faro-core';\n\nimport { throttle } from '../../../utils';\nimport { getItem, removeItem, setItem, webStorageType } from '../../../utils/webStorage';\n\nimport { isSampled } from './sampling';\nimport { STORAGE_KEY, STORAGE_UPDATE_DELAY } from './sessionConstants';\nimport { addSessionMetadataToNextSession, createUserSessionObject, getUserSessionUpdater } from './sessionManagerUtils';\nimport type { FaroUserSession } from './types';\n\nexport class PersistentSessionsManager {\n private static storageTypeLocal = webStorageType.local;\n private updateUserSession: ReturnType<typeof getUserSessionUpdater>;\n\n constructor() {\n this.updateUserSession = getUserSessionUpdater({\n fetchUserSession: PersistentSessionsManager.fetchUserSession,\n storeUserSession: PersistentSessionsManager.storeUserSession,\n });\n\n this.init();\n }\n\n static removeUserSession() {\n removeItem(STORAGE_KEY, PersistentSessionsManager.storageTypeLocal);\n }\n\n static storeUserSession(session: FaroUserSession): void {\n setItem(STORAGE_KEY, JSON.stringify(session), PersistentSessionsManager.storageTypeLocal);\n }\n\n static fetchUserSession(): FaroUserSession | null {\n const storedSession = getItem(STORAGE_KEY, PersistentSessionsManager.storageTypeLocal);\n\n if (storedSession) {\n return JSON.parse(storedSession) as FaroUserSession;\n }\n\n return null;\n }\n\n updateSession = throttle(() => this.updateUserSession(), STORAGE_UPDATE_DELAY);\n\n private init(): void {\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n this.updateSession();\n }\n });\n\n window.addEventListener('storage', (event: StorageEvent) => {\n if (event.key !== STORAGE_KEY) {\n return;\n }\n\n const newSession: FaroUserSession = JSON.parse(event.newValue ?? '');\n const previousSession: FaroUserSession = JSON.parse(event.oldValue ?? '');\n\n if (newSession.sessionId !== previousSession.sessionId) {\n faro.api?.setSession(newSession.sessionMeta);\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the local storage session\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n const sessionFromLocalStorage = PersistentSessionsManager.fetchUserSession();\n\n if (session && session.id !== sessionFromLocalStorage?.sessionId) {\n const userSession = addSessionMetadataToNextSession(\n createUserSessionObject({ sessionId: session.id, isSampled: isSampled() }),\n sessionFromLocalStorage\n );\n\n PersistentSessionsManager.storeUserSession(userSession);\n faro.api.setSession(userSession.sessionMeta);\n }\n });\n }\n}\n"]}
@@ -1,11 +1,11 @@
1
1
  import { faro } from '@grafana/faro-core';
2
2
  import { throttle } from '../../../utils';
3
3
  import { getItem, removeItem, setItem, webStorageType } from '../../../utils/webStorage';
4
+ import { isSampled } from './sampling';
4
5
  import { STORAGE_KEY, STORAGE_UPDATE_DELAY } from './sessionConstants';
5
6
  import { addSessionMetadataToNextSession, createUserSessionObject, getUserSessionUpdater } from './sessionManagerUtils';
6
7
  export class VolatileSessionsManager {
7
- constructor(initialSessionId) {
8
- this.initialSessionId = initialSessionId;
8
+ constructor() {
9
9
  this.updateSession = throttle(() => this.updateUserSession(), STORAGE_UPDATE_DELAY);
10
10
  this.updateUserSession = getUserSessionUpdater({
11
11
  fetchUserSession: VolatileSessionsManager.fetchUserSession,
@@ -27,7 +27,6 @@ export class VolatileSessionsManager {
27
27
  return null;
28
28
  }
29
29
  init() {
30
- VolatileSessionsManager.storeUserSession(createUserSessionObject(this.initialSessionId));
31
30
  document.addEventListener('visibilitychange', () => {
32
31
  if (document.visibilityState === 'visible') {
33
32
  this.updateSession();
@@ -38,7 +37,7 @@ export class VolatileSessionsManager {
38
37
  const session = meta.session;
39
38
  const sessionFromSessionStorage = VolatileSessionsManager.fetchUserSession();
40
39
  if (session && session.id !== (sessionFromSessionStorage === null || sessionFromSessionStorage === void 0 ? void 0 : sessionFromSessionStorage.sessionId)) {
41
- const userSession = addSessionMetadataToNextSession(createUserSessionObject(session.id), sessionFromSessionStorage);
40
+ const userSession = addSessionMetadataToNextSession(createUserSessionObject({ sessionId: session.id, isSampled: isSampled() }), sessionFromSessionStorage);
42
41
  VolatileSessionsManager.storeUserSession(userSession);
43
42
  faro.api.setSession(userSession.sessionMeta);
44
43
  }
@@ -1 +1 @@
1
- {"version":3,"file":"VolatileSessionManager.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/VolatileSessionManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAG1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEzF,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AACvE,OAAO,EAAE,+BAA+B,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAGxH,MAAM,OAAO,uBAAuB;IAIlC,YAAoB,gBAAyB;QAAzB,qBAAgB,GAAhB,gBAAgB,CAAS;QA2B7C,kBAAa,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,EAAE,EAAE,oBAAoB,CAAC,CAAC;QA1B7E,IAAI,CAAC,iBAAiB,GAAG,qBAAqB,CAAC;YAC7C,gBAAgB,EAAE,uBAAuB,CAAC,gBAAgB;YAC1D,gBAAgB,EAAE,uBAAuB,CAAC,gBAAgB;SAC3D,CAAC,CAAC;QAEH,IAAI,CAAC,IAAI,EAAE,CAAC;IACd,CAAC;IAED,MAAM,CAAC,iBAAiB;QACtB,UAAU,CAAC,WAAW,EAAE,uBAAuB,CAAC,kBAAkB,CAAC,CAAC;IACtE,CAAC;IAED,MAAM,CAAC,gBAAgB,CAAC,OAAwB;QAC9C,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,uBAAuB,CAAC,kBAAkB,CAAC,CAAC;IAC5F,CAAC;IAED,MAAM,CAAC,gBAAgB;QACrB,MAAM,aAAa,GAAG,OAAO,CAAC,WAAW,EAAE,uBAAuB,CAAC,kBAAkB,CAAC,CAAC;QAEvF,IAAI,aAAa,EAAE;YACjB,OAAO,IAAI,CAAC,KAAK,CAAC,aAAa,CAAoB,CAAC;SACrD;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAIO,IAAI;QACV,uBAAuB,CAAC,gBAAgB,CAAC,uBAAuB,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC;QAEzF,QAAQ,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,GAAG,EAAE;YACjD,IAAI,QAAQ,CAAC,eAAe,KAAK,SAAS,EAAE;gBAC1C,IAAI,CAAC,aAAa,EAAE,CAAC;aACtB;QACH,CAAC,CAAC,CAAC;QAEH,iGAAiG;QACjG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,8BAA8B,CAAC,IAAU;YACvE,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;YAC7B,MAAM,yBAAyB,GAAG,uBAAuB,CAAC,gBAAgB,EAAE,CAAC;YAE7E,IAAI,OAAO,IAAI,OAAO,CAAC,EAAE,MAAK,yBAAyB,aAAzB,yBAAyB,uBAAzB,yBAAyB,CAAE,SAAS,CAAA,EAAE;gBAClE,MAAM,WAAW,GAAG,+BAA+B,CACjD,uBAAuB,CAAC,OAAO,CAAC,EAAE,CAAC,EACnC,yBAAyB,CAC1B,CAAC;gBAEF,uBAAuB,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;gBACtD,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;aAC9C;QACH,CAAC,CAAC,CAAC;IACL,CAAC;;AAxDc,0CAAkB,GAAG,cAAc,CAAC,OAAO,CAAC","sourcesContent":["import { faro } from '@grafana/faro-core';\nimport type { Meta } from '@grafana/faro-core';\n\nimport { throttle } from '../../../utils';\nimport { getItem, removeItem, setItem, webStorageType } from '../../../utils/webStorage';\n\nimport { STORAGE_KEY, STORAGE_UPDATE_DELAY } from './sessionConstants';\nimport { addSessionMetadataToNextSession, createUserSessionObject, getUserSessionUpdater } from './sessionManagerUtils';\nimport type { FaroUserSession } from './types';\n\nexport class VolatileSessionsManager {\n private static storageTypeSession = webStorageType.session;\n private updateUserSession: ReturnType<typeof getUserSessionUpdater>;\n\n constructor(private initialSessionId?: string) {\n this.updateUserSession = getUserSessionUpdater({\n fetchUserSession: VolatileSessionsManager.fetchUserSession,\n storeUserSession: VolatileSessionsManager.storeUserSession,\n });\n\n this.init();\n }\n\n static removeUserSession() {\n removeItem(STORAGE_KEY, VolatileSessionsManager.storageTypeSession);\n }\n\n static storeUserSession(session: FaroUserSession): void {\n setItem(STORAGE_KEY, JSON.stringify(session), VolatileSessionsManager.storageTypeSession);\n }\n\n static fetchUserSession(): FaroUserSession | null {\n const storedSession = getItem(STORAGE_KEY, VolatileSessionsManager.storageTypeSession);\n\n if (storedSession) {\n return JSON.parse(storedSession) as FaroUserSession;\n }\n\n return null;\n }\n\n updateSession = throttle(() => this.updateUserSession(), STORAGE_UPDATE_DELAY);\n\n private init(): void {\n VolatileSessionsManager.storeUserSession(createUserSessionObject(this.initialSessionId));\n\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n this.updateSession();\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the local storage session\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n const sessionFromSessionStorage = VolatileSessionsManager.fetchUserSession();\n\n if (session && session.id !== sessionFromSessionStorage?.sessionId) {\n const userSession = addSessionMetadataToNextSession(\n createUserSessionObject(session.id),\n sessionFromSessionStorage\n );\n\n VolatileSessionsManager.storeUserSession(userSession);\n faro.api.setSession(userSession.sessionMeta);\n }\n });\n }\n}\n"]}
1
+ {"version":3,"file":"VolatileSessionManager.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/VolatileSessionManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAG1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1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E;gBAC1C,IAAI,CAAC,aAAa,EAAE,CAAC;aACtB;QACH,CAAC,CAAC,CAAC;QAEH,iGAAiG;QACjG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,8BAA8B,CAAC,IAAU;YACvE,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;YAC7B,MAAM,yBAAyB,GAAG,uBAAuB,CAAC,gBAAgB,EAAE,CAAC;YAE7E,IAAI,OAAO,IAAI,OAAO,CAAC,EAAE,MAAK,yBAAyB,aAAzB,yBAAyB,uBAAzB,yBAAyB,CAAE,SAAS,CAAA,EAAE;gBAClE,MAAM,WAAW,GAAG,+BAA+B,CACjD,uBAAuB,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,CAAC,EAC1E,yBAAyB,CAC1B,CAAC;gBAEF,uBAAuB,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;gBACtD,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;aAC9C;QACH,CAAC,CAAC,CAAC;IACL,CAAC;;AAtDc,0CAAkB,GAAG,cAAc,CAAC,OAAO,CAAC","sourcesContent":["import { faro } from '@grafana/faro-core';\nimport type { Meta } from '@grafana/faro-core';\n\nimport { throttle } from '../../../utils';\nimport { getItem, removeItem, setItem, webStorageType } from '../../../utils/webStorage';\n\nimport { isSampled } from './sampling';\nimport { STORAGE_KEY, STORAGE_UPDATE_DELAY } from './sessionConstants';\nimport { addSessionMetadataToNextSession, createUserSessionObject, getUserSessionUpdater } from './sessionManagerUtils';\nimport type { FaroUserSession } from './types';\n\nexport class VolatileSessionsManager {\n private static storageTypeSession = webStorageType.session;\n private updateUserSession: ReturnType<typeof getUserSessionUpdater>;\n\n constructor() {\n this.updateUserSession = getUserSessionUpdater({\n fetchUserSession: VolatileSessionsManager.fetchUserSession,\n storeUserSession: VolatileSessionsManager.storeUserSession,\n });\n\n this.init();\n }\n\n static removeUserSession() {\n removeItem(STORAGE_KEY, VolatileSessionsManager.storageTypeSession);\n }\n\n static storeUserSession(session: FaroUserSession): void {\n setItem(STORAGE_KEY, JSON.stringify(session), VolatileSessionsManager.storageTypeSession);\n }\n\n static fetchUserSession(): FaroUserSession | null {\n const storedSession = getItem(STORAGE_KEY, VolatileSessionsManager.storageTypeSession);\n\n if (storedSession) {\n return JSON.parse(storedSession) as FaroUserSession;\n }\n\n return null;\n }\n\n updateSession = throttle(() => this.updateUserSession(), STORAGE_UPDATE_DELAY);\n\n private init(): void {\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n this.updateSession();\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the local storage session\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n const sessionFromSessionStorage = VolatileSessionsManager.fetchUserSession();\n\n if (session && session.id !== sessionFromSessionStorage?.sessionId) {\n const userSession = addSessionMetadataToNextSession(\n createUserSessionObject({ sessionId: session.id, isSampled: isSampled() }),\n sessionFromSessionStorage\n );\n\n VolatileSessionsManager.storeUserSession(userSession);\n faro.api.setSession(userSession.sessionMeta);\n }\n });\n }\n}\n"]}
@@ -1,4 +1,5 @@
1
1
  export { PersistentSessionsManager } from './PersistentSessionsManager';
2
2
  export { VolatileSessionsManager } from './VolatileSessionManager';
3
- export * from './sessionConstants';
3
+ export { MAX_SESSION_PERSISTENCE_TIME, MAX_SESSION_PERSISTENCE_TIME_BUFFER, SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME, STORAGE_KEY, STORAGE_UPDATE_DELAY, defaultSessionTrackingConfig, } from './sessionConstants';
4
+ export { isSampled } from './sampling';
4
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AACnE,cAAc,oBAAoB,CAAC","sourcesContent":["export { PersistentSessionsManager } from './PersistentSessionsManager';\nexport { VolatileSessionsManager } from './VolatileSessionManager';\nexport * from './sessionConstants';\nexport type { FaroUserSession } from './types';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AAEnE,OAAO,EACL,4BAA4B,EAC5B,mCAAmC,EACnC,uBAAuB,EACvB,uBAAuB,EACvB,WAAW,EACX,oBAAoB,EACpB,4BAA4B,GAC7B,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export { PersistentSessionsManager } from './PersistentSessionsManager';\nexport { VolatileSessionsManager } from './VolatileSessionManager';\n\nexport {\n MAX_SESSION_PERSISTENCE_TIME,\n MAX_SESSION_PERSISTENCE_TIME_BUFFER,\n SESSION_EXPIRATION_TIME,\n SESSION_INACTIVITY_TIME,\n STORAGE_KEY,\n STORAGE_UPDATE_DELAY,\n defaultSessionTrackingConfig,\n} from './sessionConstants';\n\nexport { isSampled } from './sampling';\n\nexport type { FaroUserSession } from './types';\n"]}
@@ -0,0 +1,13 @@
1
+ import { faro } from '@grafana/faro-core';
2
+ export function isSampled() {
3
+ var _a, _b, _c;
4
+ const sendAllSignals = 1;
5
+ const sessionTracking = faro.config.sessionTracking;
6
+ let samplingRate = (_c = (_b = (_a = sessionTracking === null || sessionTracking === void 0 ? void 0 : sessionTracking.sampler) === null || _a === void 0 ? void 0 : _a.call(sessionTracking, { metas: faro.metas.value })) !== null && _b !== void 0 ? _b : sessionTracking === null || sessionTracking === void 0 ? void 0 : sessionTracking.samplingRate) !== null && _c !== void 0 ? _c : sendAllSignals;
7
+ if (typeof samplingRate !== 'number') {
8
+ const sendNoSignals = 0;
9
+ samplingRate = sendNoSignals;
10
+ }
11
+ return Math.random() < samplingRate;
12
+ }
13
+ //# sourceMappingURL=sampling.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sampling.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/sampling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,MAAM,UAAU,SAAS;;IACvB,MAAM,cAAc,GAAG,CAAC,CAAC;IACzB,MAAM,eAAe,GAAG,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC;IACpD,IAAI,YAAY,GACd,MAAA,MAAA,MAAA,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,OAAO,gEAAG,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,mCAAI,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,YAAY,mCAAI,cAAc,CAAC;IAE7G,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE;QACpC,MAAM,aAAa,GAAG,CAAC,CAAC;QACxB,YAAY,GAAG,aAAa,CAAC;KAC9B;IAED,OAAO,IAAI,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC;AACtC,CAAC","sourcesContent":["import { faro } from '@grafana/faro-core';\n\nexport function isSampled(): boolean {\n const sendAllSignals = 1;\n const sessionTracking = faro.config.sessionTracking;\n let samplingRate =\n sessionTracking?.sampler?.({ metas: faro.metas.value }) ?? sessionTracking?.samplingRate ?? sendAllSignals;\n\n if (typeof samplingRate !== 'number') {\n const sendNoSignals = 0;\n samplingRate = sendNoSignals;\n }\n\n return Math.random() < samplingRate;\n}\n"]}
@@ -2,8 +2,8 @@ export const STORAGE_KEY = '__FARO_SESSION__';
2
2
  export const SESSION_EXPIRATION_TIME = 4 * 60 * 60 * 1000; // hrs
3
3
  export const SESSION_INACTIVITY_TIME = 15 * 60 * 1000; // minutes
4
4
  export const STORAGE_UPDATE_DELAY = 1 * 1000; // seconds
5
- export const MAX_SESSION_PERSISTENCE_TIME_BUFFER = 5 * 60 * 1000;
6
- export const MAX_SESSION_PERSISTENCE_TIME = SESSION_EXPIRATION_TIME + MAX_SESSION_PERSISTENCE_TIME_BUFFER;
5
+ export const MAX_SESSION_PERSISTENCE_TIME_BUFFER = 1 * 60 * 1000;
6
+ export const MAX_SESSION_PERSISTENCE_TIME = SESSION_INACTIVITY_TIME + MAX_SESSION_PERSISTENCE_TIME_BUFFER;
7
7
  export const defaultSessionTrackingConfig = {
8
8
  enabled: false,
9
9
  persistent: false,
@@ -1 +1 @@
1
- {"version":3,"file":"sessionConstants.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/sessionConstants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,kBAAkB,CAAC;AAC9C,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,MAAM;AACjE,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,UAAU;AACjE,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,UAAU;AAExD,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AACjE,MAAM,CAAC,MAAM,4BAA4B,GAAG,uBAAuB,GAAG,mCAAmC,CAAC;AAE1G,MAAM,CAAC,MAAM,4BAA4B,GAAG;IAC1C,OAAO,EAAE,KAAK;IACd,UAAU,EAAE,KAAK;IACjB,yBAAyB,EAAE,4BAA4B;CAC/C,CAAC","sourcesContent":["export const STORAGE_KEY = '__FARO_SESSION__';\nexport const SESSION_EXPIRATION_TIME = 4 * 60 * 60 * 1000; // hrs\nexport const SESSION_INACTIVITY_TIME = 15 * 60 * 1000; // minutes\nexport const STORAGE_UPDATE_DELAY = 1 * 1000; // seconds\n\nexport const MAX_SESSION_PERSISTENCE_TIME_BUFFER = 5 * 60 * 1000;\nexport const MAX_SESSION_PERSISTENCE_TIME = SESSION_EXPIRATION_TIME + MAX_SESSION_PERSISTENCE_TIME_BUFFER;\n\nexport const defaultSessionTrackingConfig = {\n enabled: false,\n persistent: false,\n maxSessionPersistenceTime: MAX_SESSION_PERSISTENCE_TIME,\n} as const;\n"]}
1
+ {"version":3,"file":"sessionConstants.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/sessionConstants.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,WAAW,GAAG,kBAAkB,CAAC;AAC9C,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,MAAM;AACjE,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,UAAU;AACjE,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,UAAU;AAExD,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AACjE,MAAM,CAAC,MAAM,4BAA4B,GAAG,uBAAuB,GAAG,mCAAmC,CAAC;AAE1G,MAAM,CAAC,MAAM,4BAA4B,GAA8B;IACrE,OAAO,EAAE,KAAK;IACd,UAAU,EAAE,KAAK;IACjB,yBAAyB,EAAE,4BAA4B;CAC/C,CAAC","sourcesContent":["import type { Config } from '@grafana/faro-core';\n\nexport const STORAGE_KEY = '__FARO_SESSION__';\nexport const SESSION_EXPIRATION_TIME = 4 * 60 * 60 * 1000; // hrs\nexport const SESSION_INACTIVITY_TIME = 15 * 60 * 1000; // minutes\nexport const STORAGE_UPDATE_DELAY = 1 * 1000; // seconds\n\nexport const MAX_SESSION_PERSISTENCE_TIME_BUFFER = 1 * 60 * 1000;\nexport const MAX_SESSION_PERSISTENCE_TIME = SESSION_INACTIVITY_TIME + MAX_SESSION_PERSISTENCE_TIME_BUFFER;\n\nexport const defaultSessionTrackingConfig: Config['sessionTracking'] = {\n enabled: false,\n persistent: false,\n maxSessionPersistenceTime: MAX_SESSION_PERSISTENCE_TIME,\n} as const;\n"]}
@@ -1,11 +1,13 @@
1
1
  import { dateNow, faro, genShortID } from '@grafana/faro-core';
2
+ import { isSampled } from './sampling';
2
3
  import { SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME } from './sessionConstants';
3
- export function createUserSessionObject(sessionId) {
4
+ export function createUserSessionObject({ sessionId = genShortID(), isSampled = true, } = {}) {
4
5
  const now = dateNow();
5
6
  return {
6
- sessionId: sessionId !== null && sessionId !== void 0 ? sessionId : genShortID(),
7
+ sessionId,
7
8
  lastActivity: now,
8
9
  started: now,
10
+ isSampled: isSampled,
9
11
  };
10
12
  }
11
13
  export function isUserSessionValid(session) {
@@ -31,7 +33,7 @@ export function getUserSessionUpdater({ fetchUserSession, storeUserSession }) {
31
33
  storeUserSession(Object.assign(Object.assign({}, sessionFromStorage), { lastActivity: dateNow() }));
32
34
  }
33
35
  else {
34
- let newSession = addSessionMetadataToNextSession(createUserSessionObject(), sessionFromStorage);
36
+ let newSession = addSessionMetadataToNextSession(createUserSessionObject({ isSampled: isSampled() }), sessionFromStorage);
35
37
  storeUserSession(newSession);
36
38
  (_a = faro.api) === null || _a === void 0 ? void 0 : _a.setSession(newSession.sessionMeta);
37
39
  (_c = (_b = faro.config.sessionTracking) === null || _b === void 0 ? void 0 : _b.onSessionChange) === null || _c === void 0 ? void 0 : _c.call(_b, (_d = sessionFromStorage === null || sessionFromStorage === void 0 ? void 0 : sessionFromStorage.sessionMeta) !== null && _d !== void 0 ? _d : null, newSession.sessionMeta);
@@ -39,14 +41,11 @@ export function getUserSessionUpdater({ fetchUserSession, storeUserSession }) {
39
41
  };
40
42
  }
41
43
  export function addSessionMetadataToNextSession(newSession, previousSession) {
42
- var _a;
44
+ var _a, _b;
43
45
  const sessionWithMeta = Object.assign(Object.assign({}, newSession), { sessionMeta: {
44
46
  id: newSession.sessionId,
47
+ attributes: Object.assign(Object.assign(Object.assign({}, ((_b = (_a = faro.metas.value.session) === null || _a === void 0 ? void 0 : _a.attributes) !== null && _b !== void 0 ? _b : {})), (previousSession != null ? { previousSession: previousSession.sessionId } : {})), { isSampled: newSession.isSampled.toString() }),
45
48
  } });
46
- const metaAttributes = (_a = faro.metas.value.session) === null || _a === void 0 ? void 0 : _a.attributes;
47
- if (metaAttributes || previousSession != null) {
48
- sessionWithMeta.sessionMeta.attributes = Object.assign(Object.assign({}, (metaAttributes !== null && metaAttributes !== void 0 ? metaAttributes : {})), (previousSession != null ? { previousSession: previousSession.sessionId } : {}));
49
- }
50
49
  return sessionWithMeta;
51
50
  }
52
51
  //# sourceMappingURL=sessionManagerUtils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sessionManagerUtils.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/sessionManagerUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAGtF,MAAM,UAAU,uBAAuB,CAAC,SAAkB;IACxD,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;IAEtB,OAAO;QACL,SAAS,EAAE,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,UAAU,EAAE;QACpC,YAAY,EAAE,GAAG;QACjB,OAAO,EAAE,GAAG;KACb,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,OAA+B;IAChE,IAAI,OAAO,IAAI,IAAI,EAAE;QACnB,OAAO,KAAK,CAAC;KACd;IAED,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;IACtB,MAAM,aAAa,GAAG,GAAG,GAAG,OAAO,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I,eAAe,IAAI,IAAI,EAAE;QAC7C,eAAe,CAAC,WAAW,CAAC,UAAU,mCACjC,CAAC,cAAc,aAAd,cAAc,cAAd,cAAc,GAAI,EAAE,CAAC,GACtB,CAAC,eAAe,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,eAAe,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CACnF,CAAC;KACH;IAED,OAAO,eAAe,CAAC;AACzB,CAAC","sourcesContent":["import { dateNow, faro, genShortID } from '@grafana/faro-core';\n\nimport { SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME } from './sessionConstants';\nimport type { FaroUserSession } from './types';\n\nexport function createUserSessionObject(sessionId?: string): FaroUserSession {\n const now = dateNow();\n\n return {\n sessionId: sessionId ?? genShortID(),\n lastActivity: now,\n started: now,\n };\n}\n\nexport function isUserSessionValid(session: FaroUserSession | null): boolean {\n if (session == null) {\n return false;\n }\n\n const now = dateNow();\n const lifetimeValid = now - session.started < SESSION_EXPIRATION_TIME;\n\n if (!lifetimeValid) {\n return false;\n }\n\n const inactivityPeriodValid = now - session.lastActivity < SESSION_INACTIVITY_TIME;\n return inactivityPeriodValid;\n}\n\ntype GetUserSessionUpdaterParams = {\n storeUserSession: (session: FaroUserSession) => void;\n fetchUserSession: () => FaroUserSession | null;\n};\n\nexport function getUserSessionUpdater({ fetchUserSession, storeUserSession }: GetUserSessionUpdaterParams): () => void {\n return function updateSession(): void {\n if (!fetchUserSession || !storeUserSession) {\n return;\n }\n\n const sessionFromStorage = fetchUserSession();\n\n if (isUserSessionValid(sessionFromStorage)) {\n storeUserSession({ ...sessionFromStorage!, lastActivity: dateNow() });\n } else {\n let newSession = addSessionMetadataToNextSession(createUserSessionObject(), sessionFromStorage);\n\n storeUserSession(newSession);\n\n faro.api?.setSession(newSession.sessionMeta);\n faro.config.sessionTracking?.onSessionChange?.(sessionFromStorage?.sessionMeta ?? null, newSession.sessionMeta!);\n }\n };\n}\n\nexport function addSessionMetadataToNextSession(newSession: FaroUserSession, previousSession: FaroUserSession | null) {\n const sessionWithMeta: Required<FaroUserSession> = {\n ...newSession,\n sessionMeta: {\n id: newSession.sessionId,\n },\n };\n\n const metaAttributes = faro.metas.value.session?.attributes;\n if (metaAttributes || previousSession != null) {\n sessionWithMeta.sessionMeta.attributes = {\n ...(metaAttributes ?? {}),\n ...(previousSession != null ? { previousSession: previousSession.sessionId } : {}),\n };\n }\n\n return sessionWithMeta;\n}\n"]}
1
+ {"version":3,"file":"sessionManagerUtils.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/sessionManagerUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAQtF,MAAM,UAAU,uBAAuB,CAAC,EACtC,SAAS,GAAG,UAAU,EAAE,EACxB,SAAS,GAAG,IAAI,MACiB,EAAE;IACnC,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;IAEtB,OAAO;QACL,SAAS;QACT,YAAY,EAAE,GAAG;QACjB,OAAO,EAAE,GAAG;QACZ,SAAS,EAAE,SAAS;KACrB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,OAA+B;IAChE,IAAI,OAAO,IAAI,IAAI,EAAE;QACnB,OAAO,KAAK,CAAC;KACd;IAED,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;IACtB,MAAM,aAAa,GAAG,GAAG,GAAG,OAAO,CAAC,OAAO,GAAG,uBAAuB,CAAC;IAEtE,IAAI,CAAC,aAAa,EAAE;QAClB,OAAO,KAAK,CAAC;KACd;IAED,MAAM,qBAAqB,GAAG,GAAG,GAAG,OAAO,CAAC,YAAY,GAAG,uBAAuB,CAAC;IACnF,OAAO,qBAAqB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AQ,EAAE,GAC3C;SACF,GACF,CAAC;IAEF,OAAO,eAAe,CAAC;AACzB,CAAC","sourcesContent":["import { dateNow, faro, genShortID } from '@grafana/faro-core';\n\nimport { isSampled } from './sampling';\nimport { SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME } from './sessionConstants';\nimport type { FaroUserSession } from './types';\n\ntype CreateUserSessionObjectParams = {\n sessionId?: string;\n isSampled?: boolean;\n};\n\nexport function createUserSessionObject({\n sessionId = genShortID(),\n isSampled = true,\n}: CreateUserSessionObjectParams = {}): FaroUserSession {\n const now = dateNow();\n\n return {\n sessionId,\n lastActivity: now,\n started: now,\n isSampled: isSampled,\n };\n}\n\nexport function isUserSessionValid(session: FaroUserSession | null): boolean {\n if (session == null) {\n return false;\n }\n\n const now = dateNow();\n const lifetimeValid = now - session.started < SESSION_EXPIRATION_TIME;\n\n if (!lifetimeValid) {\n return false;\n }\n\n const inactivityPeriodValid = now - session.lastActivity < SESSION_INACTIVITY_TIME;\n return inactivityPeriodValid;\n}\n\ntype GetUserSessionUpdaterParams = {\n storeUserSession: (session: FaroUserSession) => void;\n fetchUserSession: () => FaroUserSession | null;\n};\n\nexport function getUserSessionUpdater({ fetchUserSession, storeUserSession }: GetUserSessionUpdaterParams): () => void {\n return function updateSession(): void {\n if (!fetchUserSession || !storeUserSession) {\n return;\n }\n\n const sessionFromStorage = fetchUserSession();\n\n if (isUserSessionValid(sessionFromStorage)) {\n storeUserSession({ ...sessionFromStorage!, lastActivity: dateNow() });\n } else {\n let newSession = addSessionMetadataToNextSession(\n createUserSessionObject({ isSampled: isSampled() }),\n sessionFromStorage\n );\n\n storeUserSession(newSession);\n\n faro.api?.setSession(newSession.sessionMeta);\n faro.config.sessionTracking?.onSessionChange?.(sessionFromStorage?.sessionMeta ?? null, newSession.sessionMeta!);\n }\n };\n}\n\nexport function addSessionMetadataToNextSession(newSession: FaroUserSession, previousSession: FaroUserSession | null) {\n const sessionWithMeta: Required<FaroUserSession> = {\n ...newSession,\n sessionMeta: {\n id: newSession.sessionId,\n attributes: {\n ...(faro.metas.value.session?.attributes ?? {}),\n ...(previousSession != null ? { previousSession: previousSession.sessionId } : {}),\n isSampled: newSession.isSampled.toString(),\n },\n },\n };\n\n return sessionWithMeta;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { MetaSession } from '@grafana/faro-core';\n\nexport interface FaroUserSession {\n sessionId: string;\n lastActivity: number;\n started: number;\n sessionMeta?: MetaSession;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/instrumentations/session/sessionManager/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { MetaSession } from '@grafana/faro-core';\n\nexport interface FaroUserSession {\n sessionId: string;\n lastActivity: number;\n started: number;\n isSampled: boolean;\n sessionMeta?: MetaSession;\n}\n"]}
@@ -1,3 +1,3 @@
1
- export * from './webStorage';
2
- export * from './throttle';
1
+ export { isLocalStorageAvailable, isSessionStorageAvailable, webStorageType, getItem, isWebStorageAvailable, removeItem, setItem, } from './webStorage';
2
+ export { throttle } from './throttle';
3
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC","sourcesContent":["export * from './webStorage';\nexport * from './throttle';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,cAAc,EACd,OAAO,EACP,qBAAqB,EACrB,UAAU,EACV,OAAO,GACR,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export {\n isLocalStorageAvailable,\n isSessionStorageAvailable,\n webStorageType,\n getItem,\n isWebStorageAvailable,\n removeItem,\n setItem,\n} from './webStorage';\n\nexport { throttle } from './throttle';\n"]}
@@ -9,3 +9,4 @@ export { ConsoleTransport, FetchTransport } from './transports';
9
9
  export type { ClockFn, ConsoleTransportOptions, FetchTransportOptions, FetchTransportRequestOptions, } from './transports';
10
10
  export { faro, allLogLevels, BaseExtension, BaseInstrumentation, BaseTransport, Conventions, createInternalLogger, createPromiseBuffer, deepEqual, defaultExceptionType, defaultGlobalObjectKey, defaultInternalLoggerLevel, defaultLogLevel, genShortID, getCurrentTimestamp, getInternalFaroFromGlobalObject, getTransportBody, globalObject, internalGlobalObjectKey, isArray, isBoolean, isDomError, isDomException, isElement, isElementDefined, isError, isErrorDefined, isErrorEvent, isEvent, isEventDefined, isFunction, isInstanceOf, isInt, isInternalFaroOnGlobalObject, isMap, isMapDefined, isNull, isNumber, isObject, isPrimitive, isRegExp, isString, isSymbol, isSyntheticEvent, isThenable, isToString, isTypeof, isUndefined, InternalLoggerLevel, LogLevel, noop, setInternalFaroOnGlobalObject, TransportItemType, transportItemTypeToBodyKey, VERSION, } from '@grafana/faro-core';
11
11
  export type { Faro, API, APIEvent, BaseObject, BaseObjectKey, BaseObjectPrimitiveValue, BaseObjectValue, BeforeSendHook, BufferItem, Config, EventAttributes, EventEvent, EventsAPI, ExceptionEvent, ExceptionStackFrame, ExceptionsAPI, ExtendedError, Extension, GlobalObject, Instrumentation, Instrumentations, InternalLogger, LogContext, LogEvent, LogsAPI, MeasurementEvent, MeasurementsAPI, Meta, MetaAPI, MetaApp, MetaAttributes, MetaBrowser, MetaGetter, MetaItem, MetaPage, Metas, MetaSDK, MetaSDKIntegration, MetaSession, MetaUser, MetaView, OTELApi, Patterns, PromiseBuffer, PromiseBufferOptions, PromiseProducer, PushErrorOptions, PushLogOptions, PushMeasurementOptions, Stacktrace, StacktraceParser, TraceContext, TraceEvent, TracesAPI, Transport, TransportBody, TransportItem, TransportItemPayload, Transports, UnpatchedConsole, } from '@grafana/faro-core';
12
+ export { PersistentSessionsManager, VolatileSessionsManager, MAX_SESSION_PERSISTENCE_TIME, MAX_SESSION_PERSISTENCE_TIME_BUFFER, SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME, STORAGE_KEY, } from './instrumentations/session';
@@ -3,7 +3,7 @@ import type { ConsoleInstrumentationOptions } from './types';
3
3
  export declare class ConsoleInstrumentation extends BaseInstrumentation {
4
4
  private options;
5
5
  readonly name = "@grafana/faro-web-sdk:instrumentation-console";
6
- readonly version = "1.2.8";
6
+ readonly version = "1.2.9";
7
7
  static defaultDisabledLevels: LogLevel[];
8
8
  constructor(options?: ConsoleInstrumentationOptions);
9
9
  initialize(): void;
@@ -1,6 +1,6 @@
1
1
  import { BaseInstrumentation } from '@grafana/faro-core';
2
2
  export declare class ErrorsInstrumentation extends BaseInstrumentation {
3
3
  readonly name = "@grafana/faro-web-sdk:instrumentation-errors";
4
- readonly version = "1.2.8";
4
+ readonly version = "1.2.9";
5
5
  initialize(): void;
6
6
  }
@@ -5,3 +5,4 @@ export { buildStackFrame, ErrorsInstrumentation, getDataFromSafariExtensions, ge
5
5
  export type { ErrorEvent, ExtendedPromiseRejectionEvent } from './errors';
6
6
  export { ViewInstrumentation } from './view';
7
7
  export { WebVitalsInstrumentation } from './webVitals';
8
+ export { PersistentSessionsManager, VolatileSessionsManager, MAX_SESSION_PERSISTENCE_TIME, MAX_SESSION_PERSISTENCE_TIME_BUFFER, SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME, STORAGE_KEY, } from './session';
@@ -1,2 +1,3 @@
1
1
  export { SessionInstrumentation } from './instrumentation';
2
- export * from './sessionManager';
2
+ export { MAX_SESSION_PERSISTENCE_TIME, MAX_SESSION_PERSISTENCE_TIME_BUFFER, PersistentSessionsManager, SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME, STORAGE_KEY, STORAGE_UPDATE_DELAY, VolatileSessionsManager, defaultSessionTrackingConfig, isSampled, } from './sessionManager';
3
+ export type { FaroUserSession } from './sessionManager';
@@ -1,10 +1,9 @@
1
1
  import { BaseInstrumentation } from '@grafana/faro-core';
2
2
  export declare class SessionInstrumentation extends BaseInstrumentation {
3
3
  readonly name = "@grafana/faro-web-sdk:instrumentation-session";
4
- readonly version = "1.2.8";
4
+ readonly version = "1.2.9";
5
5
  private notifiedSession;
6
6
  private sendSessionStartEvent;
7
- private getSessionManagerInstanceByConfiguredStrategy;
8
7
  private createInitialSessionMeta;
9
8
  initialize(): void;
10
9
  }
@@ -1,9 +1,8 @@
1
1
  import type { FaroUserSession } from './types';
2
2
  export declare class PersistentSessionsManager {
3
- private initialSessionId?;
4
3
  private static storageTypeLocal;
5
4
  private updateUserSession;
6
- constructor(initialSessionId?: string | undefined);
5
+ constructor();
7
6
  static removeUserSession(): void;
8
7
  static storeUserSession(session: FaroUserSession): void;
9
8
  static fetchUserSession(): FaroUserSession | null;
@@ -1,9 +1,8 @@
1
1
  import type { FaroUserSession } from './types';
2
2
  export declare class VolatileSessionsManager {
3
- private initialSessionId?;
4
3
  private static storageTypeSession;
5
4
  private updateUserSession;
6
- constructor(initialSessionId?: string | undefined);
5
+ constructor();
7
6
  static removeUserSession(): void;
8
7
  static storeUserSession(session: FaroUserSession): void;
9
8
  static fetchUserSession(): FaroUserSession | null;
@@ -1,4 +1,5 @@
1
1
  export { PersistentSessionsManager } from './PersistentSessionsManager';
2
2
  export { VolatileSessionsManager } from './VolatileSessionManager';
3
- export * from './sessionConstants';
3
+ export { MAX_SESSION_PERSISTENCE_TIME, MAX_SESSION_PERSISTENCE_TIME_BUFFER, SESSION_EXPIRATION_TIME, SESSION_INACTIVITY_TIME, STORAGE_KEY, STORAGE_UPDATE_DELAY, defaultSessionTrackingConfig, } from './sessionConstants';
4
+ export { isSampled } from './sampling';
4
5
  export type { FaroUserSession } from './types';
@@ -0,0 +1 @@
1
+ export declare function isSampled(): boolean;
@@ -1,11 +1,8 @@
1
+ import type { Config } from '@grafana/faro-core';
1
2
  export declare const STORAGE_KEY = "__FARO_SESSION__";
2
3
  export declare const SESSION_EXPIRATION_TIME: number;
3
4
  export declare const SESSION_INACTIVITY_TIME: number;
4
5
  export declare const STORAGE_UPDATE_DELAY: number;
5
6
  export declare const MAX_SESSION_PERSISTENCE_TIME_BUFFER: number;
6
7
  export declare const MAX_SESSION_PERSISTENCE_TIME: number;
7
- export declare const defaultSessionTrackingConfig: {
8
- readonly enabled: false;
9
- readonly persistent: false;
10
- readonly maxSessionPersistenceTime: number;
11
- };
8
+ export declare const defaultSessionTrackingConfig: Config['sessionTracking'];
@@ -1,5 +1,9 @@
1
1
  import type { FaroUserSession } from './types';
2
- export declare function createUserSessionObject(sessionId?: string): FaroUserSession;
2
+ type CreateUserSessionObjectParams = {
3
+ sessionId?: string;
4
+ isSampled?: boolean;
5
+ };
6
+ export declare function createUserSessionObject({ sessionId, isSampled, }?: CreateUserSessionObjectParams): FaroUserSession;
3
7
  export declare function isUserSessionValid(session: FaroUserSession | null): boolean;
4
8
  type GetUserSessionUpdaterParams = {
5
9
  storeUserSession: (session: FaroUserSession) => void;
@@ -3,5 +3,6 @@ export interface FaroUserSession {
3
3
  sessionId: string;
4
4
  lastActivity: number;
5
5
  started: number;
6
+ isSampled: boolean;
6
7
  sessionMeta?: MetaSession;
7
8
  }
@@ -1,7 +1,7 @@
1
1
  import { BaseInstrumentation } from '@grafana/faro-core';
2
2
  export declare class ViewInstrumentation extends BaseInstrumentation {
3
3
  readonly name = "@grafana/faro-web-sdk:instrumentation-view";
4
- readonly version = "1.2.8";
4
+ readonly version = "1.2.9";
5
5
  private notifiedView;
6
6
  private sendViewChangedEvent;
7
7
  initialize(): void;
@@ -1,7 +1,7 @@
1
1
  import { BaseInstrumentation } from '@grafana/faro-core';
2
2
  export declare class WebVitalsInstrumentation extends BaseInstrumentation {
3
3
  readonly name = "@grafana/faro-web-sdk:instrumentation-web-vitals";
4
- readonly version = "1.2.8";
4
+ readonly version = "1.2.9";
5
5
  static mapping: {
6
6
  cls: (onReport: import("web-vitals").CLSReportCallback, opts?: import("web-vitals").ReportOpts | undefined) => void;
7
7
  fcp: (onReport: import("web-vitals").FCPReportCallback, opts?: import("web-vitals").ReportOpts | undefined) => void;
@@ -4,7 +4,7 @@ import type { ConsoleTransportOptions } from './types';
4
4
  export declare class ConsoleTransport extends BaseTransport {
5
5
  private options;
6
6
  readonly name = "@grafana/faro-web-sdk:transport-console";
7
- readonly version = "1.2.8";
7
+ readonly version = "1.2.9";
8
8
  constructor(options?: ConsoleTransportOptions);
9
9
  send(item: TransportItem): void;
10
10
  }
@@ -4,7 +4,7 @@ import type { FetchTransportOptions } from './types';
4
4
  export declare class FetchTransport extends BaseTransport {
5
5
  private options;
6
6
  readonly name = "@grafana/faro-web-sdk:transport-fetch";
7
- readonly version = "1.2.8";
7
+ readonly version = "1.2.9";
8
8
  promiseBuffer: PromiseBuffer<Response | void>;
9
9
  private readonly rateLimitBackoffMs;
10
10
  private readonly getNow;
@@ -1,2 +1,2 @@
1
- export * from './webStorage';
2
- export * from './throttle';
1
+ export { isLocalStorageAvailable, isSessionStorageAvailable, webStorageType, getItem, isWebStorageAvailable, removeItem, setItem, } from './webStorage';
2
+ export { throttle } from './throttle';