@@ -333,26 +333,16 @@ class AnalyticsImpl implements Analytics {
333
333
/// message has been updated by the package.
334
334
late bool _showMessage;
335
335
336
- /// This will be switch to true once it has been confirmed by the
337
- /// client using this package that they have shown the
338
- /// consent message to the developer.
339
- ///
340
- /// If the tool using this package as already shown the consent message
341
- /// and it has been added to the config file, it will be set as true.
342
- ///
343
- /// It will also be set to true once the tool using this package has
344
- /// invoked [clientShowedMessage] .
345
- ///
346
- /// If this is false, all events will be blocked from being sent.
347
- bool _clientShowedMessage = false ;
348
-
349
336
/// When set to `true` , various assert statements will be enabled
350
337
/// to ensure usage of this class is within GA4 limitations.
351
338
final bool _enableAsserts;
352
339
353
340
/// Telemetry suppression flag that is set via [Analytics.suppressTelemetry] .
354
341
bool _telemetrySuppressed = false ;
355
342
343
+ /// Indicates if this is the first run for a given tool.
344
+ bool _firstRun = false ;
345
+
356
346
/// The list of futures that will contain all of the send events
357
347
/// from the [GAClient] .
358
348
final _futures = < Future <Response >> [];
@@ -388,7 +378,13 @@ class AnalyticsImpl implements Analytics {
388
378
toolsMessageVersion: toolsMessageVersion,
389
379
);
390
380
initializer.run ();
391
- _showMessage = initializer.firstRun;
381
+ if (initializer.firstRun) {
382
+ _showMessage = true ;
383
+ _firstRun = true ;
384
+ } else {
385
+ _showMessage = false ;
386
+ _firstRun = false ;
387
+ }
392
388
393
389
// Create the config handler that will parse the config file
394
390
_configHandler = ConfigHandler (
@@ -397,13 +393,6 @@ class AnalyticsImpl implements Analytics {
397
393
initializer: initializer,
398
394
);
399
395
400
- // If the tool has already been added to the config file
401
- // we can assume that the client has successfully shown
402
- // the consent message
403
- if (_configHandler.parsedTools.containsKey (tool.label)) {
404
- _clientShowedMessage = true ;
405
- }
406
-
407
396
// Check if the tool has already been onboarded, and if it
408
397
// has, check if the latest message version is greater to
409
398
// prompt the client to show a message
@@ -414,6 +403,11 @@ class AnalyticsImpl implements Analytics {
414
403
_configHandler.parsedTools[tool.label]? .versionNumber ?? - 1 ;
415
404
if (currentVersion < toolsMessageVersion) {
416
405
_showMessage = true ;
406
+
407
+ // If the message version has been updated, it will be considered
408
+ // as if it was a first run and any events attempting to get sent
409
+ // will be blocked
410
+ _firstRun = true ;
417
411
}
418
412
419
413
_clientIdFile = fs.file (
@@ -464,22 +458,19 @@ class AnalyticsImpl implements Analytics {
464
458
/// Checking the [telemetryEnabled] boolean reflects what the
465
459
/// config file reflects.
466
460
///
467
- /// Checking the [_showMessage] boolean indicates if this the first
468
- /// time the tool is using analytics or if there has been an update
469
- /// the messaging found in constants.dart - in both cases, analytics
470
- /// will not be sent until the second time the tool is used.
471
- ///
472
- /// Additionally, if the client has not invoked
473
- /// [Analytics.clientShowedMessage] , then no events shall be sent.
461
+ /// Checking the [_showMessage] boolean indicates if the consent
462
+ /// message has been shown for the user, this boolean is set to `true`
463
+ /// when the tool using this package invokes the [clientShowedMessage]
464
+ /// method.
474
465
///
475
466
/// If the user has suppressed telemetry [_telemetrySuppressed] will
476
467
/// return `true` to prevent events from being sent for current invocation.
468
+ ///
469
+ /// Checking if it is the first time a tool is running with this package
470
+ /// as indicated by [_firstRun] .
477
471
@override
478
472
bool get okToSend =>
479
- telemetryEnabled &&
480
- ! _showMessage &&
481
- _clientShowedMessage &&
482
- ! _telemetrySuppressed;
473
+ telemetryEnabled && ! _showMessage && ! _telemetrySuppressed && ! _firstRun;
483
474
484
475
@override
485
476
Map <String , ToolInfo > get parsedTools => _configHandler.parsedTools;
@@ -496,22 +487,24 @@ class AnalyticsImpl implements Analytics {
496
487
497
488
@override
498
489
void clientShowedMessage () {
490
+ // Check the tool needs to be added to the config file
499
491
if (! _configHandler.parsedTools.containsKey (tool.label)) {
500
492
_configHandler.addTool (
501
493
tool: tool.label,
502
494
versionNumber: toolsMessageVersion,
503
495
);
504
- _showMessage = true ;
505
496
}
497
+
498
+ // When the tool already exists but the consent message version
499
+ // has been updated
506
500
if (_configHandler.parsedTools[tool.label]! .versionNumber <
507
501
toolsMessageVersion) {
508
502
_configHandler.incrementToolVersion (
509
503
tool: tool.label,
510
504
newVersionNumber: toolsMessageVersion,
511
505
);
512
- _showMessage = true ;
513
506
}
514
- _clientShowedMessage = true ;
507
+ _showMessage = false ;
515
508
}
516
509
517
510
@override
0 commit comments