@webiny/telemetry 0.0.0-unstable.ecd8734205 β†’ 0.0.0-unstable.f6dc066313

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,12 @@
1
+ export declare interface SendEventParams {
2
+ event: string;
3
+ user?: string;
4
+ version?: string;
5
+ properties: Record<string, any>;
6
+ }
7
+
8
+ export declare function sendEvent(params: SendEventParams): Promise<void>;
9
+
10
+ export declare function isEnabled(): boolean;
11
+ export declare function enable(): boolean;
12
+ export declare function disable(): boolean;
package/cli.js CHANGED
@@ -1,40 +1,60 @@
1
- const createSendEvent = require("./sendEvent");
2
- const { globalConfig } = require("@webiny/global-config");
1
+ import { globalConfig } from "@webiny/global-config";
2
+ import { isCI } from "ci-info";
3
+ import { WTS } from "wts-client/node.js";
4
+ import baseSendEvent from "./sendEvent.js";
5
+ import { loadJsonFileSync } from "load-json-file";
6
+ import path from "path";
3
7
 
4
- const sendEvent = ({ event, user, version, properties, extraPayload }) => {
8
+ export const sendEvent = async ({ event, user, version, properties }) => {
5
9
  const shouldSend = isEnabled();
10
+ if (!shouldSend) {
11
+ return;
12
+ }
13
+
14
+ const wts = new WTS();
15
+
16
+ const wcpProperties = {};
17
+ const [wcpOrgId, wcpProjectId] = getWcpOrgProjectId();
18
+ if (wcpOrgId && wcpProjectId) {
19
+ wcpProperties.wcpOrgId = wcpOrgId;
20
+ wcpProperties.wcpProjectId = wcpProjectId;
21
+ }
6
22
 
7
- try {
8
- const sendTelemetry = createSendEvent({
9
- event,
10
- user: user || globalConfig.get("id"),
11
- version: version || require("./package.json").version,
12
- properties,
13
- extraPayload
14
- });
15
-
16
- if (shouldSend) {
17
- return sendTelemetry();
18
- }
19
- } catch (err) {
20
- // Ignore errors if telemetry is disabled.
21
- if (!shouldSend) {
22
- return;
23
- }
24
-
25
- throw err;
23
+ const packageJsonPath = path.join(import.meta.dirname, "package.json");
24
+ const packageJson = loadJsonFileSync(packageJsonPath);
25
+
26
+ return baseSendEvent({
27
+ event,
28
+ user: user || globalConfig.get("id"),
29
+ properties: {
30
+ ...properties,
31
+ ...wcpProperties,
32
+ version: version || packageJson.version,
33
+ ci: isCI,
34
+ newUser: Boolean(globalConfig.get("newUser"))
35
+ },
36
+ wts
37
+ });
38
+ };
39
+
40
+ const getWcpOrgProjectId = () => {
41
+ // In CLI, WCP project ID is stored in the `WCP_PROJECT_ID` environment variable.
42
+ const id = process.env.WCP_PROJECT_ID;
43
+ if (typeof id === "string") {
44
+ return id.split("/");
26
45
  }
46
+ return [];
27
47
  };
28
48
 
29
- const enable = () => {
49
+ export const enable = () => {
30
50
  globalConfig.set("telemetry", true);
31
51
  };
32
52
 
33
- const disable = () => {
53
+ export const disable = () => {
34
54
  globalConfig.set("telemetry", false);
35
55
  };
36
56
 
37
- const isEnabled = () => {
57
+ export const isEnabled = () => {
38
58
  const config = globalConfig.get();
39
59
 
40
60
  if (config.telemetry === false) {
@@ -44,5 +64,3 @@ const isEnabled = () => {
44
64
  // `tracking` is left here for backwards compatibility with previous versions of Webiny.
45
65
  return config.tracking !== false;
46
66
  };
47
-
48
- module.exports = { sendEvent, enable, disable, isEnabled };
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@webiny/telemetry",
3
- "version": "0.0.0-unstable.ecd8734205",
3
+ "version": "0.0.0-unstable.f6dc066313",
4
+ "type": "module",
4
5
  "license": "MIT",
5
6
  "dependencies": {
6
- "@webiny/global-config": "0.0.0-unstable.ecd8734205",
7
- "form-data": "3.0.0",
8
- "node-fetch": "2.6.1"
7
+ "@webiny/global-config": "0.0.0-unstable.f6dc066313",
8
+ "ci-info": "4.4.0",
9
+ "jsesc": "3.1.0",
10
+ "load-json-file": "7.0.1",
11
+ "strip-ansi": "6.0.1",
12
+ "wts-client": "2.0.0"
9
13
  },
10
14
  "publishConfig": {
11
15
  "access": "public",
12
16
  "directory": "."
13
17
  },
14
- "gitHead": "ecd8734205e0e21ae04076c28ff9806dad07a730"
18
+ "gitHead": "f6dc066313ddce5339d2aacec3aa84e61232689b"
15
19
  }
package/react.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function sendEvent(ev: string, properties?: Record<string, any>): Promise<any>;
package/react.js CHANGED
@@ -1,36 +1,40 @@
1
- const createSendEvent = require("./sendEvent");
1
+ import baseSendEvent from "./sendEvent.js";
2
+ import { WTS } from "wts-client/admin.js";
2
3
 
3
- const setProperties = data => {
4
- return sendEvent("$identify", data);
5
- };
6
- /**
7
- *
8
- * @param event {String}
9
- * @param data {Record<string, string>}
10
- * @return {Promise<T>}
11
- */
12
- const sendEvent = (event, data = {}) => {
13
- let properties = {};
14
- let extraPayload = {};
15
- if (event !== "$identify") {
16
- properties = data;
17
- } else {
18
- extraPayload = {
19
- $set: data
20
- };
4
+ export const sendEvent = async (event, properties = {}) => {
5
+ const shouldSend = process.env.REACT_APP_WEBINY_TELEMETRY !== "false";
6
+ if (!shouldSend) {
7
+ return;
21
8
  }
22
9
 
23
- const shouldSend = process.env.REACT_APP_WEBINY_TELEMETRY !== "false";
10
+ const wts = new WTS();
11
+
12
+ const wcpProperties = {};
13
+ const [wcpOrgId, wcpProjectId] = getWcpOrgProjectId();
14
+ if (wcpOrgId && wcpProjectId) {
15
+ wcpProperties.wcpOrgId = wcpOrgId;
16
+ wcpProperties.wcpProjectId = wcpProjectId;
17
+ }
24
18
 
25
- const sendTelemetry = createSendEvent({
19
+ return baseSendEvent({
26
20
  event,
27
- properties,
28
- extraPayload,
29
- user: process.env.REACT_APP_USER_ID,
30
- version: process.env.REACT_APP_WEBINY_VERSION
21
+ user: process.env.REACT_APP_WEBINY_TELEMETRY_USER_ID,
22
+ properties: {
23
+ ...properties,
24
+ ...wcpProperties,
25
+ version: process.env.REACT_APP_WEBINY_VERSION,
26
+ ci: process.env.REACT_APP_IS_CI === "true",
27
+ newUser: process.env.REACT_APP_WEBINY_TELEMETRY_NEW_USER === "true"
28
+ },
29
+ wts
31
30
  });
32
-
33
- return shouldSend ? sendTelemetry() : Promise.resolve();
34
31
  };
35
32
 
36
- module.exports = { setProperties, sendEvent };
33
+ 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;
36
+ if (typeof id === "string") {
37
+ return id.split("/");
38
+ }
39
+ return [];
40
+ };
package/sendEvent.js CHANGED
@@ -1,56 +1,61 @@
1
- const FormData = require("form-data");
2
- const fetch = require("node-fetch");
3
-
4
- const API_KEY = "ZdDZgkeOt4Z_m-UWmqFsE1d6-kcCK3BH0ypYTUIFty4";
5
- const API_URL = "https://t.webiny.com";
1
+ import stripAnsi from "strip-ansi";
2
+ import jsesc from "jsesc";
6
3
 
7
4
  /**
8
5
  * The main `sendEvent` function.
9
6
  * NOTE: don't use this in your app directly. Instead, use the one from `cli.js` or `react.js` files accordingly.
10
7
  */
11
- module.exports = ({ event, user, version, properties, extraPayload } = {}) => {
8
+ export default ({ event, user, properties, wts } = {}) => {
9
+ // 1. Check for the existence of required base parameters.
12
10
  if (!event) {
13
11
  throw new Error(`Cannot send event - missing "event" name.`);
14
12
  }
15
13
 
16
14
  if (!user) {
17
- throw new Error(`Cannot send event - missing "user" property.`);
15
+ throw new Error(`Cannot send event - missing "user" ID.`);
16
+ }
17
+
18
+ if (!properties) {
19
+ throw new Error(`Cannot send event - missing "properties" object.`);
20
+ }
21
+
22
+ if (!wts) {
23
+ throw new Error(`Cannot send event - missing "wts" instance.`);
18
24
  }
19
25
 
20
- if (!version) {
26
+ // 2. Extract properties and check for existence of required properties.
27
+ if (!properties.version) {
21
28
  throw new Error(`Cannot send event - missing "version" property.`);
22
29
  }
23
30
 
24
- if (!properties) {
25
- properties = {};
31
+ const hasCiProp = "ci" in properties;
32
+ if (!hasCiProp) {
33
+ throw new Error(`Cannot send event - missing "ci" boolean property.`);
26
34
  }
27
35
 
28
- if (!extraPayload) {
29
- extraPayload = {};
36
+ const hasNewUserProp = "newUser" in properties;
37
+ if (!hasNewUserProp) {
38
+ throw new Error(`Cannot send event - missing "newUser" boolean property.`);
30
39
  }
31
40
 
32
- const payload = {
33
- ...extraPayload,
34
- event,
35
- properties: {
36
- ...properties,
37
- version
38
- },
39
- distinct_id: user,
40
- api_key: API_KEY,
41
- timestamp: new Date().toISOString()
41
+ // 2. Sanitize properties.
42
+ const sanitizedProperties = {
43
+ ...properties,
44
+ newUser: properties.newUser === true ? "yes" : "no",
45
+ ci: properties.ci === true ? "yes" : "no"
42
46
  };
43
47
 
44
- const body = new FormData();
45
- body.append("data", Buffer.from(JSON.stringify(payload)).toString("base64"));
46
-
47
- // Return a function which will send the prepared body when invoked.
48
- return () => {
49
- return fetch(API_URL + "/capture/", {
50
- body,
51
- method: "POST"
52
- }).catch(() => {
53
- // Ignore errors
54
- });
55
- };
48
+ for (const key in sanitizedProperties) {
49
+ let sanitizedValue = sanitizedProperties[key];
50
+ if (typeof sanitizedValue === "string") {
51
+ sanitizedValue = sanitizedValue.trim();
52
+ sanitizedValue = stripAnsi(sanitizedValue);
53
+ sanitizedValue = jsesc(sanitizedValue);
54
+ }
55
+
56
+ sanitizedProperties[key] = sanitizedValue;
57
+ }
58
+
59
+ // 3. Send.
60
+ return wts.trackEvent(user, event, sanitizedProperties);
56
61
  };