1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
6 |
|
7 | var _temp;
|
8 |
|
9 | const os = require(`os`);
|
10 |
|
11 | const path = require(`path`);
|
12 |
|
13 | const Store = require(`./store`);
|
14 |
|
15 | const fetch = require(`node-fetch`);
|
16 |
|
17 | const Configstore = require(`configstore`);
|
18 |
|
19 | const {
|
20 | ensureDirSync
|
21 | } = require(`fs-extra`);
|
22 |
|
23 | const isTruthy = require(`./is-truthy`);
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | module.exports = (_temp = class EventStorage {
|
33 | constructor() {
|
34 | (0, _defineProperty2.default)(this, "analyticsApi", process.env.GATSBY_TELEMETRY_API || `https://analytics.gatsbyjs.com/events`);
|
35 |
|
36 | try {
|
37 | this.config = new Configstore(`gatsby`, {}, {
|
38 | globalConfigPath: true
|
39 | });
|
40 | } catch (e) {
|
41 |
|
42 | this.config = {
|
43 | get: key => this.config[key],
|
44 | set: (key, value) => this.config[key] = value,
|
45 | all: this.config,
|
46 | path: path.join(os.tmpdir(), `gatsby`),
|
47 | "telemetry.enabled": true,
|
48 | "telemetry.machineId": `not-a-machine-id`
|
49 | };
|
50 | }
|
51 |
|
52 | const baseDir = path.dirname(this.config.path);
|
53 |
|
54 | try {
|
55 | ensureDirSync(baseDir);
|
56 | } catch (e) {
|
57 | }
|
58 |
|
59 | this.store = new Store(baseDir);
|
60 | this.verbose = isTruthy(process.env.GATSBY_TELEMETRY_VERBOSE);
|
61 | this.debugEvents = isTruthy(process.env.GATSBY_TELEMETRY_DEBUG);
|
62 | this.disabled = isTruthy(process.env.GATSBY_TELEMETRY_DISABLED);
|
63 | }
|
64 |
|
65 | isTrackingDisabled() {
|
66 | return this.disabled;
|
67 | }
|
68 |
|
69 | addEvent(event) {
|
70 | if (this.disabled) {
|
71 | return;
|
72 | }
|
73 |
|
74 | const eventString = JSON.stringify(event);
|
75 |
|
76 | if (this.debugEvents || this.verbose) {
|
77 | console.error(`Captured event:`, JSON.parse(eventString));
|
78 |
|
79 | if (this.debugEvents) {
|
80 |
|
81 | return;
|
82 | }
|
83 | }
|
84 |
|
85 | this.store.appendToBuffer(eventString + `\n`);
|
86 | }
|
87 |
|
88 | async sendEvents() {
|
89 | return this.store.startFlushEvents(async eventsData => {
|
90 | const events = eventsData.split(`\n`).filter(e => e && e.length > 2)
|
91 | .map(e => JSON.parse(e));
|
92 | return this.submitEvents(events);
|
93 | });
|
94 | }
|
95 |
|
96 | async submitEvents(events) {
|
97 | try {
|
98 | const res = await fetch(this.analyticsApi, {
|
99 | method: `POST`,
|
100 | headers: {
|
101 | "content-type": `application/json`,
|
102 | "user-agent": this.getUserAgent()
|
103 | },
|
104 | body: JSON.stringify(events)
|
105 | });
|
106 | return res.ok;
|
107 | } catch (e) {
|
108 | return false;
|
109 | }
|
110 | }
|
111 |
|
112 | getUserAgent() {
|
113 | try {
|
114 | const {
|
115 | name,
|
116 | version
|
117 | } = require(`../package.json`);
|
118 |
|
119 | return `${name}:${version}`;
|
120 | } catch (e) {
|
121 | return `Gatsby Telemetry`;
|
122 | }
|
123 | }
|
124 |
|
125 | getConfig(key) {
|
126 | if (key) {
|
127 | return this.config.get(key);
|
128 | }
|
129 |
|
130 | return this.config.all;
|
131 | }
|
132 |
|
133 | updateConfig(key, value) {
|
134 | return this.config.set(key, value);
|
135 | }
|
136 |
|
137 | }, _temp); |
\ | No newline at end of file |