Google Analytics, Flutter, cookies and the General Data Protection Regulation (GDPR)

1,024

I found a working solution:

Before calling firebase.analytics() in index.html the variable ga-disable-G-FOO0490BAR has to be set to true - where G-FOO0490BAR corresponds to the google analytics measurement ID or the tracking ID of your project.

<script>
  // Initialize Google Analytics
  var measurementID = "G-FOO0490BAR";
  window['ga-disable-' + measurementID] = true;
  firebase.analytics();
</script>

I couldn't find this ID anywhere, neither in the Google nor in the Firebase Analytics console. But after installing and activating the Google Analytics Debugger plugin it was shown in the the chrome browser console when sending google analytics events.

For example:

Processing GTAG command: ["event", "test_event", {string: "Hello, World!", send_to: "G-FOO0490BAR"}]

Now I can enable / disable google analytics from Flutter with the following code:

// Enable / disable Google Analytics
FirebaseAnalytics analytics = getAnalytics();
analytics.setAnalyticsCollectionEnabled(usersAnswer);

No cookies are set when Google Analytics is disabled.

I found this solution on the following page:

An explanation of the ID to be used can be found here:

However, contrary to what is written on the latter page in my case no measurement ID was shown in the upper right portion of the panel.

Cheers, Dietrich

Share:
1,024
Dietrich Bollmann
Author by

Dietrich Bollmann

Updated on December 01, 2022

Comments

  • Dietrich Bollmann
    Dietrich Bollmann over 1 year

    Following the General Data Protection Regulation (GDPR) the user has to give his consent before any cookie is set.

    I use google / firebase analytics in a Flutter web site. When the user opens a page immediately two cookies are stored: _ga and _ga_SOMECODE.

    How can I make google analytics postpone these cookies so that I can first ask the user's consent and leave to him the choice to allow a cookie for the use of google analytics - or to reject both?

    I am looking for something like this:

    • in index.html

      // Initialize Google Analytics
      <script>
        var analyticsConfig = { enabled: false };
        firebase.analytics(analyticsConfig);
      </script>
      
    • Somewhere on the first page:

      Accept statistics cookies? [yes] [no]

      and in the corresponding dart code:

      // Enable / disable Google Analytics
      FirebaseAnalytics analytics = getAnalytics();
      analytics.setAnalyticsCollectionEnabled(usersAnswer);
      

    In the ideal case the analytics code would only be loaded when the user provided a positive answer. In the case the user's choice would be [no] the analytics code in the code base would do nothing.

    How can I implement something similar?

    Thanks, Dietrich