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

1

7

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

Morrell answered 12/8, 2020 at 16:13 Comment(0)
M
5

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

Morrell answered 12/8, 2020 at 22:9 Comment(1)
Thank you for this post. I just started dealing with GDPR cookies and consent banner and I really had no Idea about how to tackle this. You made me understand quite a lot. I opened a question #63715143 and if could have a look at it I would appreciate it very much. Thanks again.Mcatee

© 2022 - 2024 — McMap. All rights reserved.