Skip to content

Commit b66e6c8

Browse files
committed
Saad Stat Tracking #7
1 parent 16dc196 commit b66e6c8

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

lib/src/codelessly.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ class Codelessly {
641641
if (_authManager?.authData?.projectId case String projectId) {
642642
tracker.init(
643643
projectId: projectId,
644-
serverUrl: Uri.parse(_config!.firebaseCloudFunctionsBaseURL),
644+
serverUrl: Uri.parse(
645+
'${_config!.firebaseCloudFunctionsBaseURL}/api/trackStatsRequest'),
645646
);
646647
}
647648

@@ -695,7 +696,8 @@ class Codelessly {
695696
if (_authManager?.authData?.projectId case String projectId) {
696697
tracker.init(
697698
projectId: projectId,
698-
serverUrl: Uri.parse(_config!.firebaseCloudFunctionsBaseURL),
699+
serverUrl: Uri.parse(
700+
'${_config!.firebaseCloudFunctionsBaseURL}/api/trackStatsRequest'),
699701
);
700702
}
701703

lib/src/constants.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ const String bundleDownloadsField = 'bundle_downloads';
6464
const String fontDownloadsField = 'font_downloads';
6565
const String actionsField = 'actions';
6666
const String cloudActionsField = 'cloud_actions';
67+
const String populatedLayoutDownloadsField = 'populated_layout_downloads';
68+
const String layoutViewsField = 'layout_views';
6769

6870
/// The template url for the svg icons.
6971
/// {{style}}: The style of the icon. e.g. materialiconsoutlined

lib/src/data/data_manager.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class DataManager {
5454
/// The error handler to use.
5555
final CodelesslyErrorHandler errorHandler;
5656

57-
// TODO(Saad): This is only used to initialize CloudDatabase. We may be able
58-
// to decouple it.
5957
/// The stat tracker to use, used to track various reads and writes in this
6058
/// data manager.
6159
final StatTracker tracker;
@@ -1301,6 +1299,7 @@ class DataManager {
13011299
log('\tLayout [$layoutID] has no fonts.');
13021300
}
13031301

1302+
tracker.trackPopulatedLayoutDownload(layoutID);
13041303
return true;
13051304
}
13061305

lib/src/logging/stat_tracker.dart

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:convert';
22

33
import 'package:codelessly_api/codelessly_api.dart';
4-
import 'package:collection/collection.dart';
54
import 'package:http/http.dart';
65
import 'package:meta/meta.dart';
76

@@ -32,6 +31,13 @@ abstract class StatTracker {
3231
/// Tracks one document write operation.
3332
Future<void> trackWrite(String label);
3433

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+
3541
/// Tracks one bundle download operation from the CDN.
3642
Future<void> trackBundleDownload();
3743

@@ -74,19 +80,12 @@ final class CodelesslyStatTracker extends StatTracker {
7480
Future<void> sendBatch() => debouncer.run(
7581
() async {
7682
// TODO(Saad): Use an HTTP client.
77-
post(
83+
await post(
7884
serverUrl!,
85+
headers: <String, String>{'Content-Type': 'application/json'},
7986
body: jsonEncode({
8087
'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,
9089
}),
9190
);
9291
statBatch.clear();
@@ -114,6 +113,22 @@ final class CodelesslyStatTracker extends StatTracker {
114113
return sendBatch();
115114
}
116115

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+
117132
@override
118133
Future<void> trackBundleDownload() {
119134
if (disabled) return Future.value();

0 commit comments

Comments
 (0)