【问题标题】:How to setup correctly the cookie lifetime using react-cookie-consent and gatsby-plugin-gdpr-cookie for gatsby websites?如何使用 react-cookie-consent 和 gatsby-plugin-gdpr-cookie 为 gatsby 网站正确设置 cookie 生命周期?
【发布时间】:2021-07-18 08:05:40
【问题描述】:

我正在尝试建立一个 gatsby 网站,我想在其中设置(至少)谷歌分析 cookie。我已经安装了 react-cookie-consent 以及 gatsby-plugin-gdpr-cookies。我的问题是:

为什么我通过 Cookieconsent 属性配置的 cookie 的生命周期始终保持在“会话”上?当它设置为真。我尝试配置 prop expires = {365} 但它也不起作用。

谢谢!

在我的 Index.js 中:

      <CookieConsent
// debug
location='bottom'
style={{ background: 'rgb(13 168 220)' }}
buttonStyle={{ background: 'rgb(148 130 110)', fontSize: '13px' }}
buttonText='Accept'
declineButtonText='decline'
expires={365}
onAccept={() => {
  // alert('Accept was triggered by clicking the Accept button');
  Cookies.set('gatsby-gdpr-google-analytics', 'true');
}}
enableDeclineButton
setDeclineCookie
cookieName='gatsby-gdpr-google-analytics'

本网站使用 cookie 来增强用户体验。请参阅我们的隐私政策了解更多信息。

在 gatsby-config.js 文件中:

plugins: [
{
  resolve: 'gatsby-plugin-gdpr-cookies',
  options: {
    googleAnalytics: {
      trackingId: 'MY-ID', // leave empty if you want to disable the tracker
      cookieName: 'gatsby-gdpr-google-analytics', // here can you change the cookie name
      anonymize: true // default
    },
    googleTagManager: {
      trackingId: 'YOUR_GOOGLE_TAG_MANAGER_TRACKING_ID', // leave empty if you want to disable the tracker
      cookieName: 'gatsby-gdpr-google-tagmanager', // // here can you change the cookie name
      dataLayerName: 'dataLayer' // default
    },
    facebookPixel: {
      pixelId: 'YOUR_FACEBOOK_PIXEL_ID', // leave empty if you want to disable the tracker
      cookieName: 'gatsby-gdpr-facebook-pixel' // // here can you change the cookie name
    },
    // defines the environments where the tracking should be available  - default is ["production"]
    environments: ['production', 'development']
  }
},

【问题讨论】:

    标签: cookies lifetime gatsby-plugin gdprconsentform cookieconsent


    【解决方案1】:

    这里是Mastermindzh本人(插件作者)给出的解决方案: 设置 cookie 时,我们可以像这样直接传递 'expires' 属性:

    Cookies.set('gatsby-gdpr-google-analytics', 'true', { expires: 365 });
    

    我已经测试过了,它按预期工作!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 2021-03-18
      • 2012-08-06
      • 1970-01-01
      • 2019-10-06
      • 2020-01-04
      • 2020-11-08
      • 2020-11-18
      相关资源
      最近更新 更多