@webiny/telemetry 0.0.0-ee-vpcs.549378cf03

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @webiny/telemetry
package/cli.js ADDED
@@ -0,0 +1,48 @@
1
+ const createSendEvent = require("./sendEvent");
2
+ const { globalConfig } = require("@webiny/global-config");
3
+
4
+ const sendEvent = ({ event, user, version, properties, extraPayload }) => {
5
+ const shouldSend = isEnabled();
6
+
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;
26
+ }
27
+ };
28
+
29
+ const enable = () => {
30
+ globalConfig.set("telemetry", true);
31
+ };
32
+
33
+ const disable = () => {
34
+ globalConfig.set("telemetry", false);
35
+ };
36
+
37
+ const isEnabled = () => {
38
+ const config = globalConfig.get();
39
+
40
+ if (config.telemetry === false) {
41
+ return false;
42
+ }
43
+
44
+ // `tracking` is left here for backwards compatibility with previous versions of Webiny.
45
+ return config.tracking !== false;
46
+ };
47
+
48
+ module.exports = { sendEvent, enable, disable, isEnabled };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@webiny/telemetry",
3
+ "version": "0.0.0-ee-vpcs.549378cf03",
4
+ "license": "MIT",
5
+ "dependencies": {
6
+ "@webiny/global-config": "0.0.0-ee-vpcs.549378cf03",
7
+ "form-data": "3.0.0",
8
+ "node-fetch": "2.6.1"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public",
12
+ "directory": "."
13
+ },
14
+ "gitHead": "549378cf03fcd27845fc3fa23d1dc6b32896f630"
15
+ }
package/react.js ADDED
@@ -0,0 +1,36 @@
1
+ const createSendEvent = require("./sendEvent");
2
+
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
+ };
21
+ }
22
+
23
+ const shouldSend = process.env.REACT_APP_WEBINY_TELEMETRY !== "false";
24
+
25
+ const sendTelemetry = createSendEvent({
26
+ event,
27
+ properties,
28
+ extraPayload,
29
+ user: process.env.REACT_APP_USER_ID,
30
+ version: process.env.REACT_APP_WEBINY_VERSION
31
+ });
32
+
33
+ return shouldSend ? sendTelemetry() : Promise.resolve();
34
+ };
35
+
36
+ module.exports = { setProperties, sendEvent };
package/sendEvent.js ADDED
@@ -0,0 +1,56 @@
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
+
7
+ /**
8
+ * The main `sendEvent` function.
9
+ * NOTE: don't use this in your app directly. Instead, use the one from `cli.js` or `react.js` files accordingly.
10
+ */
11
+ module.exports = ({ event, user, version, properties, extraPayload } = {}) => {
12
+ if (!event) {
13
+ throw new Error(`Cannot send event - missing "event" name.`);
14
+ }
15
+
16
+ if (!user) {
17
+ throw new Error(`Cannot send event - missing "user" property.`);
18
+ }
19
+
20
+ if (!version) {
21
+ throw new Error(`Cannot send event - missing "version" property.`);
22
+ }
23
+
24
+ if (!properties) {
25
+ properties = {};
26
+ }
27
+
28
+ if (!extraPayload) {
29
+ extraPayload = {};
30
+ }
31
+
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()
42
+ };
43
+
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
+ };
56
+ };