解释:
此代码有效(使用 gatsby develop 或 gatsby build && gatsby serve),但如果我在没有 onAccept={() => window.location.reload()} 的情况下执行我的代码,则无法正常工作,因为只需加载我的 cookie -> (gatsby -gdpr-google-analytics = true) 但没有同时加载 ga cookie,我必须使用 onAccept 刷新。知道如何在不强制刷新的情况下同时加载所有 cookie 吗?
我的代码:
// gatsby-config.js
{
resolve: `gatsby-plugin-gdpr-cookies`,
options: {
googleAnalytics: {
trackingId: 'my_tracking_id', // leave empty if you want to disable the tracker
cookieName: 'gatsby-gdpr-google-analytics', // default
includeInDevelopment: true,
head: true
},
// defines the environments where the tracking should be available - default is ["production"]
environments: ['production', 'development']
},
},
// index.js
import CookieConsent from "react-cookie-consent";
<CookieConsent
overlay
id="#consentModal"
location="bottom"
buttonText="ACCEPT"
declineButtonText="DECLINE"
cookieName="gatsby-gdpr-google-analytics"
expires={150}
enableDeclineButton
style={{
background: "black",
display: "flex",
padding: "10px"
}}
buttonStyle={{
display: "inline-flex",
background: "#fff",
color: "red",
borderRadius: "4px",
justifyContent: "middle",
width: "85px",
}}
declineButtonStyle={{
display: "inline-flex",
background: "#ff5f56",
borderRadius: "4px",
color: "#fff",
cursor: "pointer",
width: "85px"
}}
onAccept={() => window.location.reload()} //I would like to remove this refresh
>
<p style={{ float: "left", }}> my text </p>
<p style={{ float: "left", }}>my text</p>
</CookieConsent>