@@ -333,26 +333,16 @@ class AnalyticsImpl implements Analytics {
333333 /// message has been updated by the package.
334334 late bool _showMessage;
335335
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-
349336 /// When set to `true` , various assert statements will be enabled
350337 /// to ensure usage of this class is within GA4 limitations.
351338 final bool _enableAsserts;
352339
353340 /// Telemetry suppression flag that is set via [Analytics.suppressTelemetry] .
354341 bool _telemetrySuppressed = false ;
355342
343+ /// Indicates if this is the first run for a given tool.
344+ bool _firstRun = false ;
345+
356346 /// The list of futures that will contain all of the send events
357347 /// from the [GAClient] .
358348 final _futures = < Future <Response >> [];
@@ -388,7 +378,13 @@ class AnalyticsImpl implements Analytics {
388378 toolsMessageVersion: toolsMessageVersion,
389379 );
390380 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+ }
392388
393389 // Create the config handler that will parse the config file
394390 _configHandler = ConfigHandler (
@@ -397,13 +393,6 @@ class AnalyticsImpl implements Analytics {
397393 initializer: initializer,
398394 );
399395
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-
407396 // Check if the tool has already been onboarded, and if it
408397 // has, check if the latest message version is greater to
409398 // prompt the client to show a message
@@ -414,6 +403,11 @@ class AnalyticsImpl implements Analytics {
414403 _configHandler.parsedTools[tool.label]? .versionNumber ?? - 1 ;
415404 if (currentVersion < toolsMessageVersion) {
416405 _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 ;
417411 }
418412
419413 _clientIdFile = fs.file (
@@ -464,22 +458,19 @@ class AnalyticsImpl implements Analytics {
464458 /// Checking the [telemetryEnabled] boolean reflects what the
465459 /// config file reflects.
466460 ///
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.
474465 ///
475466 /// If the user has suppressed telemetry [_telemetrySuppressed] will
476467 /// 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] .
477471 @override
478472 bool get okToSend =>
479- telemetryEnabled &&
480- ! _showMessage &&
481- _clientShowedMessage &&
482- ! _telemetrySuppressed;
473+ telemetryEnabled && ! _showMessage && ! _telemetrySuppressed && ! _firstRun;
483474
484475 @override
485476 Map <String , ToolInfo > get parsedTools => _configHandler.parsedTools;
@@ -496,22 +487,24 @@ class AnalyticsImpl implements Analytics {
496487
497488 @override
498489 void clientShowedMessage () {
490+ // Check the tool needs to be added to the config file
499491 if (! _configHandler.parsedTools.containsKey (tool.label)) {
500492 _configHandler.addTool (
501493 tool: tool.label,
502494 versionNumber: toolsMessageVersion,
503495 );
504- _showMessage = true ;
505496 }
497+
498+ // When the tool already exists but the consent message version
499+ // has been updated
506500 if (_configHandler.parsedTools[tool.label]! .versionNumber <
507501 toolsMessageVersion) {
508502 _configHandler.incrementToolVersion (
509503 tool: tool.label,
510504 newVersionNumber: toolsMessageVersion,
511505 );
512- _showMessage = true ;
513506 }
514- _clientShowedMessage = true ;
507+ _showMessage = false ;
515508 }
516509
517510 @override
0 commit comments