Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(936)

Side by Side Diff: pkg/telemetry/lib/telemetry.dart

Issue 2954733002: Add initial version of analytics and crash reporting package. (Closed)
Patch Set: update readme Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:io';
6
7 import 'package:path/path.dart' as path;
8 import 'package:usage/src/usage_impl.dart';
9 import 'package:usage/src/usage_impl_io.dart';
10 import 'package:usage/src/usage_impl_io.dart' as usage_io show getDartVersion;
11 import 'package:usage/usage.dart';
12 import 'package:usage/usage_io.dart';
13
14 final String _dartDirectoryName = '.dart';
15 final String _settingsFileName = 'analytics.json';
16
17 /// Dart SDK tools with analytics should display this notice.
18 ///
19 /// In addition, they should support displaying the analytics' status, and have
20 /// a flag to toggle analytics. This may look something like:
21 ///
22 /// `Analytics are currently enabled (and can be disabled with --no-analytics).`
23 final String analyticsNotice =
24 "Dart SDK tools anonymously report feature usage statistics and basic "
25 "crash reports to help improve Dart tools over time. See Google's privacy "
26 "policy: https://www.google.com/intl/en/policies/privacy/.";
27
28 /// Create an [Analytics] instance with the given trackingID and
29 /// applicationName.
30 ///
31 /// This analytics instance will share a common enablement state with the rest
32 /// of the Dart SDK tools.
33 Analytics createAnalyticsInstance(String trackingId, String applicationName,
34 {bool disableForSession: false}) {
35 Directory dir = getDartStorageDirectory();
36 if (!dir.existsSync()) {
37 dir.createSync();
38 }
39
40 File file = new File(path.join(dir.path, _settingsFileName));
41 return new _TelemetryAnalytics(
42 trackingId, applicationName, getDartVersion(), file, disableForSession);
43 }
44
45 /// The directory used to store the analytics settings file.
46 ///
47 /// Typically, the directory is `~/.dart/` (and the settings file is
48 /// `analytics.json`).
49 Directory getDartStorageDirectory() {
kevmoo 2017/06/23 21:01:05 Could just be =>
devoncarew 2017/06/26 15:53:46 Done.
50 return new Directory(path.join(userHomeDir(), _dartDirectoryName));
51 }
52
53 /// Return the version of the Dart SDK.
54 String getDartVersion() => usage_io.getDartVersion();
55
56 class _TelemetryAnalytics extends AnalyticsImpl {
57 final bool disableForSession;
58
59 _TelemetryAnalytics(
60 String trackingId,
61 String applicationName,
62 String applicationVersion,
63 File file,
64 this.disableForSession,
65 )
66 : super(
67 trackingId,
68 new IOPersistentProperties.fromFile(file),
69 new IOPostHandler(),
70 applicationName: applicationName,
71 applicationVersion: applicationVersion,
72 ) {
73 final String locale = getPlatformLocale();
74 if (locale != null) {
75 setSessionValue('ul', locale);
76 }
77 }
78
79 @override
80 bool get enabled {
81 if (disableForSession || _isRunningOnBot()) {
82 return false;
83 }
84 return super.enabled;
85 }
86 }
87
88 bool _isRunningOnBot() {
89 // https://docs.travis-ci.com/user/environment-variables/#Default-Environment- Variables
kevmoo 2017/06/23 21:01:05 long lines
devoncarew 2017/06/26 15:53:46 Done.
90 // https://www.appveyor.com/docs/environment-variables/
91 // CHROME_HEADLESS and BUILDBOT_BUILDERNAME are properties on Chrome infra bot s.
92 return Platform.environment['TRAVIS'] == 'true' ||
93 Platform.environment['BOT'] == 'true' ||
94 Platform.environment['CONTINUOUS_INTEGRATION'] == 'true' ||
95 Platform.environment['CHROME_HEADLESS'] == '1' ||
96 Platform.environment.containsKey('BUILDBOT_BUILDERNAME') ||
97 Platform.environment.containsKey('APPVEYOR') ||
98 Platform.environment.containsKey('CI');
99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698