UNPKG

5.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Debug = require("debug");
4const helper_1 = require("./helper");
5const uuid_1 = require("./utils/uuid");
6const debug = Debug('ionic:lib:telemetry');
7const GA_CODE = 'UA-44023830-30';
8let _gaTracker;
9class Telemetry {
10 constructor({ config, client, getInfo, ctx, project, session }) {
11 this.client = client;
12 this.config = config;
13 this.getInfo = getInfo;
14 this.ctx = ctx;
15 this.project = project;
16 this.session = session;
17 }
18 async sendCommand(command, args) {
19 debug('Sending telemetry for command: %O %O', command, args);
20 await helper_1.sendMessage({ config: this.config, ctx: this.ctx }, { type: 'telemetry', data: { command, args } });
21 }
22}
23exports.Telemetry = Telemetry;
24async function getLeek({ config, version }) {
25 if (!_gaTracker) {
26 const Leek = await Promise.resolve().then(() => require('leek'));
27 let telemetryToken = config.get('tokens.telemetry');
28 if (!telemetryToken) {
29 telemetryToken = uuid_1.generateUUID();
30 config.set('tokens.telemetry', telemetryToken);
31 debug(`setting telemetry token to ${telemetryToken}`);
32 }
33 _gaTracker = new Leek({
34 name: telemetryToken,
35 trackingCode: GA_CODE,
36 globalName: 'ionic',
37 version,
38 silent: !config.get('telemetry'),
39 });
40 }
41 return _gaTracker;
42}
43async function sendCommand({ config, client, getInfo, ctx, session, project }, command, args) {
44 const messageList = [];
45 const name = 'command execution';
46 const prettyArgs = args.map(a => a.includes(' ') ? `"${a}"` : a);
47 const message = messageList.concat([command], prettyArgs).join(' ');
48 await Promise.all([
49 (async () => {
50 const leek = await getLeek({ config, version: ctx.version });
51 try {
52 await leek.track({ name, message });
53 }
54 catch (e) {
55 debug(`leek track error: ${e.stack ? e.stack : e}`);
56 }
57 })(),
58 (async () => {
59 const now = new Date().toISOString();
60 const appflowId = project ? project.config.get('id') : undefined;
61 const { req } = await client.make('POST', '/events/metrics');
62 const metric = {
63 'name': 'cli_command_metrics',
64 'timestamp': now,
65 'session_id': config.get('tokens.telemetry'),
66 'source': 'cli',
67 'value': {
68 'command': command,
69 'arguments': prettyArgs.join(' '),
70 'version': ctx.version,
71 'node_version': process.version,
72 'app_id': appflowId,
73 'backend': 'pro',
74 },
75 };
76 const isLoggedIn = session.isLoggedIn();
77 const info = await getInfo();
78 if (isLoggedIn) {
79 const token = session.getUserToken();
80 req.set('Authorization', `Bearer ${token}`);
81 }
82 const frameworkInfo = info.find(item => item.key === 'Ionic Framework');
83 const npmInfo = info.find(item => item.key === 'npm');
84 const osInfo = info.find(item => item.key === 'OS');
85 const xcodeInfo = info.find(item => item.key === 'Xcode');
86 const androidSdkInfo = info.find(item => item.key === 'Android SDK Tools');
87 const cordovaInfo = info.find(item => item.key === 'Cordova CLI');
88 const cordovaPlatformsInfo = info.find(item => item.key === 'Cordova Platforms');
89 const appScriptsInfo = info.find(item => item.key === '@ionic/app-scripts');
90 if (frameworkInfo) {
91 metric['value']['framework'] = frameworkInfo.value;
92 }
93 if (npmInfo) {
94 metric['value']['npm_version'] = npmInfo.value;
95 }
96 if (osInfo) {
97 metric['value']['os'] = osInfo.value;
98 }
99 if (xcodeInfo) {
100 metric['value']['xcode_version'] = xcodeInfo.value;
101 }
102 if (androidSdkInfo) {
103 metric['value']['android_sdk_version'] = androidSdkInfo.value;
104 }
105 if (cordovaInfo) {
106 metric['value']['cordova_version'] = cordovaInfo.value;
107 }
108 if (cordovaPlatformsInfo) {
109 metric['value']['cordova_platforms'] = cordovaPlatformsInfo.value;
110 }
111 if (appScriptsInfo) {
112 metric['value']['app_scripts_version'] = appScriptsInfo.value;
113 }
114 debug('metric: %o', metric);
115 req.send({
116 'metrics': [metric],
117 'sent_at': now,
118 });
119 try {
120 await client.do(req);
121 }
122 catch (e) {
123 debug(`metric send error: ${e.stack ? e.stack : e}`);
124 }
125 })(),
126 ]);
127}
128exports.sendCommand = sendCommand;