UNPKG

3.65 kBJavaScriptView Raw
1/**
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing,
11 software distributed under the License is distributed on an
12 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 KIND, either express or implied. See the License for the
14 specific language governing permissions and limitations
15 under the License.
16*/
17
18// For further details on telemetry, see:
19// https://github.com/cordova/cordova-discuss/pull/43
20
21// Google Analytics tracking code
22var GA_TRACKING_CODE = 'UA-64283057-7';
23
24var pkg = require('../package.json');
25var Insight = require('insight');
26var insight = new Insight({
27 trackingCode: GA_TRACKING_CODE,
28 pkg: pkg
29});
30
31/**
32 * Returns true if the user opted in, and false otherwise
33 */
34function showPrompt () {
35 return new Promise(function (resolve, reject) {
36 var msg = 'May Cordova anonymously report usage statistics to improve the tool over time?';
37 insight._permissionTimeout = module.exports.timeoutInSecs || 30;
38 insight.askPermission(msg, function (unused, optIn) {
39 var EOL = require('os').EOL;
40 if (optIn) {
41 console.log(EOL + 'Thanks for opting into telemetry to help us improve cordova.');
42 module.exports.track('telemetry', 'on', 'via-cli-prompt-choice', 'successful');
43 } else {
44 console.log(EOL + 'You have been opted out of telemetry. To change this, run: cordova telemetry on.');
45 // Always track telemetry opt-outs! (whether opted-in or opted-out)
46 module.exports.track('telemetry', 'off', 'via-cli-prompt-choice', 'successful');
47 }
48 resolve(optIn);
49 });
50 });
51}
52
53function track () {
54 // Remove empty, null or undefined strings from arguments
55 for (var property in arguments) {
56 var val = arguments[property];
57 if (!val || val.length === 0) {
58 delete arguments.property;
59 }
60 }
61 insight.track.apply(insight, arguments);
62}
63
64function turnOn () {
65 insight.optOut = false;
66}
67
68function turnOff () {
69 insight.optOut = true;
70}
71
72/**
73 * Clears telemetry setting
74 * Has the same effect as if user never answered the telemetry prompt
75 * Useful for testing purposes
76 */
77function clear () {
78 insight.optOut = undefined;
79}
80
81function isOptedIn () {
82 return !insight.optOut;
83}
84
85/**
86 * Has the user already answered the telemetry prompt? (thereby opting in or out?)
87 */
88function hasUserOptedInOrOut () {
89 var insightOptOut = insight.optOut === undefined;
90 return !(insightOptOut);
91}
92
93/**
94 * Is the environment variable 'CI' specified ?
95 */
96function isCI (env) {
97 return !!env.CI;
98}
99
100/**
101 * Has the user ran a command of the form: `cordova run --no-telemetry` ?
102 */
103function isNoTelemetryFlag (args) {
104 return args.indexOf('--no-telemetry') > -1;
105}
106
107// this is to help testing, so we don't have to wait for the full 30
108module.exports = {
109 track: track,
110 turnOn: turnOn,
111 turnOff: turnOff,
112 clear: clear,
113 isOptedIn: isOptedIn,
114 hasUserOptedInOrOut: hasUserOptedInOrOut,
115 isCI: isCI,
116 showPrompt: showPrompt,
117 isNoTelemetryFlag: isNoTelemetryFlag,
118 timeoutInSecs: 30
119};