| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library usage_impl_html; |
| 6 |
| 5 import 'dart:async'; | 7 import 'dart:async'; |
| 6 import 'dart:convert' show JSON; | 8 import 'dart:convert' show JSON; |
| 7 import 'dart:html'; | 9 import 'dart:html'; |
| 8 | 10 |
| 9 import 'usage_impl.dart'; | 11 import 'usage_impl.dart'; |
| 10 | 12 |
| 11 /// An interface to a Google Analytics session, suitable for use in web apps. | 13 class HtmlPostHandler extends PostHandler { |
| 12 /// | 14 final Function mockRequestor; |
| 13 /// [analyticsUrl] is an optional replacement for the default Google Analytics | |
| 14 /// URL (`https://www.google-analytics.com/collect`). | |
| 15 class AnalyticsHtml extends AnalyticsImpl { | |
| 16 AnalyticsHtml( | |
| 17 String trackingId, String applicationName, String applicationVersion, | |
| 18 {String analyticsUrl}) | |
| 19 : super(trackingId, new HtmlPersistentProperties(applicationName), | |
| 20 new HtmlPostHandler(), | |
| 21 applicationName: applicationName, | |
| 22 applicationVersion: applicationVersion, | |
| 23 analyticsUrl: analyticsUrl) { | |
| 24 int screenWidth = window.screen.width; | |
| 25 int screenHeight = window.screen.height; | |
| 26 | 15 |
| 27 setSessionValue('sr', '${screenWidth}x$screenHeight'); | 16 HtmlPostHandler({Function this.mockRequestor}); |
| 28 setSessionValue('sd', '${window.screen.pixelDepth}-bits'); | |
| 29 setSessionValue('ul', window.navigator.language); | |
| 30 } | |
| 31 } | |
| 32 | 17 |
| 33 typedef Future<HttpRequest> HttpRequestor(String url, | |
| 34 {String method, sendData}); | |
| 35 | |
| 36 class HtmlPostHandler extends PostHandler { | |
| 37 final HttpRequestor mockRequestor; | |
| 38 | |
| 39 HtmlPostHandler({this.mockRequestor}); | |
| 40 | |
| 41 @override | |
| 42 Future sendPost(String url, Map<String, dynamic> parameters) { | 18 Future sendPost(String url, Map<String, dynamic> parameters) { |
| 43 int viewportWidth = document.documentElement.clientWidth; | 19 int viewportWidth = document.documentElement.clientWidth; |
| 44 int viewportHeight = document.documentElement.clientHeight; | 20 int viewportHeight = document.documentElement.clientHeight; |
| 45 | 21 |
| 46 parameters['vp'] = '${viewportWidth}x$viewportHeight'; | 22 parameters['vp'] = '${viewportWidth}x$viewportHeight'; |
| 47 | 23 |
| 48 String data = postEncode(parameters); | 24 String data = postEncode(parameters); |
| 49 HttpRequestor requestor = | 25 var request = mockRequestor == null ? HttpRequest.request : mockRequestor; |
| 50 mockRequestor == null ? HttpRequest.request : mockRequestor; | 26 return request(url, method: 'POST', sendData: data).catchError((e) { |
| 51 return requestor(url, method: 'POST', sendData: data).catchError((e) { | |
| 52 // Catch errors that can happen during a request, but that we can't do | 27 // Catch errors that can happen during a request, but that we can't do |
| 53 // anything about, e.g. a missing internet connection. | 28 // anything about, e.g. a missing internet conenction. |
| 54 }); | 29 }); |
| 55 } | 30 } |
| 56 | |
| 57 @override | |
| 58 void close() {} | |
| 59 } | 31 } |
| 60 | 32 |
| 61 class HtmlPersistentProperties extends PersistentProperties { | 33 class HtmlPersistentProperties extends PersistentProperties { |
| 62 Map _map; | 34 Map _map; |
| 63 | 35 |
| 64 HtmlPersistentProperties(String name) : super(name) { | 36 HtmlPersistentProperties(String name) : super(name) { |
| 65 String str = window.localStorage[name]; | 37 String str = window.localStorage[name]; |
| 66 if (str == null || str.isEmpty) str = '{}'; | 38 if (str == null || str.isEmpty) str = '{}'; |
| 67 _map = JSON.decode(str); | 39 _map = JSON.decode(str); |
| 68 } | 40 } |
| 69 | 41 |
| 70 @override | 42 dynamic operator[](String key) => _map[key]; |
| 71 dynamic operator [](String key) => _map[key]; | |
| 72 | 43 |
| 73 @override | 44 void operator[]=(String key, dynamic value) { |
| 74 void operator []=(String key, dynamic value) { | |
| 75 if (value == null) { | 45 if (value == null) { |
| 76 _map.remove(key); | 46 _map.remove(key); |
| 77 } else { | 47 } else { |
| 78 _map[key] = value; | 48 _map[key] = value; |
| 79 } | 49 } |
| 80 | 50 |
| 81 window.localStorage[name] = JSON.encode(_map); | 51 window.localStorage[name] = JSON.encode(_map); |
| 82 } | 52 } |
| 83 | |
| 84 @override | |
| 85 void syncSettings() {} | |
| 86 } | 53 } |
| OLD | NEW |