@webiny/telemetry 0.0.0-unstable.06b2ede40f β†’ 0.0.0-unstable.0d717d18dd

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
@@ -1 +1,11 @@
1
1
  # @webiny/telemetry
2
+
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
6
+
7
+ πŸ“˜ **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
package/cli.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export declare interface SendEventParams {
2
+ event: string;
3
+ version?: string;
4
+ properties: Record<string, any>;
5
+ }
6
+
7
+ export declare function sendEvent(params: SendEventParams): Promise<void>;
8
+
9
+ export declare function isEnabled(): boolean;
10
+ export declare function enable(): boolean;
11
+ export declare function disable(): boolean;
package/cli.js CHANGED
@@ -1,15 +1,24 @@
1
- const { globalConfig } = require("@webiny/global-config");
2
- const { isCI } = require("ci-info");
3
- const { WTS } = require("wts-client/node");
4
- const baseSendEvent = require("./sendEvent");
1
+ import { globalConfig } from "@webiny/global-config";
2
+ import { isCI } from "ci-info";
3
+ import { WTS } from "@webiny/wts-client/node";
4
+ import baseSendEvent from "./sendEvent.js";
5
+ import { loadJsonFileSync } from "load-json-file";
6
+ import path from "path";
5
7
 
6
- const sendEvent = async ({ event, user, version, properties }) => {
8
+ export const sendEvent = async ({ event, version, properties }) => {
7
9
  const shouldSend = isEnabled();
8
10
  if (!shouldSend) {
9
11
  return;
10
12
  }
11
13
 
12
- const wts = new WTS();
14
+ // Use the canonical Webiny machine id β€” the top-level `id` field in
15
+ // `~/.webiny/config`, owned by @webiny/global-config. The admin app
16
+ // (REACT_APP_WEBINY_TELEMETRY_USER_ID) and the website install/finish alias
17
+ // both key off this same id, so passing it here keeps CLI, admin, and
18
+ // website events on a single PostHog person. Without it, the WTS client
19
+ // falls back to its own `user.id` field, which is a different UUID and
20
+ // fragments funnels across surfaces.
21
+ const wts = new WTS({ source: "cli", distinctId: globalConfig.get("id") });
13
22
 
14
23
  const wcpProperties = {};
15
24
  const [wcpOrgId, wcpProjectId] = getWcpOrgProjectId();
@@ -18,13 +27,22 @@ const sendEvent = async ({ event, user, version, properties }) => {
18
27
  wcpProperties.wcpProjectId = wcpProjectId;
19
28
  }
20
29
 
30
+ const installationProperties = {};
31
+ const installationId = getInstallationId();
32
+ if (installationId) {
33
+ installationProperties.installation_id = installationId;
34
+ }
35
+
36
+ const packageJsonPath = path.join(import.meta.dirname, "package.json");
37
+ const packageJson = loadJsonFileSync(packageJsonPath);
38
+
21
39
  return baseSendEvent({
22
40
  event,
23
- user: user || globalConfig.get("id"),
24
41
  properties: {
25
42
  ...properties,
26
43
  ...wcpProperties,
27
- version: version || require("./package.json").version,
44
+ ...installationProperties,
45
+ version: version || packageJson.version,
28
46
  ci: isCI,
29
47
  newUser: Boolean(globalConfig.get("newUser"))
30
48
  },
@@ -33,23 +51,39 @@ const sendEvent = async ({ event, user, version, properties }) => {
33
51
  };
34
52
 
35
53
  const getWcpOrgProjectId = () => {
36
- // In CLI, WCP project ID is stored in the `WCP_PROJECT_ID` environment variable.
37
- const id = process.env.WCP_PROJECT_ID;
54
+ // In CLI, project ID is stored in the `WEBINY_PROJECT_ID` or `WCP_PROJECT_ID` environment variable.
55
+ const id = process.env.WEBINY_PROJECT_ID || process.env.WCP_PROJECT_ID;
38
56
  if (typeof id === "string") {
39
57
  return id.split("/");
40
58
  }
41
59
  return [];
42
60
  };
43
61
 
44
- const enable = () => {
62
+ /**
63
+ * Reads the anonymous per-project installation id from
64
+ * `<project-root>/package.json` β†’ `webiny.installationId`. Generated once at
65
+ * `create-webiny-project` time and tracked in git so it stays stable across
66
+ * machines that share the project. Returns null if the file is missing or
67
+ * unreadable β€” telemetry events still fire, just without the property.
68
+ */
69
+ const getInstallationId = () => {
70
+ try {
71
+ const data = loadJsonFileSync(path.join(process.cwd(), "package.json"));
72
+ return typeof data?.webiny?.installationId === "string" ? data.webiny.installationId : null;
73
+ } catch {
74
+ return null;
75
+ }
76
+ };
77
+
78
+ export const enable = () => {
45
79
  globalConfig.set("telemetry", true);
46
80
  };
47
81
 
48
- const disable = () => {
82
+ export const disable = () => {
49
83
  globalConfig.set("telemetry", false);
50
84
  };
51
85
 
52
- const isEnabled = () => {
86
+ export const isEnabled = () => {
53
87
  const config = globalConfig.get();
54
88
 
55
89
  if (config.telemetry === false) {
@@ -59,5 +93,3 @@ const isEnabled = () => {
59
93
  // `tracking` is left here for backwards compatibility with previous versions of Webiny.
60
94
  return config.tracking !== false;
61
95
  };
62
-
63
- module.exports = { sendEvent, enable, disable, isEnabled };
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@webiny/telemetry",
3
- "version": "0.0.0-unstable.06b2ede40f",
3
+ "version": "0.0.0-unstable.0d717d18dd",
4
+ "type": "module",
4
5
  "license": "MIT",
5
6
  "dependencies": {
6
- "@webiny/global-config": "0.0.0-unstable.06b2ede40f",
7
- "ci-info": "4.2.0",
8
- "jsesc": "3.0.2",
9
- "strip-ansi": "6.0.1",
10
- "wts-client": "1.0.1"
7
+ "@webiny/global-config": "0.0.0-unstable.0d717d18dd",
8
+ "@webiny/wts-client": "3.1.4",
9
+ "ci-info": "4.4.0",
10
+ "jsesc": "3.1.0",
11
+ "load-json-file": "7.0.1",
12
+ "strip-ansi": "7.2.0"
11
13
  },
12
14
  "publishConfig": {
13
- "access": "public",
14
- "directory": "."
15
- },
16
- "gitHead": "06b2ede40fc2212a70eeafd74afd50b56fb0ce82"
15
+ "access": "public"
16
+ }
17
17
  }
package/react.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export declare function sendEvent(ev: string, properties?: Record<string, any>): Promise<any>;
2
+ export declare function getMachineId(): string | null;
package/react.js CHANGED
@@ -1,13 +1,95 @@
1
- const baseSendEvent = require("./sendEvent");
2
- const { WTS } = require("wts-client/admin");
1
+ import baseSendEvent from "./sendEvent.js";
2
+ import { WTS } from "@webiny/wts-client/web";
3
3
 
4
- const sendEvent = async (event, properties = {}) => {
4
+ const STORAGE_MACHINE_ID = "wts_machine_id";
5
+ const STORAGE_PROJECT_ID = "wts_project_id";
6
+
7
+ let wtsInstance = null;
8
+ let projectId = null;
9
+ let distinctId = null;
10
+
11
+ /**
12
+ * Resolves the WTS client identity for the admin app.
13
+ *
14
+ * Priority for `distinct_id` (machine_id):
15
+ * 1. URL param `wts_did` on first load. Persisted to localStorage.
16
+ * 2. localStorage (subsequent loads).
17
+ * 3. `process.env.REACT_APP_WEBINY_TELEMETRY_USER_ID` (build-time fallback,
18
+ * set by `SetAdminAppEnvVarsBefore{Build,Watch}` from `~/.webiny/config`).
19
+ *
20
+ * Priority for `project_id` (installation_id):
21
+ * 1. URL param `iid` on first load. Persisted to localStorage.
22
+ * 2. localStorage.
23
+ * 3. `process.env.REACT_APP_WEBINY_INSTALLATION_ID` (build-time fallback,
24
+ * set from `<project>/package.json` β†’ `webiny.installationId`).
25
+ *
26
+ * Attached as a super-property on every admin event so PostHog funnels can
27
+ * group per-install.
28
+ */
29
+ const initWts = () => {
30
+ if (wtsInstance) {
31
+ return wtsInstance;
32
+ }
33
+
34
+ distinctId = process.env.REACT_APP_WEBINY_TELEMETRY_USER_ID;
35
+ projectId = process.env.REACT_APP_WEBINY_INSTALLATION_ID || null;
36
+
37
+ if (typeof window !== "undefined") {
38
+ const params = new URLSearchParams(window.location.search);
39
+
40
+ const fromUrl = params.get("wts_did");
41
+ if (fromUrl) {
42
+ distinctId = fromUrl;
43
+ try {
44
+ window.localStorage.setItem(STORAGE_MACHINE_ID, fromUrl);
45
+ } catch {
46
+ // localStorage unavailable; URL value is used for this session only.
47
+ }
48
+ } else {
49
+ try {
50
+ distinctId = window.localStorage.getItem(STORAGE_MACHINE_ID) || distinctId;
51
+ } catch {
52
+ // ignore
53
+ }
54
+ }
55
+
56
+ const iidFromUrl = params.get("iid");
57
+ if (iidFromUrl) {
58
+ projectId = iidFromUrl;
59
+ try {
60
+ window.localStorage.setItem(STORAGE_PROJECT_ID, iidFromUrl);
61
+ } catch {
62
+ // ignore
63
+ }
64
+ } else {
65
+ try {
66
+ projectId = window.localStorage.getItem(STORAGE_PROJECT_ID) || projectId;
67
+ } catch {
68
+ // env-var value (set above) is used as fallback.
69
+ }
70
+ }
71
+ }
72
+
73
+ wtsInstance = new WTS({ source: "admin", distinctId });
74
+ return wtsInstance;
75
+ };
76
+
77
+ /**
78
+ * Returns the machine_id used by admin events, if known. Used by the
79
+ * install/finish CTA to construct the alias handoff URL.
80
+ */
81
+ export const getMachineId = () => {
82
+ initWts();
83
+ return distinctId || null;
84
+ };
85
+
86
+ export const sendEvent = async (event, properties = {}) => {
5
87
  const shouldSend = process.env.REACT_APP_WEBINY_TELEMETRY !== "false";
6
88
  if (!shouldSend) {
7
89
  return;
8
90
  }
9
91
 
10
- const wts = new WTS();
92
+ const wts = initWts();
11
93
 
12
94
  const wcpProperties = {};
13
95
  const [wcpOrgId, wcpProjectId] = getWcpOrgProjectId();
@@ -18,10 +100,10 @@ const sendEvent = async (event, properties = {}) => {
18
100
 
19
101
  return baseSendEvent({
20
102
  event,
21
- user: process.env.REACT_APP_WEBINY_TELEMETRY_USER_ID,
22
103
  properties: {
23
104
  ...properties,
24
105
  ...wcpProperties,
106
+ ...(projectId ? { project_id: projectId } : {}),
25
107
  version: process.env.REACT_APP_WEBINY_VERSION,
26
108
  ci: process.env.REACT_APP_IS_CI === "true",
27
109
  newUser: process.env.REACT_APP_WEBINY_TELEMETRY_NEW_USER === "true"
@@ -31,12 +113,10 @@ const sendEvent = async (event, properties = {}) => {
31
113
  };
32
114
 
33
115
  const getWcpOrgProjectId = () => {
34
- // In React applications, WCP project ID is stored in the `REACT_APP_WCP_PROJECT_ID` environment variable.
35
- const id = process.env.REACT_APP_WCP_PROJECT_ID;
116
+ // In React applications, project ID is stored in the `REACT_APP_WEBINY_PROJECT_ID` or `REACT_APP_WCP_PROJECT_ID` environment variable.
117
+ const id = process.env.REACT_APP_WEBINY_PROJECT_ID || process.env.REACT_APP_WCP_PROJECT_ID;
36
118
  if (typeof id === "string") {
37
119
  return id.split("/");
38
120
  }
39
121
  return [];
40
122
  };
41
-
42
- module.exports = { sendEvent };
package/sendEvent.js CHANGED
@@ -1,19 +1,19 @@
1
- const stripAnsi = require("strip-ansi");
2
- const jsesc = require("jsesc");
1
+ import stripAnsi from "strip-ansi";
2
+ import jsesc from "jsesc";
3
+
3
4
  /**
4
5
  * The main `sendEvent` function.
5
6
  * NOTE: don't use this in your app directly. Instead, use the one from `cli.js` or `react.js` files accordingly.
7
+ *
8
+ * Identity is owned by the WTS instance β€” `cli.js` reads `~/.webiny/config`,
9
+ * `react.js` reads URL params / localStorage / env. This function only
10
+ * validates and sanitises the event payload before dispatching.
6
11
  */
7
- module.exports = ({ event, user, properties, wts } = {}) => {
8
- // 1. Check for existence of required base parameters.
12
+ export default ({ event, properties, wts } = {}) => {
9
13
  if (!event) {
10
14
  throw new Error(`Cannot send event - missing "event" name.`);
11
15
  }
12
16
 
13
- if (!user) {
14
- throw new Error(`Cannot send event - missing "user" ID.`);
15
- }
16
-
17
17
  if (!properties) {
18
18
  throw new Error(`Cannot send event - missing "properties" object.`);
19
19
  }
@@ -22,7 +22,6 @@ module.exports = ({ event, user, properties, wts } = {}) => {
22
22
  throw new Error(`Cannot send event - missing "wts" instance.`);
23
23
  }
24
24
 
25
- // 2. Extract properties and check for existence of required properties.
26
25
  if (!properties.version) {
27
26
  throw new Error(`Cannot send event - missing "version" property.`);
28
27
  }
@@ -37,7 +36,6 @@ module.exports = ({ event, user, properties, wts } = {}) => {
37
36
  throw new Error(`Cannot send event - missing "newUser" boolean property.`);
38
37
  }
39
38
 
40
- // 2. Sanitize properties.
41
39
  const sanitizedProperties = {
42
40
  ...properties,
43
41
  newUser: properties.newUser === true ? "yes" : "no",
@@ -55,6 +53,5 @@ module.exports = ({ event, user, properties, wts } = {}) => {
55
53
  sanitizedProperties[key] = sanitizedValue;
56
54
  }
57
55
 
58
- // 3. Send.
59
- return wts.trackEvent(user, event, sanitizedProperties);
56
+ return wts.track(event, sanitizedProperties);
60
57
  };