Skip to content

Commit dc787b5

Browse files
committed
Enable stat tracking.
1 parent 3de7a3d commit dc787b5

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/src/constants.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const String templatesPath = 'templates';
5858
/// STAT TRACKING
5959
const String statsCollection = 'stats';
6060
const String lostStatsDoc = '_lost';
61+
const String loadsField = 'loads';
6162
const String writesField = 'writes';
6263
const String readsField = 'reads';
6364
const String bundleDownloadsField = 'bundle_downloads';

lib/src/logging/stat_tracker.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:convert';
23

34
import 'package:codelessly_api/codelessly_api.dart';
@@ -7,7 +8,7 @@ import 'package:meta/meta.dart';
78
import '../../codelessly_sdk.dart';
89
import '../utils/debouncer.dart';
910

10-
const _kDisableStatReporting = true;
11+
const _kDisableStatReporting = false;
1112

1213
/// A class that tracks statistics of various operations in the SDK.
1314
abstract class StatTracker {
@@ -27,6 +28,9 @@ abstract class StatTracker {
2728
this.serverUrl = serverUrl;
2829
}
2930

31+
/// Tracks one complete visual view of a Codelessly CloudUI Layout.
32+
Future<void> trackLoad();
33+
3034
/// Tracks one document read operation.
3135
Future<void> trackRead(String label);
3236

@@ -87,14 +91,14 @@ final class CodelesslyStatTracker extends StatTracker {
8791
}
8892

8993
// TODO(Saad): Use an HTTP client.
90-
await post(
94+
unawaited(post(
9195
serverUrl!,
9296
headers: <String, String>{'Content-Type': 'application/json'},
9397
body: jsonEncode({
9498
'projectId': projectId,
9599
'stats': statBatch,
96100
}),
97-
);
101+
));
98102
statBatch.clear();
99103
},
100104
forceRunAfter: 20,
@@ -104,6 +108,14 @@ final class CodelesslyStatTracker extends StatTracker {
104108
statBatch[field] = (statBatch[field] ?? 0) + 1;
105109
}
106110

111+
@override
112+
Future<void> trackLoad() {
113+
if (disabled) return Future.value();
114+
115+
incrementField(loadsField);
116+
return sendBatch();
117+
}
118+
107119
@override
108120
Future<void> trackRead(String label) {
109121
if (disabled) return Future.value();

lib/src/ui/codelessly_widget.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,18 @@ class _CodelesslyWidgetState extends State<CodelesslyWidget> {
264264
StreamSubscription<CodelesslyException?>? _exceptionSubscription;
265265
CodelesslyException? _lastException;
266266

267-
// Saved in the state for the didChangeDependencies method to use to compare
268-
// with the new canvas ID to determine if the layout needs to be reloaded
269-
// when the media query changes resulting into a new breakpoint.
267+
/// Saved in the state for the didChangeDependencies method to use to compare
268+
/// with the new canvas ID to determine if the layout needs to be reloaded
269+
/// when the media query changes resulting into a new breakpoint.
270270
String? canvasID;
271271

272-
// Saved in the state for the didChangeDependencies method to use.
272+
/// Saved in the state for the didChangeDependencies method to use.
273273
String? effectiveLayoutID;
274274

275+
/// Tracks whether this widget went through a full layout load and was made
276+
/// visible to the user successfully at least once.
277+
bool didView = false;
278+
275279
@override
276280
void initState() {
277281
super.initState();
@@ -548,6 +552,11 @@ class _CodelesslyWidgetState extends State<CodelesslyWidget> {
548552
canvasID ??= _getCanvasIDForLayoutGroup(
549553
effectiveLayoutID, model, MediaQuery.sizeOf(context));
550554

555+
if (!didView) {
556+
didView = true;
557+
_effectiveCodelessly.tracker.trackLoad();
558+
}
559+
551560
final layoutWidget = Material(
552561
clipBehavior: Clip.none,
553562
type: MaterialType.transparency,

0 commit comments

Comments
 (0)