【问题标题】:Cookies are not loading first time in Gatsby with gatsby-plugin-gdpr-cookies and react-cookie-consentcookie 没有在 Gatsby 中首次加载 gatsby-plugin-gdpr-cookies 和 react-cookie-consent
【发布时间】:2021-01-20 00:14:52
【问题描述】:

解释:

此代码有效(使用 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>

【问题讨论】:

    标签: javascript reactjs cookies gatsby gatsby-plugin


    【解决方案1】:

    用途:

    import CookieConsent from "react-cookie-consent";
    import { useLocation } from "@reach/router" 
    import { initializeAndTrack } from 'gatsby-plugin-gdpr-cookies'
        
    <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={() => {
              initializeAndTrack(location) 
            }}
          >
            <p style={{ float: "left", }}> my text </p>
            <p style={{ float: "left", }}>my text</p>
          </CookieConsent> 
    

    gatsby-plugin-gdpr-cookies 支持tracking without refreshing the page using some built-in consumers

    导入助手后,您可以在 cookie 横幅回调中执行 initializeAndTrack(location)。这将使用您在 gatsby-config.js 中的选项初始化插件,然后根据接受的 cookie/服务开始跟踪用户。

    【讨论】:

    • 你把 const location = useLocation(); 放在哪里,因为我目前正在 gatsby-browser 中这样做,它会引发错误
    • 你的意思是import { useLocation } from "@reach/router"
    • 我将我的 cookie 同意移动到一个组件,现在这部分没有出错。然而,initializeAndTrack 似乎不起作用。在接受 cookie 后,在路由更改或刷新页面之前,Google 跟踪代码管理器不会初始化(触发 onAccept={() =&gt; { initializeAndTrack(location) }},就像上面的答案一样
    • 这就是图书馆的工作方式。来自文档:“此插件将在 Gatsby 的 onRouteUpdate 上触发一个名为 gatsbyRouteChange 的新事件(默认情况下)(仅在访问者同意的情况下)”。随意打开一个新的答案,而不是评论一个特定的实现。
    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 2021-03-18
    • 1970-01-01
    • 2021-06-08
    • 2019-10-17
    • 2023-03-09
    • 2020-01-04
    • 2019-04-02
    相关资源
    最近更新 更多