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 #67 from dart-lang/refactor_libraries
Browse files Browse the repository at this point in the history
refactor libraries based on conditional directive changes
  • Loading branch information
devoncarew committed Mar 15, 2016
2 parents b855b3d + 9f84d45 commit 2e373d3
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 139 deletions.
8 changes: 7 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Changelog

## unreleased
- added a `usage` implementation for Flutter (uses conditional directives)
- removed `lib/usage_html.dart`; use the new Analytics.create() static method
- removed `lib/usage_io.dart`; use the new Analytics.create() static method
- bumped to `2.0.0` for API changes and library refactorings

## 1.2.0
- added an optional `analyticsUrl` parameter to the usage constructors

## 1.1.0
- fix two strong mode analysis issues (overrridding a field declaration with a
- fix two strong mode analysis issues (overriding a field declaration with a
setter/getter pair)

## 1.0.1
Expand Down
26 changes: 14 additions & 12 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,49 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
* A simple web app to hand-test the usage library.
*/
/// A simple web app to hand-test the usage library.
library usage_example;

import 'dart:async';
import 'dart:html';

import 'package:usage/usage_html.dart';
import 'package:usage/usage.dart';

Analytics _analytics;
String _lastUa;
int _count = 0;

void main() {
querySelector('#foo').onClick.listen((_) => _handleFoo(getAnalytics()));
querySelector('#bar').onClick.listen((_) => _handleBar(getAnalytics()));
querySelector('#page').onClick.listen((_) => _changePage(getAnalytics()));
querySelector('#foo').onClick.listen((_) => _handleFoo());
querySelector('#bar').onClick.listen((_) => _handleBar());
querySelector('#page').onClick.listen((_) => _changePage());
}

String _ua() => (querySelector('#ua') as InputElement).value.trim();

Analytics getAnalytics() {
Future<Analytics> getAnalytics() async {
if (_analytics == null || _lastUa != _ua()) {
_lastUa = _ua();
_analytics = new AnalyticsHtml(_lastUa, 'Test app', '1.0');
_analytics = await Analytics.create(_lastUa, 'Test app', '1.0');
_analytics.optIn = true;
_analytics.sendScreenView(window.location.pathname);
}

return _analytics;
}

void _handleFoo(Analytics analytics) {
Future _handleFoo() async {
Analytics analytics = await getAnalytics();
analytics.sendEvent('main', 'foo');
}

void _handleBar(Analytics analytics) {
Future _handleBar() async {
Analytics analytics = await getAnalytics();
analytics.sendEvent('main', 'bar');
}

void _changePage(Analytics analytics) {
Future _changePage() async {
Analytics analytics = await getAnalytics();
window.history.pushState(null, 'new page', '${++_count}.html');
analytics.sendScreenView(window.location.pathname);
}
2 changes: 1 addition & 1 deletion example/flutter_example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dependencies:
usage:
path: ../..
flutter:
path: ../../../../../Users/jackson/git/flutter/packages/flutter
path: ../../../flutter/packages/flutter
12 changes: 6 additions & 6 deletions example/ga.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
* A simple command-line app to hand-test the usage library.
*/
/// A simple command-line app to hand-test the usage library.
library usage_ga;

import 'package:usage/usage_io.dart';
import 'dart:async';

void main(List args) {
import 'package:usage/usage.dart';

Future main(List args) async {
final String DEFAULT_UA = 'UA-55029513-1';

if (args.isEmpty) {
Expand All @@ -21,7 +21,7 @@ void main(List args) {

String ua = args.isEmpty ? DEFAULT_UA : args.first;

Analytics ga = new AnalyticsIO(ua, 'ga_test', '1.0');
Analytics ga = await Analytics.create(ua, 'ga_test', '1.0');
ga.optIn = true;

ga.sendScreenView('home').then((_) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ library usage_impl;
import 'dart:async';
import 'dart:math' as math;

import 'uuid.dart';
import '../usage.dart';
import 'uuid.dart';

final int _MAX_EXCEPTION_LENGTH = 100;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/usage_impl_default.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand Down
4 changes: 2 additions & 2 deletions lib/src/usage_impl_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Expand All @@ -12,8 +12,8 @@ import 'package:flutter/http.dart' as http;
import 'package:flutter/services.dart';
import 'package:path/path.dart' as path;

import 'usage_impl.dart';
import '../usage.dart';
import 'usage_impl.dart';

Future<Analytics> createAnalytics(
String trackingId,
Expand Down
22 changes: 21 additions & 1 deletion lib/src/usage_impl_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'dart:async';
import 'dart:convert' show JSON;
import 'dart:html';

import '../usage.dart';
import 'usage_impl.dart';
import '../usage_html.dart';

Future<Analytics> createAnalytics(
String trackingId,
Expand All @@ -25,6 +25,26 @@ Future<Analytics> createAnalytics(
));
}

class AnalyticsHtml extends AnalyticsImpl {
AnalyticsHtml(String trackingId, String applicationName, String applicationVersion, {
String analyticsUrl
}) : super(
trackingId,
new HtmlPersistentProperties(applicationName),
new HtmlPostHandler(),
applicationName: applicationName,
applicationVersion: applicationVersion,
analyticsUrl: analyticsUrl
) {
int screenWidth = window.screen.width;
int screenHeight = window.screen.height;

setSessionValue('sr', '${screenWidth}x$screenHeight');
setSessionValue('sd', '${window.screen.pixelDepth}-bits');
setSessionValue('ul', window.navigator.language);
}
}

class HtmlPostHandler extends PostHandler {
final Function mockRequestor;

Expand Down
15 changes: 14 additions & 1 deletion lib/src/usage_impl_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import 'dart:io';

import 'package:path/path.dart' as path;

import '../usage.dart';
import 'usage_impl.dart';
import '../usage_io.dart';

Future<Analytics> createAnalytics(
String trackingId,
Expand All @@ -27,6 +27,19 @@ Future<Analytics> createAnalytics(
));
}

class AnalyticsIO extends AnalyticsImpl {
AnalyticsIO(String trackingId, String applicationName, String applicationVersion, {
String analyticsUrl
}) : super(
trackingId,
new IOPersistentProperties(applicationName),
new IOPostHandler(),
applicationName: applicationName,
applicationVersion: applicationVersion,
analyticsUrl: analyticsUrl
);
}

String _createUserAgent() {
// Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en)
// Dart/1.8.0-edge.41170 (macos; macos; macos; null)
Expand Down
4 changes: 1 addition & 3 deletions lib/src/uuid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
* A UUID generator library.
*/
/// A UUID generator library.
library usage.uuid;

import 'dart:math' show Random;
Expand Down
26 changes: 12 additions & 14 deletions lib/usage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
// BSD-style license that can be found in the LICENSE file.

/**
* `usage` is a wrapper around Google Analytics for both command-line apps
* and web apps.
* `usage` is a wrapper around Google Analytics for both command-line, web, and
* Flutter apps.
*
* In order to use this library as a web app, import the `analytics_html.dart`
* library and instantiate the [AnalyticsHtml] class.
* In order to use this library, call the [Analytics.create] static method.
* You'll get either the command-line, web, or Flutter implementation based on
* the current platform.
*
* In order to use this library as a command-line app, import the
* `analytics_io.dart` library and instantiate the [AnalyticsIO] class.
*
* For both classes, you need to provide a Google Analytics tracking ID, the
* application name, and the application version.
* When creating a new Analytics instance, you need to provide a Google
* Analytics tracking ID, the application name, and the application version.
*
* Your application should provide an opt-in option for the user. If they
* opt-in, set the [optIn] field to `true`. This setting will persist across
Expand All @@ -36,14 +34,14 @@ import 'src/usage_impl_default.dart'
final RegExp _pathRegex = new RegExp(r'file:/\S+/(\S+\.dart)');

/**
* An interface to a Google Analytics session. You'll get the correct one
* for your platform by calling the [Analytics.create] static method.
* An interface to a Google Analytics session. You'll get the correct one for
* your platform by calling the [Analytics.create] static method.
* [AnalyticsMock] can be used for testing or for some variants of an opt-in
* workflow.
*
* The analytics information is sent on a best-effort basis. So, failures to
* send the GA information will not result in errors from the asynchronous
* `send` methods.
* The analytics information is sent on a best-effort basis. Failures to send
* the GA information will not result in errors from the asynchronous `send`
* methods.
*/
abstract class Analytics {
static Future<Analytics> create(
Expand Down
45 changes: 0 additions & 45 deletions lib/usage_html.dart

This file was deleted.

37 changes: 0 additions & 37 deletions lib/usage_io.dart

This file was deleted.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

name: usage
version: 2.0.0-dev
description: A Google Analytics wrapper for both command-line and web apps.
description: A Google Analytics wrapper for both command-line, web, and Flutter apps.
homepage: https://github.com/dart-lang/usage
author: Dart Team <misc@dartlang.org>

Expand Down
Loading

0 comments on commit 2e373d3

Please sign in to comment.