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

Commit

Permalink
prep for publishing 3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed Jun 23, 2017
1 parent 6e51b7b commit 1fefee8
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 25 deletions.
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ analyzer:
strong-mode: true
linter:
rules:
#- annotate_overrides
- annotate_overrides
- empty_constructor_bodies
- empty_statements
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- expose the `Analytics.applicationName` and `Analytics.applicationVersion`
properties
- make it easier for clients to extend the `AnalyticsIO` class
- allow for custom parameters when sending a screenView

## 3.1.1
- make Analytics.clientId available immediately
Expand Down
27 changes: 19 additions & 8 deletions lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ class AnalyticsImpl implements Analytics {
static const String _defaultAnalyticsUrl =
'https://www.google-analytics.com/collect';

@override
final String trackingId;
@override
final String applicationName;
@override
final String applicationVersion;

final PersistentProperties properties;
Expand All @@ -72,6 +75,7 @@ class AnalyticsImpl implements Analytics {

final List<Future> _futures = [];

@override
AnalyticsOpt analyticsOpt = AnalyticsOpt.optOut;

String _url;
Expand All @@ -91,6 +95,7 @@ class AnalyticsImpl implements Analytics {

bool _firstRun;

@override
bool get firstRun {
if (_firstRun == null) {
_firstRun = properties['firstRun'] == null;
Expand All @@ -103,23 +108,20 @@ class AnalyticsImpl implements Analytics {
return _firstRun;
}

/**
* Will analytics data be sent?
*/
@override
bool get enabled {
bool optIn = analyticsOpt == AnalyticsOpt.optIn;
return optIn
? properties['enabled'] == true
: properties['enabled'] != false;
}

/**
* Enable or disable sending of analytics data.
*/
@override
set enabled(bool value) {
properties['enabled'] = value;
}

@override
Future sendScreenView(String viewName, {Map<String, String> parameters}) {
Map<String, dynamic> args = {'cd': viewName};
if (parameters != null) {
Expand All @@ -128,8 +130,9 @@ class AnalyticsImpl implements Analytics {
return _sendPayload('screenview', args);
}

Future sendEvent(String category, String action, {String label, int value,
Map<String, String> parameters}) {
@override
Future sendEvent(String category, String action,
{String label, int value, Map<String, String> parameters}) {
Map<String, dynamic> args = {'ec': category, 'ea': action};
if (label != null) args['el'] = label;
if (value != null) args['ev'] = value;
Expand All @@ -139,11 +142,13 @@ class AnalyticsImpl implements Analytics {
return _sendPayload('event', args);
}

@override
Future sendSocial(String network, String action, String target) {
Map<String, dynamic> args = {'sn': network, 'sa': action, 'st': target};
return _sendPayload('social', args);
}

@override
Future sendTiming(String variableName, int time,
{String category, String label}) {
Map<String, dynamic> args = {'utv': variableName, 'utt': time};
Expand All @@ -152,12 +157,14 @@ class AnalyticsImpl implements Analytics {
return _sendPayload('timing', args);
}

@override
AnalyticsTimer startTimer(String variableName,
{String category, String label}) {
return new AnalyticsTimer(this, variableName,
category: category, label: label);
}

@override
Future sendException(String description, {bool fatal}) {
// We trim exceptions to a max length; google analytics will apply it's own
// truncation, likely around 150 chars or so.
Expand All @@ -181,8 +188,10 @@ class AnalyticsImpl implements Analytics {
return _sendPayload('exception', args);
}

@override
dynamic getSessionValue(String param) => _variableMap[param];

@override
void setSessionValue(String param, dynamic value) {
if (value == null) {
_variableMap.remove(param);
Expand All @@ -191,8 +200,10 @@ class AnalyticsImpl implements Analytics {
}
}

@override
Stream<Map<String, dynamic>> get onSend => _sendController.stream;

@override
Future waitForLastPing({Duration timeout}) {
Future f = Future.wait(_futures).catchError((e) => null);

Expand Down
4 changes: 4 additions & 0 deletions lib/src/usage_impl_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class HtmlPostHandler extends PostHandler {

HtmlPostHandler({Function this.mockRequestor});

@override
Future sendPost(String url, Map<String, dynamic> parameters) {
int viewportWidth = document.documentElement.clientWidth;
int viewportHeight = document.documentElement.clientHeight;
Expand All @@ -59,8 +60,10 @@ class HtmlPersistentProperties extends PersistentProperties {
_map = JSON.decode(str);
}

@override
dynamic operator [](String key) => _map[key];

@override
void operator []=(String key, dynamic value) {
if (value == null) {
_map.remove(key);
Expand All @@ -71,5 +74,6 @@ class HtmlPersistentProperties extends PersistentProperties {
window.localStorage[name] = JSON.encode(_map);
}

@override
void syncSettings() {}
}
4 changes: 4 additions & 0 deletions lib/src/usage_impl_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class IOPostHandler extends PostHandler {

IOPostHandler({HttpClient this.mockClient}) : _userAgent = _createUserAgent();

@override
Future sendPost(String url, Map<String, dynamic> parameters) async {
String data = postEncode(parameters);

Expand Down Expand Up @@ -120,8 +121,10 @@ class IOPersistentProperties extends PersistentProperties {
syncSettings();
}

@override
dynamic operator [](String key) => _map[key];

@override
void operator []=(String key, dynamic value) {
if (value == null && !_map.containsKey(key)) return;
if (_map[key] == value) return;
Expand All @@ -137,6 +140,7 @@ class IOPersistentProperties extends PersistentProperties {
} catch (_) {}
}

@override
void syncSettings() {
try {
String contents = _file.readAsStringSync();
Expand Down
38 changes: 26 additions & 12 deletions lib/usage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ abstract class Analytics {

/**
* Sends a screen view hit to Google Analytics.
*
*
* [parameters] can be any analytics key/value pair. Useful
* for custom dimensions, etc.
*/
Expand All @@ -92,12 +92,12 @@ abstract class Analytics {
/**
* Sends an Event hit to Google Analytics. [label] specifies the event label.
* [value] specifies the event value. Values must be non-negative.
*
*
* [parameters] can be any analytics key/value pair. Useful
* for custom dimensions, etc.
*/
Future sendEvent(String category, String action, {String label, int value,
Map<String, String> parameters});
Future sendEvent(String category, String action,
{String label, int value, Map<String, String> parameters});

/**
* Sends a Social hit to Google Analytics. [network] specifies the social
Expand Down Expand Up @@ -225,8 +225,11 @@ class AnalyticsTimer {
* stand-in for that will never ping the GA server, or as a mock in test code.
*/
class AnalyticsMock implements Analytics {
@override
String get trackingId => 'UA-0';
@override
String get applicationName => 'mock-app';
@override
String get applicationVersion => '1.0.0';

final bool logCalls;
Expand All @@ -243,35 +246,40 @@ class AnalyticsMock implements Analytics {
*/
AnalyticsMock([this.logCalls = false]);

@override
bool get firstRun => false;

@override
AnalyticsOpt analyticsOpt = AnalyticsOpt.optOut;

@override
bool enabled = true;

@override
String get clientId => '00000000-0000-4000-0000-000000000000';

@override
Future sendScreenView(String viewName, {Map<String, String> parameters}) {
parameters ??= <String, String>{};
parameters['viewName'] = viewName;
return _log('screenView', parameters);
}

Future sendEvent(String category, String action, {String label, int value,
Map<String, String> parameters}) {
@override
Future sendEvent(String category, String action,
{String label, int value, Map<String, String> parameters}) {
parameters ??= <String, String>{};
return _log('event', {
'category': category,
'action': action,
'label': label,
'value': value
}..addAll(parameters));
return _log(
'event',
{'category': category, 'action': action, 'label': label, 'value': value}
..addAll(parameters));
}

@override
Future sendSocial(String network, String action, String target) =>
_log('social', {'network': network, 'action': action, 'target': target});

@override
Future sendTiming(String variableName, int time,
{String category, String label}) {
return _log('timing', {
Expand All @@ -282,21 +290,27 @@ class AnalyticsMock implements Analytics {
});
}

@override
AnalyticsTimer startTimer(String variableName,
{String category, String label}) {
return new AnalyticsTimer(this, variableName,
category: category, label: label);
}

@override
Future sendException(String description, {bool fatal}) =>
_log('exception', {'description': description, 'fatal': fatal});

@override
dynamic getSessionValue(String param) => null;

@override
void setSessionValue(String param, dynamic value) {}

@override
Stream<Map<String, dynamic>> get onSend => _sendController.stream;

@override
Future waitForLastPing({Duration timeout}) => new Future.value();

Future _log(String hitType, Map m) {
Expand Down
4 changes: 1 addition & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ For more information, please see the Google Analytics Measurement Protocol

## Contributing

Run the tests with `pub run test`.

Analyze the code with `dartanalyzer lib/*.dart test/*.dart`.

This comment has been minimized.

Copy link
@sethladd

sethladd Jun 25, 2017

Contributor

Curious, why was this removed? Don't we want people to analyze the code? (I needed to be reminded on how to do this, especially to cover everything in lib and test)

This comment has been minimized.

Copy link
@devoncarew

devoncarew Jun 25, 2017

Author Contributor

The bots will fail on bad PRs - it's already doing the analysis. I don't think we really need to tell people how to analyze dart code. Generally they'll be using IDEs which do this for them automatically.

Test can be run using `pub run test`.

## Issues and bugs

Expand Down
4 changes: 4 additions & 0 deletions test/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ class MockProperties extends PersistentProperties {
if (props != null) this.props.addAll(props);
}

@override
dynamic operator [](String key) => props[key];

@override
void operator []=(String key, dynamic value) {
props[key] = value;
}

@override
void syncSettings() {}
}

class MockPostHandler extends PostHandler {
List<Map<String, dynamic>> sentValues = [];

@override
Future sendPost(String url, Map<String, dynamic> parameters) {
sentValues.add(parameters);

Expand Down
13 changes: 13 additions & 0 deletions test/usage_impl_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,52 @@ void defineTests() {
}

class MockHttpClient implements HttpClient {
@override
String userAgent;
int sendCount = 0;
int writeCount = 0;
bool closed = false;

@override
Future<HttpClientRequest> postUrl(Uri url) {
return new Future.value(new MockHttpClientRequest(this));
}

@override
noSuchMethod(Invocation invocation) {}
}

class MockHttpClientRequest implements HttpClientRequest {
final MockHttpClient client;

MockHttpClientRequest(this.client);

@override
void write(Object obj) {
client.writeCount++;
}

@override
Future<HttpClientResponse> close() {
client.closed = true;
return new Future.value(new MockHttpClientResponse(client));
}

@override
noSuchMethod(Invocation invocation) {}
}

class MockHttpClientResponse implements HttpClientResponse {
final MockHttpClient client;

MockHttpClientResponse(this.client);

@override
Future/*<E>*/ drain/*<E>*/([/*=E*/ futureValue]) {
client.sendCount++;
return new Future.value();
}

@override
noSuchMethod(Invocation invocation) {}
}
3 changes: 2 additions & 1 deletion test/usage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ void defineTests() {
mock.sendScreenView('main');
mock.sendScreenView('withParameters', parameters: {'cd1': 'custom'});
mock.sendEvent('files', 'save');
mock.sendEvent('eventWithParameters', 'save', parameters: {'cd1': 'custom'});
mock.sendEvent('eventWithParameters', 'save',
parameters: {'cd1': 'custom'});
mock.sendSocial('g+', 'plus', 'userid');
mock.sendTiming('compile', 123);
mock.startTimer('compile').finish();
Expand Down

0 comments on commit 1fefee8

Please sign in to comment.