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

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

Issue 2954733002: Add initial version of analytics and crash reporting package. (Closed)
Patch Set: updates from review comments Created 3 years, 5 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
« no previous file with comments | « pkg/telemetry/lib/crash_reporting.dart ('k') | pkg/telemetry/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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() =>
50 new Directory(path.join(userHomeDir(), _dartDirectoryName));
51
52 /// Return the version of the Dart SDK.
53 String getDartVersion() => usage_io.getDartVersion();
54
55 class _TelemetryAnalytics extends AnalyticsImpl {
56 final bool disableForSession;
57
58 _TelemetryAnalytics(
59 String trackingId,
60 String applicationName,
61 String applicationVersion,
62 File file,
63 this.disableForSession,
64 )
65 : super(
66 trackingId,
67 new IOPersistentProperties.fromFile(file),
68 new IOPostHandler(),
69 applicationName: applicationName,
70 applicationVersion: applicationVersion,
71 ) {
72 final String locale = getPlatformLocale();
73 if (locale != null) {
74 setSessionValue('ul', locale);
75 }
76 }
77
78 @override
79 bool get enabled {
80 if (disableForSession || _isRunningOnBot()) {
81 return false;
82 }
83 return super.enabled;
84 }
85 }
86
87 bool _isRunningOnBot() {
88 // - https://docs.travis-ci.com/user/environment-variables/
89 // - https://www.appveyor.com/docs/environment-variables/
90 // - CHROME_HEADLESS and BUILDBOT_BUILDERNAME are properties on Chrome infra
91 // bots.
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
« no previous file with comments | « pkg/telemetry/lib/crash_reporting.dart ('k') | pkg/telemetry/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698