Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 494
Dietrich Bollmann Avatar asked Oct 16 '22 01:10

Dietrich Bollmann


1 Answers

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:

  • Disable Google Analytics measurement https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out

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

  • What happened to my Tracking ID? https://support.google.com/analytics/answer/9539598

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

like image 143
Dietrich Bollmann Avatar answered Oct 31 '22 14:10

Dietrich Bollmann