| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// A simple web app to hand-test the usage library. | 5 /** |
| 6 * A simple web app to hand-test the usage library. |
| 7 */ |
| 6 library usage_example; | 8 library usage_example; |
| 7 | 9 |
| 8 import 'dart:html'; | 10 import 'dart:html'; |
| 9 | 11 |
| 10 import 'package:usage/usage_html.dart'; | 12 import 'package:usage/usage_html.dart'; |
| 11 | 13 |
| 12 Analytics _analytics; | 14 Analytics _analytics; |
| 13 String _lastUa; | 15 String _lastUa; |
| 14 int _count = 0; | 16 int _count = 0; |
| 15 | 17 |
| 16 void main() { | 18 void main() { |
| 17 querySelector('#foo').onClick.listen((_) => _handleFoo()); | 19 querySelector('#foo').onClick.listen((_) => _handleFoo(getAnalytics())); |
| 18 querySelector('#bar').onClick.listen((_) => _handleBar()); | 20 querySelector('#bar').onClick.listen((_) => _handleBar(getAnalytics())); |
| 19 querySelector('#page').onClick.listen((_) => _changePage()); | 21 querySelector('#page').onClick.listen((_) => _changePage(getAnalytics())); |
| 20 } | 22 } |
| 21 | 23 |
| 22 String _ua() => (querySelector('#ua') as InputElement).value.trim(); | 24 String _ua() => (querySelector('#ua') as InputElement).value.trim(); |
| 23 | 25 |
| 24 Analytics getAnalytics() { | 26 Analytics getAnalytics() { |
| 25 if (_analytics == null || _lastUa != _ua()) { | 27 if (_analytics == null || _lastUa != _ua()) { |
| 26 _lastUa = _ua(); | 28 _lastUa = _ua(); |
| 27 _analytics = new AnalyticsHtml(_lastUa, 'Test app', '1.0'); | 29 _analytics = new AnalyticsHtml(_lastUa, 'Test app', '1.0'); |
| 30 _analytics.optIn = true; |
| 28 _analytics.sendScreenView(window.location.pathname); | 31 _analytics.sendScreenView(window.location.pathname); |
| 29 } | 32 } |
| 30 | 33 |
| 31 return _analytics; | 34 return _analytics; |
| 32 } | 35 } |
| 33 | 36 |
| 34 void _handleFoo() { | 37 void _handleFoo(Analytics analytics) { |
| 35 Analytics analytics = getAnalytics(); | |
| 36 analytics.sendEvent('main', 'foo'); | 38 analytics.sendEvent('main', 'foo'); |
| 37 } | 39 } |
| 38 | 40 |
| 39 void _handleBar() { | 41 void _handleBar(Analytics analytics) { |
| 40 Analytics analytics = getAnalytics(); | |
| 41 analytics.sendEvent('main', 'bar'); | 42 analytics.sendEvent('main', 'bar'); |
| 42 } | 43 } |
| 43 | 44 |
| 44 void _changePage() { | 45 void _changePage(Analytics analytics) { |
| 45 Analytics analytics = getAnalytics(); | |
| 46 window.history.pushState(null, 'new page', '${++_count}.html'); | 46 window.history.pushState(null, 'new page', '${++_count}.html'); |
| 47 analytics.sendScreenView(window.location.pathname); | 47 analytics.sendScreenView(window.location.pathname); |
| 48 } | 48 } |
| OLD | NEW |