@shware/analytics 15.0.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.
package/README.md CHANGED
@@ -159,7 +159,7 @@ async function logout() {
159
159
  }
160
160
  ```
161
161
 
162
- `reset()` is sign-out, as in PostHog: the current session ends (its unreported engagement goes out as a `user_engagement` with `trigger: 'reset'`), a new visitor starts on the device, and the third-party setters drop the user. What happens until the next sign-in is anonymous and belongs to whoever signs in next; the visitor left behind keeps the previous user's history. Call it when the session ends any other way too (expiry, deletion), and from every tab that notices: it replaces the visitor once per sign-out, so a second tab calling it does not cut the new anonymous visitor off. `identify` resets by itself when a different user signs in than the one it last identified on the device, so a sign-out the host missed does not hand one user's visitor to the next. The device id and the first visit or first open are the device's and stay.
162
+ `reset()` is sign-out, as in PostHog: the current session ends (its unreported engagement goes out as a `user_engagement` with `trigger: 'reset'`), a new visitor starts on the device, and the third-party setters drop the user. What happens until the next sign-in is anonymous and belongs to whoever signs in next; the visitor left behind keeps the previous user's history. Call it when the session ends any other way too (expiry, deletion), and from every tab that notices, however late: it only replaces the visitor while the device's identity is still the one that page identified, so a second tab calling it does not cut the new anonymous visitor off, nor reset the next user who has signed in since. `identify` resets by itself when a different user signs in than the one it last identified on the device, so a sign-out the host missed does not hand one user's visitor to the next. The device id and the first visit or first open are the device's and stay.
163
163
 
164
164
  ### Typed events and strict event names
165
165
 
@@ -14,7 +14,11 @@ const IDENTIFY_EVENT = "identify";
14
14
  * The visitor and user this page last identified, as `<visitor>:<user>`: identify once per binding
15
15
  * per page, not per call. The visitor is part of it because the storage is shared: another tab's
16
16
  * `reset()` replaces the visitor under this page, and the same user here must then bind the new
17
- * one too. Cleared by `reset()`, so the same user signing back in is identified again.
17
+ * one too.
18
+ *
19
+ * `undefined` until this page identifies anyone; `null` once it has signed out (`reset()`), so the
20
+ * same user signing back in is identified again, and a later `reset()` here knows it has nothing
21
+ * left to sign out — the device's identity, if any, is someone this page has not identified.
18
22
  */
19
23
  let identified;
20
24
  /** Hands the user to every third-party user setter; one that throws does not stop the others. */
@@ -45,7 +49,10 @@ function identify(userId, userData) {
45
49
  let visitor = "";
46
50
  try {
47
51
  const previous = require_setup_index.config.storage.getItem(require_constants_storage.keys.user_id);
48
- if (previous && previous !== userId) reset();
52
+ if (previous && previous !== userId) {
53
+ startOver();
54
+ setUsers({});
55
+ }
49
56
  require_setup_index.config.storage.setItem(require_constants_storage.keys.user_id, userId);
50
57
  visitor = require_visitor_id.visitorId();
51
58
  } catch (e) {
@@ -75,29 +82,38 @@ function identify(userId, userData) {
75
82
  *
76
83
  * The device id and the first visit or first open stay: they describe the device, not the person.
77
84
  *
78
- * Once per sign-out, however many times it is called: the session and visitor are only replaced
79
- * while a user is identified on the device (`keys.user_id`, which `identify` writes on every page
80
- * load and this clears). Every tab of a site sees the sign-out and calls this, and the storage is
81
- * shared: a second call would cut the anonymous visitor the first one started off from the next
82
- * sign-in, and end its session early. A page that identified someone still has its setters told,
83
- * since GA holds the user per page.
85
+ * Only the sign-out this page knows of: the session and visitor are replaced while the device's
86
+ * identity (`keys.user_id` and the visitor, shared by every tab) is still the one this page last
87
+ * identified, or the page has neither identified nor signed out anyone (a host that identifies at
88
+ * sign-in only, on a page loaded since). Every tab of a site sees a sign-out and calls this,
89
+ * some of them late — a tab frozen in the background handles it on resuming, after another tab
90
+ * may have signed out and in again. A call that finds no user, or someone else's binding, is
91
+ * stale: replacing that would cut the new anonymous visitor off from the next sign-in, or end the
92
+ * next user's session and leave their pages on a visitor nobody identified. It only clears this
93
+ * page: its setters are still told, since GA holds the user per page.
84
94
  *
85
95
  * Synchronous and never throws: sign-out must not fail on analytics.
86
96
  */
87
97
  function reset() {
88
- let signedIn = false;
98
+ let current = false;
89
99
  try {
90
- signedIn = !!require_setup_index.config.storage.getItem(require_constants_storage.keys.user_id);
91
- if (signedIn) {
92
- require_track_index.endSession();
93
- require_visitor_id.newVisitorId();
94
- require_setup_index.config.storage.setItem(require_constants_storage.keys.user_id, "");
95
- }
100
+ const user = require_setup_index.config.storage.getItem(require_constants_storage.keys.user_id);
101
+ current = !!user && (identified === void 0 || identified === `${require_visitor_id.visitorId()}:${user}`);
102
+ if (current) startOver();
96
103
  } catch (e) {
97
104
  if (e instanceof Error) console.log(e.message);
98
105
  }
99
- if (signedIn || identified) setUsers({});
100
- identified = void 0;
106
+ if (current || identified) setUsers({});
107
+ identified = null;
108
+ }
109
+ /**
110
+ * Ends the device's session and starts a new visitor with no user: `reset()` for the sign-out it
111
+ * knows of, `identify` for a user signing in over another.
112
+ */
113
+ function startOver() {
114
+ require_track_index.endSession();
115
+ require_visitor_id.newVisitorId();
116
+ require_setup_index.config.storage.setItem(require_constants_storage.keys.user_id, "");
101
117
  }
102
118
  exports.IDENTIFY_EVENT = IDENTIFY_EVENT;
103
119
  exports.identify = identify;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["config","keys","visitorId"],"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. Cleared by `reset()`, so the same user signing back in is identified again.\n */\nlet identified: string | 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) reset();\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 * Once per sign-out, however many times it is called: the session and visitor are only replaced\n * while a user is identified on the device (`keys.user_id`, which `identify` writes on every page\n * load and this clears). Every tab of a site sees the sign-out and calls this, and the storage is\n * shared: a second call would cut the anonymous visitor the first one started off from the next\n * sign-in, and end its session early. A page that identified someone still has its setters told,\n * 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 signedIn = false;\n try {\n signedIn = !!config.storage.getItem(keys.user_id);\n if (signedIn) {\n endSession();\n newVisitorId();\n config.storage.setItem(keys.user_id, '');\n }\n } catch (e: unknown) {\n if (e instanceof Error) console.log(e.message);\n }\n if (signedIn || identified) setUsers({});\n identified = undefined;\n}\n"],"mappings":";;;;;;;;;;;AAcA,MAAa,iBAAiB;;;;;;;AAQ9B,IAAI;;AAGJ,SAAS,SAAS,MAA2C;CAC3D,oBAAA,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,WAAWA,oBAAAA,OAAO,QAAQ,QAAQC,0BAAAA,KAAK,OAAO;EACpD,IAAI,YAAY,aAAa,QAAQ,MAAM;EAC3C,oBAAA,OAAO,QAAQ,QAAQA,0BAAAA,KAAK,SAAS,MAAM;EAC3C,UAAUC,mBAAAA,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,oBAAA,MAAM,gBAAgB,EAAE,SAAS,OAAO,GAAG,EAAE,0BAA0B,MAAM,CAAC;CAChF;CAEA,SAAS;EAAE,SAAS;EAAQ,WAAW;CAAS,CAAC;AACnD;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,QAAQ;CACtB,IAAI,WAAW;CACf,IAAI;EACF,WAAW,CAAC,CAACF,oBAAAA,OAAO,QAAQ,QAAQC,0BAAAA,KAAK,OAAO;EAChD,IAAI,UAAU;GACZ,oBAAA,WAAW;GACX,mBAAA,aAAa;GACb,oBAAA,OAAO,QAAQ,QAAQA,0BAAAA,KAAK,SAAS,EAAE;EACzC;CACF,SAAS,GAAY;EACnB,IAAI,aAAa,OAAO,QAAQ,IAAI,EAAE,OAAO;CAC/C;CACA,IAAI,YAAY,YAAY,SAAS,CAAC,CAAC;CACvC,aAAa,KAAA;AACf"}
1
+ {"version":3,"file":"index.cjs","names":["config","keys","visitorId"],"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,oBAAA,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,WAAWA,oBAAAA,OAAO,QAAQ,QAAQC,0BAAAA,KAAK,OAAO;EACpD,IAAI,YAAY,aAAa,QAAQ;GAEnC,UAAU;GACV,SAAS,CAAC,CAAC;EACb;EACA,oBAAA,OAAO,QAAQ,QAAQA,0BAAAA,KAAK,SAAS,MAAM;EAC3C,UAAUC,mBAAAA,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,oBAAA,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,OAAOF,oBAAAA,OAAO,QAAQ,QAAQC,0BAAAA,KAAK,OAAO;EAChD,UAAU,CAAC,CAAC,SAAS,eAAe,KAAA,KAAa,eAAe,GAAGC,mBAAAA,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,oBAAA,WAAW;CACX,mBAAA,aAAa;CACb,oBAAA,OAAO,QAAQ,QAAQD,0BAAAA,KAAK,SAAS,EAAE;AACzC"}
@@ -35,12 +35,15 @@ export declare function identify(userId: string, userData?: UserProvidedData): v
35
35
  *
36
36
  * The device id and the first visit or first open stay: they describe the device, not the person.
37
37
  *
38
- * Once per sign-out, however many times it is called: the session and visitor are only replaced
39
- * while a user is identified on the device (`keys.user_id`, which `identify` writes on every page
40
- * load and this clears). Every tab of a site sees the sign-out and calls this, and the storage is
41
- * shared: a second call would cut the anonymous visitor the first one started off from the next
42
- * sign-in, and end its session early. A page that identified someone still has its setters told,
43
- * since GA holds the user per page.
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.
44
47
  *
45
48
  * Synchronous and never throws: sign-out must not fail on analytics.
46
49
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAmCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;wBA2CpC"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAuCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkDpC"}
@@ -35,12 +35,15 @@ export declare function identify(userId: string, userData?: UserProvidedData): v
35
35
  *
36
36
  * The device id and the first visit or first open stay: they describe the device, not the person.
37
37
  *
38
- * Once per sign-out, however many times it is called: the session and visitor are only replaced
39
- * while a user is identified on the device (`keys.user_id`, which `identify` writes on every page
40
- * load and this clears). Every tab of a site sees the sign-out and calls this, and the storage is
41
- * shared: a second call would cut the anonymous visitor the first one started off from the next
42
- * sign-in, and end its session early. A page that identified someone still has its setters told,
43
- * since GA holds the user per page.
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.
44
47
  *
45
48
  * Synchronous and never throws: sign-out must not fail on analytics.
46
49
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAmCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;wBA2CpC"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAuCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkDpC"}
@@ -13,7 +13,11 @@ const IDENTIFY_EVENT = "identify";
13
13
  * The visitor and user this page last identified, as `<visitor>:<user>`: identify once per binding
14
14
  * per page, not per call. The visitor is part of it because the storage is shared: another tab's
15
15
  * `reset()` replaces the visitor under this page, and the same user here must then bind the new
16
- * one too. Cleared by `reset()`, so the same user signing back in is identified again.
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.
17
21
  */
18
22
  let identified;
19
23
  /** Hands the user to every third-party user setter; one that throws does not stop the others. */
@@ -44,7 +48,10 @@ function identify(userId, userData) {
44
48
  let visitor = "";
45
49
  try {
46
50
  const previous = config.storage.getItem(keys.user_id);
47
- if (previous && previous !== userId) reset();
51
+ if (previous && previous !== userId) {
52
+ startOver();
53
+ setUsers({});
54
+ }
48
55
  config.storage.setItem(keys.user_id, userId);
49
56
  visitor = visitorId();
50
57
  } catch (e) {
@@ -74,29 +81,38 @@ function identify(userId, userData) {
74
81
  *
75
82
  * The device id and the first visit or first open stay: they describe the device, not the person.
76
83
  *
77
- * Once per sign-out, however many times it is called: the session and visitor are only replaced
78
- * while a user is identified on the device (`keys.user_id`, which `identify` writes on every page
79
- * load and this clears). Every tab of a site sees the sign-out and calls this, and the storage is
80
- * shared: a second call would cut the anonymous visitor the first one started off from the next
81
- * sign-in, and end its session early. A page that identified someone still has its setters told,
82
- * since GA holds the user per page.
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.
83
93
  *
84
94
  * Synchronous and never throws: sign-out must not fail on analytics.
85
95
  */
86
96
  function reset() {
87
- let signedIn = false;
97
+ let current = false;
88
98
  try {
89
- signedIn = !!config.storage.getItem(keys.user_id);
90
- if (signedIn) {
91
- endSession();
92
- newVisitorId();
93
- config.storage.setItem(keys.user_id, "");
94
- }
99
+ const user = config.storage.getItem(keys.user_id);
100
+ current = !!user && (identified === void 0 || identified === `${visitorId()}:${user}`);
101
+ if (current) startOver();
95
102
  } catch (e) {
96
103
  if (e instanceof Error) console.log(e.message);
97
104
  }
98
- if (signedIn || identified) setUsers({});
99
- identified = void 0;
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, "");
100
116
  }
101
117
  export { IDENTIFY_EVENT, identify, reset, visitorId };
102
118
 
@@ -1 +1 @@
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. Cleared by `reset()`, so the same user signing back in is identified again.\n */\nlet identified: string | 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) reset();\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 * Once per sign-out, however many times it is called: the session and visitor are only replaced\n * while a user is identified on the device (`keys.user_id`, which `identify` writes on every page\n * load and this clears). Every tab of a site sees the sign-out and calls this, and the storage is\n * shared: a second call would cut the anonymous visitor the first one started off from the next\n * sign-in, and end its session early. A page that identified someone still has its setters told,\n * 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 signedIn = false;\n try {\n signedIn = !!config.storage.getItem(keys.user_id);\n if (signedIn) {\n endSession();\n newVisitorId();\n config.storage.setItem(keys.user_id, '');\n }\n } catch (e: unknown) {\n if (e instanceof Error) console.log(e.message);\n }\n if (signedIn || identified) setUsers({});\n identified = undefined;\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAa,iBAAiB;;;;;;;AAQ9B,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,MAAM;EAC3C,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;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,QAAQ;CACtB,IAAI,WAAW;CACf,IAAI;EACF,WAAW,CAAC,CAAC,OAAO,QAAQ,QAAQ,KAAK,OAAO;EAChD,IAAI,UAAU;GACZ,WAAW;GACX,aAAa;GACb,OAAO,QAAQ,QAAQ,KAAK,SAAS,EAAE;EACzC;CACF,SAAS,GAAY;EACnB,IAAI,aAAa,OAAO,QAAQ,IAAI,EAAE,OAAO;CAC/C;CACA,IAAI,YAAY,YAAY,SAAS,CAAC,CAAC;CACvC,aAAa,KAAA;AACf"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/analytics",
3
- "version": "15.0.0",
3
+ "version": "15.0.1",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",