UNPKG

3.26 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
6
7var _temp;
8
9const os = require(`os`);
10
11const path = require(`path`);
12
13const Store = require(`./store`);
14
15const fetch = require(`node-fetch`);
16
17const Configstore = require(`configstore`);
18
19const {
20 ensureDirSync
21} = require(`fs-extra`);
22
23const isTruthy = require(`./is-truthy`);
24/* The events data collection is a spooled process that
25 * buffers events to a local fs based buffer
26 * which then is asynchronously flushed to the server.
27 * This both increases the fault tolerancy and allows collection
28 * to continue even when working offline.
29 */
30
31
32module.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 // This should never happen
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) {// TODO: Log this event
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 // Bail because we don't want to send debug events
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) // drop empty lines
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