Disable analytics packages until users accept GDPR cookie consent

Thanks for these great Analytics packages!
I would like to disable them, until the user accept my Cookie consent (as much as I know, this is the law in EU). I wish to use this npm package, which hase an onAccept prop.

So I set up my frontity.settings.js file, as the following:

const settings = {
  name: 'MyProjectName',
  state: {
    frontity: {
      ...
    },
    analytics: {
      pageviews: {
        googleAnalytics: false
      },
      events: {
        googleAnalytics: false
      }
    }
  },
  packages: [
    ...
    {
      name: '@frontity/google-analytics',
      state: {
        googleAnalytics: {
          trackingId: 'UA-123456789-1'
        }
      }
    },
    ...
    '@frontity/yoast'
  ]
}

export default settings

And I created an action in the /myTheme/src/index.js file:

import Theme from './components'

const myTheme = {
  name: 'myThemeName',
  roots: {
    theme: Theme
  },
 ...
  // Actions are functions that modify the state or deal with other parts of
  // Frontity like libraries.
  actions: {
    theme: {
    ...
    },
    analytics: {
      enableAnalytics: ({ state }) => {
        state.analytics.pageviews.googleAnalytics = true
        state.analytics.events.googleAnalytics = true
      }
    }
  },
...
}

export default chakraTheme

I can call it in my components/index.js file, as following:

<CookieConsent
  onAccept={() => { actions.analytics.enableAnalytics() }}
>

It’s working great! My only problem is, that even these steps, google analytics cookies will be created on pageload :frowning: .
Any suggestion?

Probably I need something like this.
I tried it in my components/index.js:

useEffect(() => {
  if (window) {
    window['ga-disable-UA-MY_ANALYTICS_ID-1'] = true
  }
}, [])

but the cookies are still created… :frowning:

Hi @koli14,

The Analytics packages are a group of analytics packages grouped to ease its implementation on sites (so you can for example, track some events to different services at the same time)

Each analytics service has its own package, and each one of these packages is in charge of loading the needed javascript file

In the current state of these packages, the javascript files related to each package, are always loaded whenever you add the package in the frontity.settings.js

I assume the cookies are automatically created when you load these files so the solution may come from loading these files only when a specific condition is met

You can open a Feature Discussion or directly modify the code of these packages to add this behaviour to these packages