Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #111 from dart-lang/usage_3_2_0
Browse files Browse the repository at this point in the history
expose additional fields on Analytics
  • Loading branch information
devoncarew authored Jun 23, 2017
2 parents 33aa327 + 5c2f173 commit 6e51b7b
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 20 deletions.
2 changes: 0 additions & 2 deletions .analysis_options

This file was deleted.

7 changes: 7 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
analyzer:
strong-mode: true
linter:
rules:
#- annotate_overrides
- empty_constructor_bodies
- empty_statements
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Changelog

## 3.1.1
## 3.2.0
- expose the `Analytics.applicationName` and `Analytics.applicationVersion`
properties
- make it easier for clients to extend the `AnalyticsIO` class

## 3.1.1
- make Analytics.clientId available immediately

## 3.1.0
Expand Down
13 changes: 7 additions & 6 deletions lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ class AnalyticsImpl implements Analytics {
static const String _defaultAnalyticsUrl =
'https://www.google-analytics.com/collect';

/**
* Tracking ID / Property ID.
*/
final String trackingId;
final String applicationName;
final String applicationVersion;

final PersistentProperties properties;
final PostHandler postHandler;
Expand All @@ -81,9 +80,7 @@ class AnalyticsImpl implements Analytics {
new StreamController.broadcast(sync: true);

AnalyticsImpl(this.trackingId, this.properties, this.postHandler,
{String applicationName,
String applicationVersion,
String analyticsUrl}) {
{this.applicationName, this.applicationVersion, String analyticsUrl}) {
assert(trackingId != null);

if (applicationName != null) setSessionValue('an', applicationName);
Expand Down Expand Up @@ -266,6 +263,10 @@ abstract class PersistentProperties {

dynamic operator [](String key);
void operator []=(String key, dynamic value);

/// Re-read settings from the backing store. This may be a no-op on some
/// platforms.
void syncSettings();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/src/usage_impl_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ class HtmlPersistentProperties extends PersistentProperties {

window.localStorage[name] = JSON.encode(_map);
}

void syncSettings() {}
}
37 changes: 26 additions & 11 deletions lib/src/usage_impl_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:convert' show JSON;
import 'dart:convert' show JSON, JsonEncoder;
import 'dart:io';

import 'package:path/path.dart' as path;
Expand Down Expand Up @@ -60,7 +60,7 @@ String _createUserAgent() {
}
}

String _userHomeDir() {
String userHomeDir() {
String envKey = Platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME';
String value = Platform.environment[envKey];
return value == null ? '.' : value;
Expand Down Expand Up @@ -96,23 +96,28 @@ class IOPostHandler extends PostHandler {
}
}

JsonEncoder _jsonEncoder = new JsonEncoder.withIndent(' ');

class IOPersistentProperties extends PersistentProperties {
File _file;
Map _map;

IOPersistentProperties(String name, {String documentDirPath}) : super(name) {
String fileName = '.${name.replaceAll(' ', '_')}';
documentDirPath ??= _userHomeDir();
documentDirPath ??= userHomeDir();
_file = new File(path.join(documentDirPath, fileName));
if (!_file.existsSync()) {
_file.createSync();
}
syncSettings();
}

try {
if (!_file.existsSync()) _file.createSync();
String contents = _file.readAsStringSync();
if (contents.isEmpty) contents = '{}';
_map = JSON.decode(contents);
} catch (_) {
_map = {};
IOPersistentProperties.fromFile(File file) : super(path.basename(file.path)) {
_file = file;
if (!_file.existsSync()) {
_file.createSync();
}
syncSettings();
}

dynamic operator [](String key) => _map[key];
Expand All @@ -128,9 +133,19 @@ class IOPersistentProperties extends PersistentProperties {
}

try {
_file.writeAsStringSync(JSON.encode(_map) + '\n');
_file.writeAsStringSync(_jsonEncoder.convert(_map) + '\n');
} catch (_) {}
}

void syncSettings() {
try {
String contents = _file.readAsStringSync();
if (contents.isEmpty) contents = '{}';
_map = JSON.decode(contents);
} catch (_) {
_map = {};
}
}
}

/// Return the string for the platform's locale; return's `null` if the locale
Expand Down
9 changes: 9 additions & 0 deletions lib/usage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ abstract class Analytics {
*/
String get trackingId;

/// The application name.
String get applicationName;

/// The application version.
String get applicationVersion;

/**
* Is this the first time the tool has run?
*/
Expand Down Expand Up @@ -220,6 +226,9 @@ class AnalyticsTimer {
*/
class AnalyticsMock implements Analytics {
String get trackingId => 'UA-0';
String get applicationName => 'mock-app';
String get applicationVersion => '1.0.0';

final bool logCalls;

/**
Expand Down
2 changes: 2 additions & 0 deletions test/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class MockProperties extends PersistentProperties {
void operator []=(String key, dynamic value) {
props[key] = value;
}

void syncSettings() {}
}

class MockPostHandler extends PostHandler {
Expand Down
15 changes: 15 additions & 0 deletions test/usage_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ void defineTests() {
});

group('AnalyticsImpl', () {
test('trackingId', () {
AnalyticsImplMock mock = createMock();
expect(mock.trackingId, isNotNull);
});

test('applicationName', () {
AnalyticsImplMock mock = createMock();
expect(mock.applicationName, isNotNull);
});

test('applicationVersion', () {
AnalyticsImplMock mock = createMock();
expect(mock.applicationVersion, isNotNull);
});

test('respects disabled', () {
AnalyticsImplMock mock = createMock();
mock.enabled = false;
Expand Down

0 comments on commit 6e51b7b

Please sign in to comment.