UNPKG

3.56 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
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20/* jshint node:true, bitwise:true, undef:true, trailing:true, quotmark:true,
21 indent:4, unused:vars, latedef:nofunc,
22 laxcomma:true
23*/
24
25
26
27// For further details on telemetry, see:
28// https://github.com/cordova/cordova-discuss/pull/43
29
30var Q = require('q');
31
32// Google Analytics tracking code
33var GA_TRACKING_CODE = 'UA-64283057-7';
34
35var pkg = require('../package.json');
36var Insight = require('insight');
37var insight = new Insight({
38 trackingCode: GA_TRACKING_CODE,
39 pkg: pkg
40});
41
42/**
43 * Returns true if the user opted in, and false otherwise
44 */
45function showPrompt() {
46
47 var deferred = Q.defer();
48
49 var msg = "May Cordova anonymously report usage statistics to improve the tool over time?";
50 insight.askPermission(msg, function (unused, optIn) {
51 var EOL = require('os').EOL;
52 if (optIn) {
53 console.log(EOL + "Thanks for opting into telemetry to help us improve cordova.");
54 track('telemetry', 'on', 'via-cli-prompt-choice', 'successful');
55 } else {
56 console.log(EOL + "You have been opted out of telemetry. To change this, run: cordova telemetry on.");
57 // Always track telemetry opt-outs! (whether opted-in or opted-out)
58 track('telemetry', 'off', 'via-cli-prompt-choice', 'successful');
59 }
60
61 deferred.resolve(optIn);
62 });
63
64 return deferred.promise;
65}
66
67function track() {
68 // Remove empty, null or undefined strings from arguments
69 for (var property in arguments) {
70 var val = arguments[property];
71 if (!val || val.length === 0) {
72 delete arguments.property;
73 }
74 }
75 insight.track.apply(insight, arguments);
76}
77
78function turnOn() {
79 insight.optOut = false;
80}
81
82function turnOff() {
83 insight.optOut = true;
84}
85
86/**
87 * Clears telemetry setting
88 * Has the same effect as if user never answered the telemetry prompt
89 * Useful for testing purposes
90 */
91function clear() {
92 insight.optOut = undefined;
93}
94
95function isOptedIn() {
96 return !insight.optOut;
97}
98
99/**
100 * Has the user already answered the telemetry prompt? (thereby opting in or out?)
101 */
102function hasUserOptedInOrOut() {
103 return !(insight.optOut === undefined);
104}
105
106/**
107 * Is the environment variable 'CI' specified ?
108 */
109function isCI(env) {
110 return !!env.CI;
111}
112
113/**
114 * Has the user ran a command of the form: `cordova run --no-telemetry` ?
115 */
116function isNoTelemetryFlag(args) {
117 return args.indexOf('--no-telemetry') > -1;
118}
119
120module.exports = {
121 track: track,
122 turnOn: turnOn,
123 turnOff: turnOff,
124 clear: clear,
125 isOptedIn: isOptedIn,
126 hasUserOptedInOrOut: hasUserOptedInOrOut,
127 isCI: isCI,
128 showPrompt: showPrompt,
129 isNoTelemetryFlag: isNoTelemetryFlag
130};
\No newline at end of file