|
1 | 1 | import 'dart:convert'; |
2 | 2 |
|
3 | 3 | import 'package:codelessly_api/codelessly_api.dart'; |
4 | | -import 'package:collection/collection.dart'; |
5 | 4 | import 'package:http/http.dart'; |
6 | 5 | import 'package:meta/meta.dart'; |
7 | 6 |
|
@@ -32,6 +31,13 @@ abstract class StatTracker { |
32 | 31 | /// Tracks one document write operation. |
33 | 32 | Future<void> trackWrite(String label); |
34 | 33 |
|
| 34 | + /// Tracks one complete populated layout download operation. |
| 35 | + Future<void> trackPopulatedLayoutDownload(String label); |
| 36 | + |
| 37 | + /// Tracks a layout as being viewed, determined by the life cycle of the |
| 38 | + /// CodelesslyWidget. |
| 39 | + Future<void> trackLayoutView(String label); |
| 40 | + |
35 | 41 | /// Tracks one bundle download operation from the CDN. |
36 | 42 | Future<void> trackBundleDownload(); |
37 | 43 |
|
@@ -74,19 +80,12 @@ final class CodelesslyStatTracker extends StatTracker { |
74 | 80 | Future<void> sendBatch() => debouncer.run( |
75 | 81 | () async { |
76 | 82 | // TODO(Saad): Use an HTTP client. |
77 | | - post( |
| 83 | + await post( |
78 | 84 | serverUrl!, |
| 85 | + headers: <String, String>{'Content-Type': 'application/json'}, |
79 | 86 | body: jsonEncode({ |
80 | 87 | 'projectId': projectId, |
81 | | - 'stats': { |
82 | | - for (final entry in statBatch.entries |
83 | | - .whereNot((entry) => entry.key == writesField)) |
84 | | - entry.key: entry.value, |
85 | | - |
86 | | - // Account for this stat tracking operation as an additional write |
87 | | - // operation. |
88 | | - writesField: (statBatch[writesField] ?? 0) + 1, |
89 | | - }, |
| 88 | + 'stats': statBatch, |
90 | 89 | }), |
91 | 90 | ); |
92 | 91 | statBatch.clear(); |
@@ -114,6 +113,22 @@ final class CodelesslyStatTracker extends StatTracker { |
114 | 113 | return sendBatch(); |
115 | 114 | } |
116 | 115 |
|
| 116 | + @override |
| 117 | + Future<void> trackPopulatedLayoutDownload(String label) { |
| 118 | + if (disabled) return Future.value(); |
| 119 | + |
| 120 | + incrementField('$populatedLayoutDownloadsField/$label'); |
| 121 | + return sendBatch(); |
| 122 | + } |
| 123 | + |
| 124 | + @override |
| 125 | + Future<void> trackLayoutView(String label) { |
| 126 | + if (disabled) return Future.value(); |
| 127 | + |
| 128 | + incrementField('$layoutViewsField/$label'); |
| 129 | + return sendBatch(); |
| 130 | + } |
| 131 | + |
117 | 132 | @override |
118 | 133 | Future<void> trackBundleDownload() { |
119 | 134 | if (disabled) return Future.value(); |
|
0 commit comments