如何解决如何使用 react-cookie-consent 和 gatsby-plugin-gdpr-cookie 为 gatsby 网站正确设置 cookie 生存期?
我正在尝试建立一个 gatsby 站点,我想在其中设置(至少)谷歌分析 cookie。我已经安装了 react-cookie-consent 和 gatsby-plugin-gdpr-cookies。我的问题是:
为什么我通过 Cookieconsent 属性配置的 cookie 的生命周期总是保留在“会话”上?当它设置为 true 时。我试图配置道具 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']
}
},
解决方法
这是 Mastermindzh 本人(插件作者)给出的解决方案: 在设置 cookie 时,我们可以像这样直接传递 'expires' 属性:
Cookies.set('gatsby-gdpr-google-analytics','true',{ expires: 365 });
我测试了它,它按预期工作!!!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。