-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
analytics.ts
68 lines (63 loc) · 1.82 KB
/
analytics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as vscode from 'vscode';
import axios from 'axios';
import * as uuid from 'uuidv4';
import { isUndefined } from 'lodash';
function userAllowsTelemetry(): boolean {
const config = vscode.workspace.getConfiguration('telemetry');
return config.get<string>('telemetryLevel', 'off') === 'all';
}
export default class Analytics {
private store: vscode.Memento;
private GA4_TOKEN = process.env.CALVA_DEV_GA4_TOKEN ?? 'GgrUWszmTo2FG538YCUGpw';
private GA4_MEASUREMENT_ID = process.env.CALVA_DEV_GA4_ID ?? 'G-HYZ3MX6DL1';
constructor(context: vscode.ExtensionContext) {
this.store = context.globalState;
}
private userID(): string {
const KEY = 'userLogID';
const value = this.store.get<string>(KEY);
if (isUndefined(value)) {
const newID = uuid.uuid();
void this.store.update(KEY, newID);
return newID;
} else {
return value;
}
}
async logGA4Pageview(path: string) {
if (!userAllowsTelemetry()) {
return;
}
try {
return axios
.post(
`https://www.google-analytics.com/mp/collect?measurement_id=${this.GA4_MEASUREMENT_ID}&api_secret=${this.GA4_TOKEN}`,
{
client_id: this.userID(),
user_id: this.userID(),
events: [
{
name: 'page_view',
params: {
page_location: path,
page_title: path.replace(/^\//, ''),
engagement_time_msec: 1,
},
},
],
},
{
headers: {
'User-Agent': 'Mozilla/5.0 VSCode',
'Content-Type': 'application/json',
},
}
)
.catch(function (error) {
console.log(error);
});
} catch (error) {
console.log(error);
}
}
}