@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.
- package/README.md +48 -33
- package/dist/constants/storage.cjs +7 -1
- package/dist/constants/storage.cjs.map +1 -1
- package/dist/constants/storage.d.cts +6 -0
- package/dist/constants/storage.d.cts.map +1 -1
- package/dist/constants/storage.d.mts +6 -0
- package/dist/constants/storage.d.mts.map +1 -1
- package/dist/constants/storage.mjs +7 -1
- package/dist/constants/storage.mjs.map +1 -1
- package/dist/index.cjs +2 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/schema/index.cjs +1 -1
- package/dist/schema/index.cjs.map +1 -1
- package/dist/schema/index.mjs +1 -1
- package/dist/schema/index.mjs.map +1 -1
- package/dist/setup/session.cjs +9 -0
- package/dist/setup/session.cjs.map +1 -1
- package/dist/setup/session.d.cts +10 -0
- package/dist/setup/session.d.cts.map +1 -1
- package/dist/setup/session.d.mts +10 -0
- package/dist/setup/session.d.mts.map +1 -1
- package/dist/setup/session.mjs +9 -0
- package/dist/setup/session.mjs.map +1 -1
- package/dist/third-parties/google-analytics.cjs.map +1 -1
- package/dist/third-parties/google-analytics.mjs.map +1 -1
- package/dist/third-parties/posthog.cjs +9 -8
- package/dist/third-parties/posthog.cjs.map +1 -1
- package/dist/third-parties/posthog.d.cts +9 -8
- package/dist/third-parties/posthog.d.cts.map +1 -1
- package/dist/third-parties/posthog.d.mts +9 -8
- package/dist/third-parties/posthog.d.mts.map +1 -1
- package/dist/third-parties/posthog.mjs +9 -8
- package/dist/third-parties/posthog.mjs.map +1 -1
- package/dist/track/gtag.cjs.map +1 -1
- package/dist/track/gtag.d.cts +2 -1
- package/dist/track/gtag.d.cts.map +1 -1
- package/dist/track/gtag.d.mts +2 -1
- package/dist/track/gtag.d.mts.map +1 -1
- package/dist/track/gtag.mjs.map +1 -1
- package/dist/track/index.cjs +117 -67
- package/dist/track/index.cjs.map +1 -1
- package/dist/track/index.d.cts +9 -2
- package/dist/track/index.d.cts.map +1 -1
- package/dist/track/index.d.mts +9 -2
- package/dist/track/index.d.mts.map +1 -1
- package/dist/track/index.mjs +117 -68
- package/dist/track/index.mjs.map +1 -1
- package/dist/visitor/id.cjs +5 -0
- package/dist/visitor/id.cjs.map +1 -1
- package/dist/visitor/id.d.cts +2 -0
- package/dist/visitor/id.d.cts.map +1 -1
- package/dist/visitor/id.d.mts +2 -0
- package/dist/visitor/id.d.mts.map +1 -1
- package/dist/visitor/id.mjs +5 -1
- package/dist/visitor/id.mjs.map +1 -1
- package/dist/visitor/index.cjs +94 -24
- package/dist/visitor/index.cjs.map +1 -1
- package/dist/visitor/index.d.cts +35 -8
- package/dist/visitor/index.d.cts.map +1 -1
- package/dist/visitor/index.d.mts +35 -8
- package/dist/visitor/index.d.mts.map +1 -1
- package/dist/visitor/index.mjs +95 -26
- package/dist/visitor/index.mjs.map +1 -1
- package/dist/visitor/types.d.cts +3 -19
- package/dist/visitor/types.d.cts.map +1 -1
- package/dist/visitor/types.d.mts +3 -19
- package/dist/visitor/types.d.mts.map +1 -1
- package/package.json +1 -1
package/dist/visitor/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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; `
|
|
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
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
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
|
|
21
|
+
* Synchronous and never throws: sign-in must not fail on analytics.
|
|
22
22
|
*/
|
|
23
|
-
export declare function
|
|
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":";;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAuCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkDpC"}
|
package/dist/visitor/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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; `
|
|
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
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
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
|
|
21
|
+
* Synchronous and never throws: sign-in must not fail on analytics.
|
|
22
22
|
*/
|
|
23
|
-
export declare function
|
|
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":";;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/visitor/index.ts"],"mappings":";;;;;;;qBAca;;;;;;;;;;;;;;;wBAuCG,SAAS,gBAAgB,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkDpC"}
|
package/dist/visitor/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { config } from "../setup/index.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
|
13
|
-
*
|
|
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; `
|
|
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
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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
|
|
45
|
+
* Synchronous and never throws: sign-in must not fail on analytics.
|
|
29
46
|
*/
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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
|
|
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/dist/visitor/types.d.cts
CHANGED
|
@@ -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
|
-
*
|
|
20
|
-
*
|
|
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
|
|
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":"
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/visitor/types.ts"],"mappings":";YAEY,oBAAoB,OAAO;;;;;YAM3B,wBAAwB,MAAM,KAAK"}
|
package/dist/visitor/types.d.mts
CHANGED
|
@@ -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
|
-
*
|
|
20
|
-
*
|
|
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
|
|
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":"
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../../src/visitor/types.ts"],"mappings":";YAEY,oBAAoB,OAAO;;;;;YAM3B,wBAAwB,MAAM,KAAK"}
|