blob: d570341621a7fe9f6241844169dc7b9235929b1c [file] [log] [blame]
Devon Carew3798eaa2015-01-13 13:25:56 -08001// 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
3// BSD-style license that can be found in the LICENSE file.
4
5/**
6 * A simple web app to hand-test the usage library.
7 */
8library usage_example;
9
10import 'dart:html';
11
12import 'package:usage/usage_html.dart';
13
14Analytics _analytics;
15String _lastUa;
16int _count = 0;
17
18void main() {
19 querySelector('#foo').onClick.listen((_) => _handleFoo(getAnalytics()));
20 querySelector('#bar').onClick.listen((_) => _handleBar(getAnalytics()));
21 querySelector('#page').onClick.listen((_) => _changePage(getAnalytics()));
22}
23
24String _ua() => (querySelector('#ua') as InputElement).value.trim();
25
26Analytics getAnalytics() {
27 if (_analytics == null || _lastUa != _ua()) {
28 _lastUa = _ua();
29 _analytics = new AnalyticsHtml(_lastUa, 'Test app', '1.0');
30 _analytics.sendScreenView(window.location.pathname);
31 }
32
33 return _analytics;
34}
35
36void _handleFoo(Analytics analytics) {
37 analytics.sendEvent('main', 'foo');
38}
39
40void _handleBar(Analytics analytics) {
41 analytics.sendEvent('main', 'bar');
42}
43
44void _changePage(Analytics analytics) {
45 window.history.pushState(null, 'new page', '${++_count}.html');
46 analytics.sendScreenView(window.location.pathname);
47}