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 |
|
30 | var Q = require('q');
|
31 |
|
32 | // Google Analytics tracking code
|
33 | var GA_TRACKING_CODE = 'UA-64283057-7';
|
34 |
|
35 | var pkg = require('../package.json');
|
36 | var Insight = require('insight');
|
37 | var 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 | */
|
45 | function 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 |
|
67 | function 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 |
|
78 | function turnOn() {
|
79 | insight.optOut = false;
|
80 | }
|
81 |
|
82 | function 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 | */
|
91 | function clear() {
|
92 | insight.optOut = undefined;
|
93 | }
|
94 |
|
95 | function isOptedIn() {
|
96 | return !insight.optOut;
|
97 | }
|
98 |
|
99 | /**
|
100 | * Has the user already answered the telemetry prompt? (thereby opting in or out?)
|
101 | */
|
102 | function hasUserOptedInOrOut() {
|
103 | var insightOptOut = insight.optOut === undefined;
|
104 | return !(insightOptOut);
|
105 | }
|
106 |
|
107 | /**
|
108 | * Is the environment variable 'CI' specified ?
|
109 | */
|
110 | function isCI(env) {
|
111 | return !!env.CI;
|
112 | }
|
113 |
|
114 | /**
|
115 | * Has the user ran a command of the form: `cordova run --no-telemetry` ?
|
116 | */
|
117 | function isNoTelemetryFlag(args) {
|
118 | return args.indexOf('--no-telemetry') > -1;
|
119 | }
|
120 |
|
121 | module.exports = {
|
122 | track: track,
|
123 | turnOn: turnOn,
|
124 | turnOff: turnOff,
|
125 | clear: clear,
|
126 | isOptedIn: isOptedIn,
|
127 | hasUserOptedInOrOut: hasUserOptedInOrOut,
|
128 | isCI: isCI,
|
129 | showPrompt: showPrompt,
|
130 | isNoTelemetryFlag: isNoTelemetryFlag
|
131 | }; |
\ | No newline at end of file |