@webiny/telemetry 5.40.0-beta.2 → 5.40.0-beta.3

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/cli.js CHANGED
@@ -1,29 +1,44 @@
1
- const createSendEvent = require("./sendEvent");
2
1
  const { globalConfig } = require("@webiny/global-config");
2
+ const isCI = require("is-ci");
3
+ const { WTS } = require("wts/src/node");
4
+ const baseSendEvent = require("./sendEvent");
3
5
 
4
- const sendEvent = ({ event, user, version, properties, extraPayload }) => {
6
+ const sendEvent = async ({ event, user, version, properties }) => {
5
7
  const shouldSend = isEnabled();
8
+ if (!shouldSend) {
9
+ return;
10
+ }
11
+
12
+ const wts = new WTS();
13
+
14
+ const wcpProperties = {};
15
+ const [wcpOrgId, wcpProjectId] = getWcpOrgProjectId();
16
+ if (wcpOrgId && wcpProjectId) {
17
+ wcpProperties.wcpOrgId = wcpOrgId;
18
+ wcpProperties.wcpProjectId = wcpProjectId;
19
+ }
6
20
 
7
- try {
8
- const sendTelemetry = createSendEvent({
9
- event,
10
- user: user || globalConfig.get("id"),
21
+ return baseSendEvent({
22
+ event,
23
+ user: user || globalConfig.get("id"),
24
+ properties: {
25
+ ...properties,
26
+ ...wcpProperties,
11
27
  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;
28
+ ci: isCI,
29
+ newUser: Boolean(globalConfig.get("newUser"))
30
+ },
31
+ wts
32
+ });
33
+ };
34
+
35
+ 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;
38
+ if (typeof id === "string") {
39
+ return id.split("/");
26
40
  }
41
+ return [];
27
42
  };
28
43
 
29
44
  const enable = () => {
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "@webiny/telemetry",
3
- "version": "5.40.0-beta.2",
3
+ "version": "5.40.0-beta.3",
4
4
  "license": "MIT",
5
5
  "dependencies": {
6
- "@webiny/global-config": "5.40.0-beta.2",
7
- "form-data": "3.0.0",
8
- "node-fetch": "2.6.1"
6
+ "@webiny/global-config": "5.40.0-beta.3",
7
+ "is-ci": "3.0.0",
8
+ "jsesc": "3.0.2",
9
+ "strip-ansi": "6.0.1",
10
+ "wts": "https://github.com/webiny/wts#51fd5a89a8d12b27def57ff99a71e071e9085e6c"
9
11
  },
10
12
  "publishConfig": {
11
13
  "access": "public",
12
14
  "directory": "."
13
15
  },
14
- "gitHead": "15e414a228687140ce2f30bd2f55763368e99827"
16
+ "gitHead": "638d8b84063906cd1aa979ed6e0487ad26fbcf2f"
15
17
  }
package/react.d.ts CHANGED
@@ -1,2 +1 @@
1
- export declare function setProperties(data: Record<string, any>): void;
2
- export declare function sendEvent(ev: string, data?: Record<string, any>): Promise<any>;
1
+ export declare function sendEvent(ev: string, properties?: Record<string, any>): Promise<any>;
package/react.js CHANGED
@@ -1,36 +1,42 @@
1
- const createSendEvent = require("./sendEvent");
1
+ const baseSendEvent = require("./sendEvent");
2
+ const { WTS } = require("wts/src/admin");
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
+ 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();
24
11
 
25
- const sendTelemetry = createSendEvent({
12
+ const wcpProperties = {};
13
+ const [wcpOrgId, wcpProjectId] = getWcpOrgProjectId();
14
+ if (wcpOrgId && wcpProjectId) {
15
+ wcpProperties.wcpOrgId = wcpOrgId;
16
+ wcpProperties.wcpProjectId = wcpProjectId;
17
+ }
18
+
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
  });
31
+ };
32
32
 
33
- return shouldSend ? sendTelemetry() : Promise.resolve();
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 [];
34
40
  };
35
41
 
36
- module.exports = { setProperties, sendEvent };
42
+ module.exports = { sendEvent };
package/sendEvent.js CHANGED
@@ -1,56 +1,60 @@
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";
6
-
1
+ const stripAnsi = require("strip-ansi");
2
+ const jsesc = require("jsesc");
7
3
  /**
8
4
  * The main `sendEvent` function.
9
5
  * NOTE: don't use this in your app directly. Instead, use the one from `cli.js` or `react.js` files accordingly.
10
6
  */
11
- module.exports = ({ event, user, version, properties, extraPayload } = {}) => {
7
+ module.exports = ({ event, user, properties, wts } = {}) => {
8
+ // 1. Check for existence of required base parameters.
12
9
  if (!event) {
13
10
  throw new Error(`Cannot send event - missing "event" name.`);
14
11
  }
15
12
 
16
13
  if (!user) {
17
- throw new Error(`Cannot send event - missing "user" property.`);
14
+ throw new Error(`Cannot send event - missing "user" ID.`);
15
+ }
16
+
17
+ if (!properties) {
18
+ throw new Error(`Cannot send event - missing "properties" object.`);
19
+ }
20
+
21
+ if (!wts) {
22
+ throw new Error(`Cannot send event - missing "wts" instance.`);
18
23
  }
19
24
 
20
- if (!version) {
25
+ // 2. Extract properties and check for existence of required properties.
26
+ if (!properties.version) {
21
27
  throw new Error(`Cannot send event - missing "version" property.`);
22
28
  }
23
29
 
24
- if (!properties) {
25
- properties = {};
30
+ const hasCiProp = "ci" in properties;
31
+ if (!hasCiProp) {
32
+ throw new Error(`Cannot send event - missing "ci" boolean property.`);
26
33
  }
27
34
 
28
- if (!extraPayload) {
29
- extraPayload = {};
35
+ const hasNewUserProp = "newUser" in properties;
36
+ if (!hasNewUserProp) {
37
+ throw new Error(`Cannot send event - missing "newUser" boolean property.`);
30
38
  }
31
39
 
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()
40
+ // 2. Sanitize properties.
41
+ const sanitizedProperties = {
42
+ ...properties,
43
+ newUser: properties.newUser === true ? "yes" : "no",
44
+ ci: properties.ci === true ? "yes" : "no"
42
45
  };
43
46
 
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
- };
47
+ for (const key in sanitizedProperties) {
48
+ let sanitizedValue = sanitizedProperties[key];
49
+ if (typeof sanitizedValue === "string") {
50
+ sanitizedValue = sanitizedValue.trim();
51
+ sanitizedValue = stripAnsi(sanitizedValue);
52
+ sanitizedValue = jsesc(sanitizedValue);
53
+ }
54
+
55
+ sanitizedProperties[key] = sanitizedValue;
56
+ }
57
+
58
+ // 3. Send.
59
+ return wts.trackEvent(user, event, sanitizedProperties);
56
60
  };