【问题标题】:How to configure gatsby-plugin-google-analytics with cookies consent?如何在 cookie 同意的情况下配置 gatsby-plugin-google-analytics?
【发布时间】:2020-01-04 05:49:58
【问题描述】:

我已经使用gatsby 开发了一个网站,我正在通过gatsby-plugin-google-analytics 使用谷歌分析插件,现在为了与用户友好,我想添加一个 cookie 同意,用户将有两个选项是否接受或拒绝使用 cookie。如果用户拒绝,那么我想停止谷歌分析来跟踪用户活动。我深入研究了他们的文档,但找不到实现这一目标的选项,有什么办法可以实现这一目标。

{
  resolve: `gatsby-plugin-google-analytics`,
  options: {
    trackingId: siteConfig.googleAnalyticsId,
    // Defines where to place the tracking script - `true` in the head and `false` in the body
    head: false,
  },
},

对于运动,我的gatsby-config.js 看起来像这样。如何实现这一目标。

提前谢谢你。

【问题讨论】:

    标签: google-analytics gatsby


    【解决方案1】:

    该插件仅以与 Gatsby 兼容的方式为您加载库和仪器页面跟踪调用。所有其他 Google Analytics 调用,包括 disabling measurement for a user 都与正常工作一样。

    由您决定:

    1. 构建并显示带有选择退出的 cookie 通知
    2. 记住用户何时选择退出
    3. 通过设置window['ga-disable-UA-XXXXX-Y'] = true 为用户(在进行任何ga() 调用之前)在每个页面加载时传达此信息

    您应该能够在从gatsby-browser.js 导出为onClientEntry 的函数中执行此操作。例如:

    export const onClientEntry = () => {
      if (userHasOptedOutOfThirdPartyTracking()) {
        window[`ga-disable-${process.env.GATSBY_GOOGLE_ANALYTICS_ID`] = true
      }
    }
    

    【讨论】:

      【解决方案2】:

      对于 Gatsby 网站,您可以在 gatsby-config.jsreact-cookie-consent 中组合使用 gatsby-plugin-gdpr-cookies 进行分析 cookie 管理,以显示 cookie 横幅。

      # using npm
      npm install --save gatsby-plugin-gdpr-cookies react-cookie-consent
      
      # using yarn
      yarn add gatsby-plugin-gdpr-cookies react-cookie-consent
      

      你可以找到更详细的教程here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-08
        • 2019-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-15
        • 2020-11-17
        相关资源
        最近更新 更多