@shware/analytics 14.1.0 → 15.0.1

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 (70) hide show
  1. package/README.md +48 -33
  2. package/dist/constants/storage.cjs +7 -1
  3. package/dist/constants/storage.cjs.map +1 -1
  4. package/dist/constants/storage.d.cts +6 -0
  5. package/dist/constants/storage.d.cts.map +1 -1
  6. package/dist/constants/storage.d.mts +6 -0
  7. package/dist/constants/storage.d.mts.map +1 -1
  8. package/dist/constants/storage.mjs +7 -1
  9. package/dist/constants/storage.mjs.map +1 -1
  10. package/dist/index.cjs +2 -1
  11. package/dist/index.d.cts +2 -2
  12. package/dist/index.d.mts +2 -2
  13. package/dist/index.mjs +2 -2
  14. package/dist/schema/index.cjs +1 -1
  15. package/dist/schema/index.cjs.map +1 -1
  16. package/dist/schema/index.mjs +1 -1
  17. package/dist/schema/index.mjs.map +1 -1
  18. package/dist/setup/session.cjs +9 -0
  19. package/dist/setup/session.cjs.map +1 -1
  20. package/dist/setup/session.d.cts +10 -0
  21. package/dist/setup/session.d.cts.map +1 -1
  22. package/dist/setup/session.d.mts +10 -0
  23. package/dist/setup/session.d.mts.map +1 -1
  24. package/dist/setup/session.mjs +9 -0
  25. package/dist/setup/session.mjs.map +1 -1
  26. package/dist/third-parties/google-analytics.cjs.map +1 -1
  27. package/dist/third-parties/google-analytics.mjs.map +1 -1
  28. package/dist/third-parties/posthog.cjs +9 -8
  29. package/dist/third-parties/posthog.cjs.map +1 -1
  30. package/dist/third-parties/posthog.d.cts +9 -8
  31. package/dist/third-parties/posthog.d.cts.map +1 -1
  32. package/dist/third-parties/posthog.d.mts +9 -8
  33. package/dist/third-parties/posthog.d.mts.map +1 -1
  34. package/dist/third-parties/posthog.mjs +9 -8
  35. package/dist/third-parties/posthog.mjs.map +1 -1
  36. package/dist/track/gtag.cjs.map +1 -1
  37. package/dist/track/gtag.d.cts +2 -1
  38. package/dist/track/gtag.d.cts.map +1 -1
  39. package/dist/track/gtag.d.mts +2 -1
  40. package/dist/track/gtag.d.mts.map +1 -1
  41. package/dist/track/gtag.mjs.map +1 -1
  42. package/dist/track/index.cjs +117 -67
  43. package/dist/track/index.cjs.map +1 -1
  44. package/dist/track/index.d.cts +9 -2
  45. package/dist/track/index.d.cts.map +1 -1
  46. package/dist/track/index.d.mts +9 -2
  47. package/dist/track/index.d.mts.map +1 -1
  48. package/dist/track/index.mjs +117 -68
  49. package/dist/track/index.mjs.map +1 -1
  50. package/dist/visitor/id.cjs +5 -0
  51. package/dist/visitor/id.cjs.map +1 -1
  52. package/dist/visitor/id.d.cts +2 -0
  53. package/dist/visitor/id.d.cts.map +1 -1
  54. package/dist/visitor/id.d.mts +2 -0
  55. package/dist/visitor/id.d.mts.map +1 -1
  56. package/dist/visitor/id.mjs +5 -1
  57. package/dist/visitor/id.mjs.map +1 -1
  58. package/dist/visitor/index.cjs +94 -24
  59. package/dist/visitor/index.cjs.map +1 -1
  60. package/dist/visitor/index.d.cts +35 -8
  61. package/dist/visitor/index.d.cts.map +1 -1
  62. package/dist/visitor/index.d.mts +35 -8
  63. package/dist/visitor/index.d.mts.map +1 -1
  64. package/dist/visitor/index.mjs +95 -26
  65. package/dist/visitor/index.mjs.map +1 -1
  66. package/dist/visitor/types.d.cts +3 -19
  67. package/dist/visitor/types.d.cts.map +1 -1
  68. package/dist/visitor/types.d.mts +3 -19
  69. package/dist/visitor/types.d.mts.map +1 -1
  70. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import { UpdateVisitorDTO } from "../schema/index.cjs";
1
+ import { UserProvidedData } from "../track/types.cjs";
2
2
  import { visitorId } from "./id.cjs";
3
3
  /**
4
4
  * The event that binds the visitor to the user who signed in on it: the server sets the visitor's
@@ -10,16 +10,43 @@ export declare const IDENTIFY_EVENT = "identify";
10
10
  * Hands the signed-in user to the analytics: the third-party user setters (gtag, the pixels) now,
11
11
  * and the server through an `identify` event, sent when the user differs from the one this page
12
12
  * last identified — a host calls this on every page load once someone is signed in. Like any
13
- * event it goes with the next batch or the page-hide beacon; `user_data` is handed to the setters
13
+ * event it goes with the next batch or the page-hide beacon; `userData` is handed to the setters
14
14
  * only, never stored with the event.
15
15
  *
16
- * Without a `user_id` (missing, or `''`) it signs the visitor out: `setVisitor({})` is
17
- * sign-out. Nothing goes to the server, the setters get `distinct_id: null` (PostHog resets its
18
- * identity on it), and the next sign-in is identified again even if it is the same user. Still
19
- * `track('logout')` as usual — that is an ordinary event, and resets nothing.
16
+ * The visitor's sessions so far — those before the sign-in included — become this user's. So a
17
+ * different user than the one last identified on this device (an expired session, a sign-out the
18
+ * host never reported) is `reset()` first: the previous user's visitor stays theirs, and this one
19
+ * starts on a new visitor.
20
20
  *
21
- * Synchronous and never throws: sign-in and sign-out must not fail on analytics.
21
+ * Synchronous and never throws: sign-in must not fail on analytics.
22
22
  */
23
- export declare function setVisitor(dto: Omit<UpdateVisitorDTO, 'tags'>): void;
23
+ export declare function identify(userId: string, userData?: UserProvidedData): void;
24
+ /**
25
+ * Signs the visitor out, as PostHog's `reset()` does: call it on sign-out, after
26
+ * `track('logout')`, so that event still belongs to the user leaving.
27
+ *
28
+ * - The current session ends: what it accrued goes out as a `user_engagement`
29
+ * (`trigger: 'reset'`), as when the page is closed, and the queue is sent under the visitor
30
+ * and session its events happened in.
31
+ * - A new visitor starts, and the next event opens a new session on it. Everything until the next
32
+ * sign-in is anonymous, and becomes whoever's signs in next; the visitor that is left stays the
33
+ * previous user's, with its history.
34
+ * - The third-party setters drop the user (PostHog resets its identity, GA clears its user_id).
35
+ *
36
+ * The device id and the first visit or first open stay: they describe the device, not the person.
37
+ *
38
+ * Only the sign-out this page knows of: the session and visitor are replaced while the device's
39
+ * identity (`keys.user_id` and the visitor, shared by every tab) is still the one this page last
40
+ * identified, or the page has neither identified nor signed out anyone (a host that identifies at
41
+ * sign-in only, on a page loaded since). Every tab of a site sees a sign-out and calls this,
42
+ * some of them late — a tab frozen in the background handles it on resuming, after another tab
43
+ * may have signed out and in again. A call that finds no user, or someone else's binding, is
44
+ * stale: replacing that would cut the new anonymous visitor off from the next sign-in, or end the
45
+ * next user's session and leave their pages on a visitor nobody identified. It only clears this
46
+ * page: its setters are still told, since GA holds the user per page.
47
+ *
48
+ * Synchronous and never throws: sign-out must not fail on analytics.
49
+ */
50
+ export declare function reset(): void;
24
51
  export { visitorId };
25
52
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAWa;;;;;;;;;;;;;;;wBAsBG,WAAW,KAAK,KAAK"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAuCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkDpC"}
@@ -1,4 +1,4 @@
1
- import { UpdateVisitorDTO } from "../schema/index.mjs";
1
+ import { UserProvidedData } from "../track/types.mjs";
2
2
  import { visitorId } from "./id.mjs";
3
3
  /**
4
4
  * The event that binds the visitor to the user who signed in on it: the server sets the visitor's
@@ -10,16 +10,43 @@ export declare const IDENTIFY_EVENT = "identify";
10
10
  * Hands the signed-in user to the analytics: the third-party user setters (gtag, the pixels) now,
11
11
  * and the server through an `identify` event, sent when the user differs from the one this page
12
12
  * last identified — a host calls this on every page load once someone is signed in. Like any
13
- * event it goes with the next batch or the page-hide beacon; `user_data` is handed to the setters
13
+ * event it goes with the next batch or the page-hide beacon; `userData` is handed to the setters
14
14
  * only, never stored with the event.
15
15
  *
16
- * Without a `user_id` (missing, or `''`) it signs the visitor out: `setVisitor({})` is
17
- * sign-out. Nothing goes to the server, the setters get `distinct_id: null` (PostHog resets its
18
- * identity on it), and the next sign-in is identified again even if it is the same user. Still
19
- * `track('logout')` as usual — that is an ordinary event, and resets nothing.
16
+ * The visitor's sessions so far — those before the sign-in included — become this user's. So a
17
+ * different user than the one last identified on this device (an expired session, a sign-out the
18
+ * host never reported) is `reset()` first: the previous user's visitor stays theirs, and this one
19
+ * starts on a new visitor.
20
20
  *
21
- * Synchronous and never throws: sign-in and sign-out must not fail on analytics.
21
+ * Synchronous and never throws: sign-in must not fail on analytics.
22
22
  */
23
- export declare function setVisitor(dto: Omit<UpdateVisitorDTO, 'tags'>): void;
23
+ export declare function identify(userId: string, userData?: UserProvidedData): void;
24
+ /**
25
+ * Signs the visitor out, as PostHog's `reset()` does: call it on sign-out, after
26
+ * `track('logout')`, so that event still belongs to the user leaving.
27
+ *
28
+ * - The current session ends: what it accrued goes out as a `user_engagement`
29
+ * (`trigger: 'reset'`), as when the page is closed, and the queue is sent under the visitor
30
+ * and session its events happened in.
31
+ * - A new visitor starts, and the next event opens a new session on it. Everything until the next
32
+ * sign-in is anonymous, and becomes whoever's signs in next; the visitor that is left stays the
33
+ * previous user's, with its history.
34
+ * - The third-party setters drop the user (PostHog resets its identity, GA clears its user_id).
35
+ *
36
+ * The device id and the first visit or first open stay: they describe the device, not the person.
37
+ *
38
+ * Only the sign-out this page knows of: the session and visitor are replaced while the device's
39
+ * identity (`keys.user_id` and the visitor, shared by every tab) is still the one this page last
40
+ * identified, or the page has neither identified nor signed out anyone (a host that identifies at
41
+ * sign-in only, on a page loaded since). Every tab of a site sees a sign-out and calls this,
42
+ * some of them late — a tab frozen in the background handles it on resuming, after another tab
43
+ * may have signed out and in again. A call that finds no user, or someone else's binding, is
44
+ * stale: replacing that would cut the new anonymous visitor off from the next sign-in, or end the
45
+ * next user's session and leave their pages on a visitor nobody identified. It only clears this
46
+ * page: its setters are still told, since GA holds the user per page.
47
+ *
48
+ * Synchronous and never throws: sign-out must not fail on analytics.
49
+ */
50
+ export declare function reset(): void;
24
51
  export { visitorId };
25
52
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAWa;;;;;;;;;;;;;;;wBAsBG,WAAW,KAAK,KAAK"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAuCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkDpC"}
@@ -1,6 +1,7 @@
1
1
  import { config } from "../setup/index.mjs";
2
- import { visitorId } from "./id.mjs";
3
- import { track } from "../track/index.mjs";
2
+ import { keys } from "../constants/storage.mjs";
3
+ import { newVisitorId, visitorId } from "./id.mjs";
4
+ import { endSession, track } from "../track/index.mjs";
4
5
  // src/visitor/index.ts
5
6
  /**
6
7
  * The event that binds the visitor to the user who signed in on it: the server sets the visitor's
@@ -9,42 +10,110 @@ import { track } from "../track/index.mjs";
9
10
  */
10
11
  const IDENTIFY_EVENT = "identify";
11
12
  /**
12
- * The user this page has identified the visitor as: identify once per user, not per call. Cleared
13
- * on sign-out, so the same user signing back in on this page is identified again.
13
+ * The visitor and user this page last identified, as `<visitor>:<user>`: identify once per binding
14
+ * per page, not per call. The visitor is part of it because the storage is shared: another tab's
15
+ * `reset()` replaces the visitor under this page, and the same user here must then bind the new
16
+ * one too.
17
+ *
18
+ * `undefined` until this page identifies anyone; `null` once it has signed out (`reset()`), so the
19
+ * same user signing back in is identified again, and a later `reset()` here knows it has nothing
20
+ * left to sign out — the device's identity, if any, is someone this page has not identified.
14
21
  */
15
22
  let identified;
23
+ /** Hands the user to every third-party user setter; one that throws does not stop the others. */
24
+ function setUsers(user) {
25
+ config.thirdPartyUserSetters.forEach((setter) => {
26
+ try {
27
+ setter(user);
28
+ } catch (e) {
29
+ if (e instanceof Error) console.log(e.message);
30
+ }
31
+ });
32
+ }
16
33
  /**
17
34
  * Hands the signed-in user to the analytics: the third-party user setters (gtag, the pixels) now,
18
35
  * and the server through an `identify` event, sent when the user differs from the one this page
19
36
  * last identified — a host calls this on every page load once someone is signed in. Like any
20
- * event it goes with the next batch or the page-hide beacon; `user_data` is handed to the setters
37
+ * event it goes with the next batch or the page-hide beacon; `userData` is handed to the setters
21
38
  * only, never stored with the event.
22
39
  *
23
- * Without a `user_id` (missing, or `''`) it signs the visitor out: `setVisitor({})` is
24
- * sign-out. Nothing goes to the server, the setters get `distinct_id: null` (PostHog resets its
25
- * identity on it), and the next sign-in is identified again even if it is the same user. Still
26
- * `track('logout')` as usual — that is an ordinary event, and resets nothing.
40
+ * The visitor's sessions so far — those before the sign-in included — become this user's. So a
41
+ * different user than the one last identified on this device (an expired session, a sign-out the
42
+ * host never reported) is `reset()` first: the previous user's visitor stays theirs, and this one
43
+ * starts on a new visitor.
27
44
  *
28
- * Synchronous and never throws: sign-in and sign-out must not fail on analytics.
45
+ * Synchronous and never throws: sign-in must not fail on analytics.
29
46
  */
30
- function setVisitor(dto) {
31
- if (!dto.user_id) identified = void 0;
32
- else if (dto.user_id !== identified) {
33
- identified = dto.user_id;
34
- track(IDENTIFY_EVENT, { user_id: dto.user_id }, { enableThirdPartyTracking: false });
35
- }
36
- const identity = {
37
- ...dto,
38
- distinct_id: dto.user_id || null
39
- };
40
- config.thirdPartyUserSetters.forEach((setter) => {
41
- try {
42
- setter(identity);
43
- } catch (e) {
44
- if (e instanceof Error) console.log(e.message);
47
+ function identify(userId, userData) {
48
+ let visitor = "";
49
+ try {
50
+ const previous = config.storage.getItem(keys.user_id);
51
+ if (previous && previous !== userId) {
52
+ startOver();
53
+ setUsers({});
45
54
  }
55
+ config.storage.setItem(keys.user_id, userId);
56
+ visitor = visitorId();
57
+ } catch (e) {
58
+ if (e instanceof Error) console.log(e.message);
59
+ }
60
+ const binding = `${visitor}:${userId}`;
61
+ if (binding !== identified) {
62
+ identified = binding;
63
+ track(IDENTIFY_EVENT, { user_id: userId }, { enableThirdPartyTracking: false });
64
+ }
65
+ setUsers({
66
+ user_id: userId,
67
+ user_data: userData
46
68
  });
47
69
  }
48
- export { IDENTIFY_EVENT, setVisitor, visitorId };
70
+ /**
71
+ * Signs the visitor out, as PostHog's `reset()` does: call it on sign-out, after
72
+ * `track('logout')`, so that event still belongs to the user leaving.
73
+ *
74
+ * - The current session ends: what it accrued goes out as a `user_engagement`
75
+ * (`trigger: 'reset'`), as when the page is closed, and the queue is sent under the visitor
76
+ * and session its events happened in.
77
+ * - A new visitor starts, and the next event opens a new session on it. Everything until the next
78
+ * sign-in is anonymous, and becomes whoever's signs in next; the visitor that is left stays the
79
+ * previous user's, with its history.
80
+ * - The third-party setters drop the user (PostHog resets its identity, GA clears its user_id).
81
+ *
82
+ * The device id and the first visit or first open stay: they describe the device, not the person.
83
+ *
84
+ * Only the sign-out this page knows of: the session and visitor are replaced while the device's
85
+ * identity (`keys.user_id` and the visitor, shared by every tab) is still the one this page last
86
+ * identified, or the page has neither identified nor signed out anyone (a host that identifies at
87
+ * sign-in only, on a page loaded since). Every tab of a site sees a sign-out and calls this,
88
+ * some of them late — a tab frozen in the background handles it on resuming, after another tab
89
+ * may have signed out and in again. A call that finds no user, or someone else's binding, is
90
+ * stale: replacing that would cut the new anonymous visitor off from the next sign-in, or end the
91
+ * next user's session and leave their pages on a visitor nobody identified. It only clears this
92
+ * page: its setters are still told, since GA holds the user per page.
93
+ *
94
+ * Synchronous and never throws: sign-out must not fail on analytics.
95
+ */
96
+ function reset() {
97
+ let current = false;
98
+ try {
99
+ const user = config.storage.getItem(keys.user_id);
100
+ current = !!user && (identified === void 0 || identified === `${visitorId()}:${user}`);
101
+ if (current) startOver();
102
+ } catch (e) {
103
+ if (e instanceof Error) console.log(e.message);
104
+ }
105
+ if (current || identified) setUsers({});
106
+ identified = null;
107
+ }
108
+ /**
109
+ * Ends the device's session and starts a new visitor with no user: `reset()` for the sign-out it
110
+ * knows of, `identify` for a user signing in over another.
111
+ */
112
+ function startOver() {
113
+ endSession();
114
+ newVisitorId();
115
+ config.storage.setItem(keys.user_id, "");
116
+ }
117
+ export { IDENTIFY_EVENT, identify, reset, visitorId };
49
118
 
50
119
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../../src/visitor/index.ts"],"sourcesContent":["import type { UpdateVisitorDTO } from '../schema/index';\nimport { config } from '../setup/index';\nimport { track } from '../track/index';\n\nexport { visitorId } from './id';\n\n/**\n * The event that binds the visitor to the user who signed in on it: the server sets the visitor's\n * `user_id` and `distinct_id` from its `user_id` property, in the transaction that stores the\n * batch — which also creates the visitor when it is new, so the binding can never arrive before it.\n */\nexport const IDENTIFY_EVENT = 'identify';\n\n/**\n * The user this page has identified the visitor as: identify once per user, not per call. Cleared\n * on sign-out, so the same user signing back in on this page is identified again.\n */\nlet identified: string | undefined;\n\n/**\n * Hands the signed-in user to the analytics: the third-party user setters (gtag, the pixels) now,\n * and the server through an `identify` event, sent when the user differs from the one this page\n * last identified — a host calls this on every page load once someone is signed in. Like any\n * event it goes with the next batch or the page-hide beacon; `user_data` is handed to the setters\n * only, never stored with the event.\n *\n * Without a `user_id` (missing, or `''`) it signs the visitor out: `setVisitor({})` is\n * sign-out. Nothing goes to the server, the setters get `distinct_id: null` (PostHog resets its\n * identity on it), and the next sign-in is identified again even if it is the same user. Still\n * `track('logout')` as usual — that is an ordinary event, and resets nothing.\n *\n * Synchronous and never throws: sign-in and sign-out must not fail on analytics.\n */\nexport function setVisitor(dto: Omit<UpdateVisitorDTO, 'tags'>) {\n if (!dto.user_id) {\n identified = undefined;\n } else if (dto.user_id !== identified) {\n identified = dto.user_id;\n track(IDENTIFY_EVENT, { user_id: dto.user_id }, { enableThirdPartyTracking: false });\n }\n\n // Once bound, the server's person key for the visitor is the user's id, so the setters are told\n // that rather than waiting for the server to say it.\n const identity = { ...dto, distinct_id: dto.user_id || null };\n config.thirdPartyUserSetters.forEach((setter) => {\n try {\n setter(identity);\n } catch (e: unknown) {\n // One third-party script does not get to stop the others.\n if (e instanceof Error) console.log(e.message);\n }\n });\n}\n"],"mappings":";;;;;;;;;AAWA,MAAa,iBAAiB;;;;;AAM9B,IAAI;;;;;;;;;;;;;;;AAgBJ,SAAgB,WAAW,KAAqC;CAC9D,IAAI,CAAC,IAAI,SACP,aAAa,KAAA;MACR,IAAI,IAAI,YAAY,YAAY;EACrC,aAAa,IAAI;EACjB,MAAM,gBAAgB,EAAE,SAAS,IAAI,QAAQ,GAAG,EAAE,0BAA0B,MAAM,CAAC;CACrF;CAIA,MAAM,WAAW;EAAE,GAAG;EAAK,aAAa,IAAI,WAAW;CAAK;CAC5D,OAAO,sBAAsB,SAAS,WAAW;EAC/C,IAAI;GACF,OAAO,QAAQ;EACjB,SAAS,GAAY;GAEnB,IAAI,aAAa,OAAO,QAAQ,IAAI,EAAE,OAAO;EAC/C;CACF,CAAC;AACH"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../src/visitor/index.ts"],"sourcesContent":["import { keys } from '../constants/storage';\nimport { config } from '../setup/index';\nimport { endSession, track } from '../track/index';\nimport type { UserProvidedData } from '../track/types';\nimport { newVisitorId, visitorId } from './id';\nimport type { ThirdPartyUserSetter } from './types';\n\nexport { visitorId } from './id';\n\n/**\n * The event that binds the visitor to the user who signed in on it: the server sets the visitor's\n * `user_id` and `distinct_id` from its `user_id` property, in the transaction that stores the\n * batch — which also creates the visitor when it is new, so the binding can never arrive before it.\n */\nexport const IDENTIFY_EVENT = 'identify';\n\n/**\n * The visitor and user this page last identified, as `<visitor>:<user>`: identify once per binding\n * per page, not per call. The visitor is part of it because the storage is shared: another tab's\n * `reset()` replaces the visitor under this page, and the same user here must then bind the new\n * one too.\n *\n * `undefined` until this page identifies anyone; `null` once it has signed out (`reset()`), so the\n * same user signing back in is identified again, and a later `reset()` here knows it has nothing\n * left to sign out — the device's identity, if any, is someone this page has not identified.\n */\nlet identified: string | null | undefined;\n\n/** Hands the user to every third-party user setter; one that throws does not stop the others. */\nfunction setUsers(user: Parameters<ThirdPartyUserSetter>[0]) {\n config.thirdPartyUserSetters.forEach((setter) => {\n try {\n setter(user);\n } catch (e: unknown) {\n if (e instanceof Error) console.log(e.message);\n }\n });\n}\n\n/**\n * Hands the signed-in user to the analytics: the third-party user setters (gtag, the pixels) now,\n * and the server through an `identify` event, sent when the user differs from the one this page\n * last identified — a host calls this on every page load once someone is signed in. Like any\n * event it goes with the next batch or the page-hide beacon; `userData` is handed to the setters\n * only, never stored with the event.\n *\n * The visitor's sessions so far — those before the sign-in included — become this user's. So a\n * different user than the one last identified on this device (an expired session, a sign-out the\n * host never reported) is `reset()` first: the previous user's visitor stays theirs, and this one\n * starts on a new visitor.\n *\n * Synchronous and never throws: sign-in must not fail on analytics.\n */\nexport function identify(userId: string, userData?: UserProvidedData) {\n let visitor = '';\n try {\n const previous = config.storage.getItem(keys.user_id);\n if (previous && previous !== userId) {\n // Another user is on the device: start over whatever this page last identified.\n startOver();\n setUsers({});\n }\n config.storage.setItem(keys.user_id, userId);\n visitor = visitorId();\n } catch (e: unknown) {\n if (e instanceof Error) console.log(e.message);\n }\n\n const binding = `${visitor}:${userId}`;\n if (binding !== identified) {\n identified = binding;\n track(IDENTIFY_EVENT, { user_id: userId }, { enableThirdPartyTracking: false });\n }\n\n setUsers({ user_id: userId, user_data: userData });\n}\n\n/**\n * Signs the visitor out, as PostHog's `reset()` does: call it on sign-out, after\n * `track('logout')`, so that event still belongs to the user leaving.\n *\n * - The current session ends: what it accrued goes out as a `user_engagement`\n * (`trigger: 'reset'`), as when the page is closed, and the queue is sent under the visitor\n * and session its events happened in.\n * - A new visitor starts, and the next event opens a new session on it. Everything until the next\n * sign-in is anonymous, and becomes whoever's signs in next; the visitor that is left stays the\n * previous user's, with its history.\n * - The third-party setters drop the user (PostHog resets its identity, GA clears its user_id).\n *\n * The device id and the first visit or first open stay: they describe the device, not the person.\n *\n * Only the sign-out this page knows of: the session and visitor are replaced while the device's\n * identity (`keys.user_id` and the visitor, shared by every tab) is still the one this page last\n * identified, or the page has neither identified nor signed out anyone (a host that identifies at\n * sign-in only, on a page loaded since). Every tab of a site sees a sign-out and calls this,\n * some of them late — a tab frozen in the background handles it on resuming, after another tab\n * may have signed out and in again. A call that finds no user, or someone else's binding, is\n * stale: replacing that would cut the new anonymous visitor off from the next sign-in, or end the\n * next user's session and leave their pages on a visitor nobody identified. It only clears this\n * page: its setters are still told, since GA holds the user per page.\n *\n * Synchronous and never throws: sign-out must not fail on analytics.\n */\nexport function reset() {\n let current = false;\n try {\n const user = config.storage.getItem(keys.user_id);\n current = !!user && (identified === undefined || identified === `${visitorId()}:${user}`);\n if (current) startOver();\n } catch (e: unknown) {\n if (e instanceof Error) console.log(e.message);\n }\n if (current || identified) setUsers({});\n identified = null;\n}\n\n/**\n * Ends the device's session and starts a new visitor with no user: `reset()` for the sign-out it\n * knows of, `identify` for a user signing in over another.\n */\nfunction startOver() {\n endSession();\n newVisitorId();\n config.storage.setItem(keys.user_id, '');\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAa,iBAAiB;;;;;;;;;;;AAY9B,IAAI;;AAGJ,SAAS,SAAS,MAA2C;CAC3D,OAAO,sBAAsB,SAAS,WAAW;EAC/C,IAAI;GACF,OAAO,IAAI;EACb,SAAS,GAAY;GACnB,IAAI,aAAa,OAAO,QAAQ,IAAI,EAAE,OAAO;EAC/C;CACF,CAAC;AACH;;;;;;;;;;;;;;;AAgBA,SAAgB,SAAS,QAAgB,UAA6B;CACpE,IAAI,UAAU;CACd,IAAI;EACF,MAAM,WAAW,OAAO,QAAQ,QAAQ,KAAK,OAAO;EACpD,IAAI,YAAY,aAAa,QAAQ;GAEnC,UAAU;GACV,SAAS,CAAC,CAAC;EACb;EACA,OAAO,QAAQ,QAAQ,KAAK,SAAS,MAAM;EAC3C,UAAU,UAAU;CACtB,SAAS,GAAY;EACnB,IAAI,aAAa,OAAO,QAAQ,IAAI,EAAE,OAAO;CAC/C;CAEA,MAAM,UAAU,GAAG,QAAQ,GAAG;CAC9B,IAAI,YAAY,YAAY;EAC1B,aAAa;EACb,MAAM,gBAAgB,EAAE,SAAS,OAAO,GAAG,EAAE,0BAA0B,MAAM,CAAC;CAChF;CAEA,SAAS;EAAE,SAAS;EAAQ,WAAW;CAAS,CAAC;AACnD;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,QAAQ;CACtB,IAAI,UAAU;CACd,IAAI;EACF,MAAM,OAAO,OAAO,QAAQ,QAAQ,KAAK,OAAO;EAChD,UAAU,CAAC,CAAC,SAAS,eAAe,KAAA,KAAa,eAAe,GAAG,UAAU,EAAE,GAAG;EAClF,IAAI,SAAS,UAAU;CACzB,SAAS,GAAY;EACnB,IAAI,aAAa,OAAO,QAAQ,IAAI,EAAE,OAAO;CAC/C;CACA,IAAI,WAAW,YAAY,SAAS,CAAC,CAAC;CACtC,aAAa;AACf;;;;;AAMA,SAAS,YAAY;CACnB,WAAW;CACX,aAAa;CACb,OAAO,QAAQ,QAAQ,KAAK,SAAS,EAAE;AACzC"}
@@ -1,24 +1,8 @@
1
- import { Environment, Platform, TrackTags } from "../track/types.cjs";
2
1
  import { UpdateVisitorDTO } from "../schema/index.cjs";
3
2
  export type VisitorProperties = Record<Lowercase<string>, string | number | boolean | null>;
4
- export interface Visitor {
5
- id: string;
6
- /**
7
- * The person this visitor belongs to, kept by the server: the visitor's own id until someone
8
- * signs in on it, the user's id from then on. What first-party attribution counts people by,
9
- * and what third parties are told to identify the visitor as. Null only from a server that
10
- * predates it.
11
- */
12
- distinct_id: string | null;
13
- device_id: string;
14
- platform: Platform;
15
- environment: Environment;
16
- tags: TrackTags;
17
- }
18
3
  /**
19
- * What a third-party user setter is handed after `setVisitor`: the payload that was sent, plus
20
- * the server's `distinct_id` for the visitor as it now stands.
4
+ * Hands the user to a third party: `identify` passes the user and the `user_data` it was given,
5
+ * `reset()` no user, which a setter reads as sign-out.
21
6
  */
22
- export type VisitorIdentity = UpdateVisitorDTO & Pick<Visitor, 'distinct_id'>;
23
- export type ThirdPartyUserSetter = (identity: VisitorIdentity) => void;
7
+ export type ThirdPartyUserSetter = (user: Omit<UpdateVisitorDTO, 'tags'>) => void;
24
8
  //# sourceMappingURL=types.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.cts","names":[],"sources":["../../src/visitor/types.ts"],"mappings":";;YAGY,oBAAoB,OAAO;iBAEtB;EACf;;;;;;;EAOA;EACA;EACA,UAAU;EACV,aAAa;EACb,MAAM;;;;;;YAOI,kBAAkB,mBAAmB,KAAK;YAE1C,wBAAwB,UAAU"}
1
+ {"version":3,"file":"types.d.cts","names":[],"sources":["../../src/visitor/types.ts"],"mappings":";YAEY,oBAAoB,OAAO;;;;;YAM3B,wBAAwB,MAAM,KAAK"}
@@ -1,24 +1,8 @@
1
- import { Environment, Platform, TrackTags } from "../track/types.mjs";
2
1
  import { UpdateVisitorDTO } from "../schema/index.mjs";
3
2
  export type VisitorProperties = Record<Lowercase<string>, string | number | boolean | null>;
4
- export interface Visitor {
5
- id: string;
6
- /**
7
- * The person this visitor belongs to, kept by the server: the visitor's own id until someone
8
- * signs in on it, the user's id from then on. What first-party attribution counts people by,
9
- * and what third parties are told to identify the visitor as. Null only from a server that
10
- * predates it.
11
- */
12
- distinct_id: string | null;
13
- device_id: string;
14
- platform: Platform;
15
- environment: Environment;
16
- tags: TrackTags;
17
- }
18
3
  /**
19
- * What a third-party user setter is handed after `setVisitor`: the payload that was sent, plus
20
- * the server's `distinct_id` for the visitor as it now stands.
4
+ * Hands the user to a third party: `identify` passes the user and the `user_data` it was given,
5
+ * `reset()` no user, which a setter reads as sign-out.
21
6
  */
22
- export type VisitorIdentity = UpdateVisitorDTO & Pick<Visitor, 'distinct_id'>;
23
- export type ThirdPartyUserSetter = (identity: VisitorIdentity) => void;
7
+ export type ThirdPartyUserSetter = (user: Omit<UpdateVisitorDTO, 'tags'>) => void;
24
8
  //# sourceMappingURL=types.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.mts","names":[],"sources":["../../src/visitor/types.ts"],"mappings":";;YAGY,oBAAoB,OAAO;iBAEtB;EACf;;;;;;;EAOA;EACA;EACA,UAAU;EACV,aAAa;EACb,MAAM;;;;;;YAOI,kBAAkB,mBAAmB,KAAK;YAE1C,wBAAwB,UAAU"}
1
+ {"version":3,"file":"types.d.mts","names":[],"sources":["../../src/visitor/types.ts"],"mappings":";YAEY,oBAAoB,OAAO;;;;;YAM3B,wBAAwB,MAAM,KAAK"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/analytics",
3
- "version": "14.1.0",
3
+ "version": "15.0.1",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",