Google Analytics for Next.js 13: Consent Mode with GDPR Compliant Cookie Banner

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ม.ค. 2025

ความคิดเห็น • 66

  • @RyanGaudion
    @RyanGaudion  ปีที่แล้ว +1

    🔗 For more details, check out the accompanying blog post: gaudion.dev/blog/setup-google-analytics-with-gdpr-compliant-cookie-consent-in-nextjs13

  • @randymartinez3999
    @randymartinez3999 ปีที่แล้ว +3

    Awesome video, gave me a better understanding of Google Analytics. Looking forward for those extra material of changing consent and asking in 6 months times. Thank you for the work! Keep it up.

  • @joshtriedcoding
    @joshtriedcoding ปีที่แล้ว +2

    Great video, thanks for sharing

    • @RyanGaudion
      @RyanGaudion  ปีที่แล้ว

      Thanks for watching!

    • @RyanGaudion
      @RyanGaudion  ปีที่แล้ว

      Thanks for watching!

    • @AlgorithmDoctor
      @AlgorithmDoctor ปีที่แล้ว

      I love the video, It has helped me.
      If @joshtriedcoding likes it, then you've earned my subscription.❤❤

  • @pplytas
    @pplytas ปีที่แล้ว +1

    Amazing video. Clear to the point and helpful. Thank you so much!

  • @lesliewubbel9157
    @lesliewubbel9157 ปีที่แล้ว +2

    Hands down best youtube tutorial I have experienced

  • @dawn_signal
    @dawn_signal ปีที่แล้ว +1

    thanks a lot for making this. 👍👍 looking forward to more Next13 vids.

    • @RyanGaudion
      @RyanGaudion  ปีที่แล้ว

      Glad you liked it! New content coming soon 🤞

  • @appstuff6565
    @appstuff6565 ปีที่แล้ว +1

    Hey Ryan, its recommended to use GTM these days, are you familiar with it and how can we do with that, a follow up video to this would perhaps complete the whole analytics implementation because yours is quite a well documented video compared to others on youtube. Thanks.

  • @johnfstockland
    @johnfstockland 8 หลายเดือนก่อน +1

    Very helpful tutorial. However, I'm running into the issue that localStorage updates fine if I press Decline or Accept, but Gtag doesn't refetch (i.e. I only see the initial Collect request with G1-0, when I then click accept and update localstorage it does not actually refetch to update the new consent)

  • @MaxRohowsky
    @MaxRohowsky 5 หลายเดือนก่อน

    Commenting for the algo. Helped!

  • @raman9359
    @raman9359 ปีที่แล้ว +1

    Thank you! It is exactly what I was looking for!

  • @ICOReviewtoken
    @ICOReviewtoken ปีที่แล้ว +1

    I want to get information from API such as page view, realtime active, what should I do? Old tutorials use Next/Route but in nextjs 13 useRouter is imported from Next/Navigation

  • @somidhroy5048
    @somidhroy5048 ปีที่แล้ว +3

    Can anyone explain me one thing(could be a silly thing which i am not able to understand)
    so here:
    const [cookieConsent, setCookieConsent] = useState(false)
    we have setted cookieConsent default value to false, and here
    ${
    cookieConsent !== null ? "hidden" : "flex"
    } px-3 md:px-4 py-3 justify-between items-center flex-col sm:flex-row gap-4
    bg-gray-700 rounded-lg shadow`}
    we have put a condition that if the cookieConsent is null then only show the cookie banner but here cookie consent will never be null because the default value is false no?
    help me with this anyone please

    • @prathameshjadhav2047
      @prathameshjadhav2047 ปีที่แล้ว

      Change this line your code will work fine
      const [cookieConsent, setCookieConsent] = useState(getLocalStorage("cookie_consent", false));

  • @mdstudio8139
    @mdstudio8139 ปีที่แล้ว +3

    Anyone else always getting cookie_consent false and CookieBanner not re rendering using this code ?

  • @musaamuhsen
    @musaamuhsen 5 หลายเดือนก่อน

    dude this video saved me thanks so much

  • @basith237
    @basith237 ปีที่แล้ว +3

    hi, i found a bug on the result of static rendering, it's produce id="__next_error__" and failed to generate static page. the cause of this bug is because we use useSearchParams on this goole analytics component , from next.js docs: "If a route is statically rendered, calling useSearchParams() will cause the tree up to the closest Suspense boundary to be client-side rendered." So we need wrap component with

    • @abed.shaaban
      @abed.shaaban ปีที่แล้ว

      still getting the error after adding suspense, what should i do and what should the fallback be knowing that it will be injected in the tag and not inside the tag which is shown in the layout.tsx

    • @basith237
      @basith237 ปีที่แล้ว

      @@abed.shaaban what kind of error, is it __next-error__ like above?

    • @abed.shaaban
      @abed.shaaban ปีที่แล้ว

      ​@@basith237 warn Entire page /.../[slug] deopted into client-side rendering.

  • @ogbillity
    @ogbillity ปีที่แล้ว +1

    Excellent tutorial. Thank you so much.

  • @edge-rps
    @edge-rps ปีที่แล้ว

    Thank you for sharing, helped me huge!

  • @mykarelami
    @mykarelami ปีที่แล้ว

    thanks for an amazing blog on remote mdx in next js 😘

  • @basith237
    @basith237 ปีที่แล้ว +7

    hi, sorry, i think this code shoulde useState(
    getLocalStorage("cookie_consent", null)
    ) not useState(false)
    const [cookieConsent, setCookieConsent] = useState(false);
    useState(false) on this line of code means that the user already decline the cookie and make cookie banner not show
    useState(
    getLocalStorage("cookie_consent", null)
    ) make default value is null if user not decide decline or accept cookies
    cmiiw, thanks for this tutorials, it's really helpful to me

    • @ihtishamulhaq5429
      @ihtishamulhaq5429 ปีที่แล้ว

      sure, I will try this in my implementation, lets see

    • @udaiabojabal4896
      @udaiabojabal4896 6 วันที่ผ่านมา

      doing so will cause an error on the server.
      if you initiate with false, it's not work in development mode but it will work well in production

  • @OmarPalenzuela
    @OmarPalenzuela ปีที่แล้ว +2

    Hi, I don't know if you could help me, when I build the application the SSR is lost due to the GA integration. Could you help me?

    • @RyanGaudion
      @RyanGaudion  ปีที่แล้ว +1

      Hey Omar, check out the pinned comment - this is a known bug in Next.js but can be fixed with a Suspense boundary

  • @ihtishamulhaq5429
    @ihtishamulhaq5429 ปีที่แล้ว

    Saved my day thank you for the video, helped alot 🙂

  • @ilirbajrami2902
    @ilirbajrami2902 ปีที่แล้ว +1

    How can we detect all the cookies (not only GA) and create a consent banner to choose which ones to keep and which ones to delete. Im willing to pay for a course about this.

    • @JeNNySS
      @JeNNySS ปีที่แล้ว +1

      Yes! I want to know it too!

  • @appstuff6565
    @appstuff6565 ปีที่แล้ว

    Hey Ryan, Thanks for this. Im using NextJS 13.5 with supabase. Im creating a hobby project to understand how things work in web dev. Right now, Im at analytics stage, I have a doubt around them.
    1. Calculating the views and incoming traffic on my website and how many signed up. This one seems fairly easily and tutorials are available so I understand. However,
    2. Since I have users and their profile specific dashboard, I want my users to see as well how many people or visits my users had, and since linktree style, how many times each button was click. Now I dont want just anyone to see any others, but only the user can see their analytic insight from how many profile page visits they had and the buttons/links clicked on their profile page.
    Do you think #2 its possible with google analytics? If so, can you briefly guide me to the right direction? Thanks!

  • @PaxProfundisAeterna
    @PaxProfundisAeterna ปีที่แล้ว +1

    I needed envolve Cookie Banner in body and Google Analytics in head with suspense , there is some other configuration without use searchparams ? I new in nextjs and this take some time to resolve kkkkk 😵‍💫

  • @SaurabhSrivastava-i1q
    @SaurabhSrivastava-i1q ปีที่แล้ว

    How to get the _ga client id from the cookie in NextJS 13?

  • @nj-tastycreations8307
    @nj-tastycreations8307 29 วันที่ผ่านมา

    where the null come from

  • @rithick3280
    @rithick3280 ปีที่แล้ว

    I receive url is not defined error message

  • @LukasWeijl
    @LukasWeijl ปีที่แล้ว +1

    My program fails due to it says that window.gtag is not a function. do you hava a solution for this? Im using next14.0.3

    • @LukasWeijl
      @LukasWeijl ปีที่แล้ว

      Managed to fix it.

    • @aristotelispallasidis
      @aristotelispallasidis ปีที่แล้ว

      @@LukasWeijl How?

    • @LukasWeijl
      @LukasWeijl ปีที่แล้ว +1

      @@aristotelispallasidis I'm not actually sure but, in the beginning, I used wrapper around my . I also turned off Strictmode because it made my banner always show due to the useeffect always running twice and making the local storage set to faulty value. My URL doesn't use searchparams as well.

    • @LukasWeijl
      @LukasWeijl ปีที่แล้ว +1

      Check these things otherwise, it might be something else as well. Check your Node version to see if that is your fault.

  • @BensTechLab
    @BensTechLab ปีที่แล้ว

    Well done! Fantastic tutorial. I'm curious what percentage of users deny cookies...

  • @terry_swd
    @terry_swd ปีที่แล้ว

    great job, thanks for the vid!

  • @svennijhuis2859
    @svennijhuis2859 ปีที่แล้ว +1

    Thank you for your video. I have a question regarding an issue I'm experiencing. Although I have successfully added OpenGraph metadata to my website, it seems to conflict with the GoogleAnalytics component. I would like to know if you have any insights into what might be causing this problem and how to resolve it?

    • @RyanGaudion
      @RyanGaudion  ปีที่แล้ว +2

      This is a known bug and can be resolved by wrapping your Analytics components in the root layout with a Suspense. Hopefully Next.js fix soon: github.com/vercel/next.js/issues/48442#issuecomment-1519139562

    • @svennijhuis2859
      @svennijhuis2859 ปีที่แล้ว +1

      ​@@RyanGaudion Thank you! It was a straightforward fix. I appreciate your help.

    • @mogo6798
      @mogo6798 ปีที่แล้ว +1

      @@RyanGaudion After wrapping the Analytics component, a new error, 'TypeError: window.gtag is not a function,' occurs in the CookieBanner. This issue only arises when I use suspense. Do you have any idea why?

  • @djelasrca8091
    @djelasrca8091 ปีที่แล้ว +4

    I cant see collect message in network tab

    • @nerd_abroad
      @nerd_abroad ปีที่แล้ว

      Same

    • @djelasrca8091
      @djelasrca8091 ปีที่แล้ว +2

      @@nerd_abroad ad blocker in your browser

    • @nerd_abroad
      @nerd_abroad ปีที่แล้ว

      @@djelasrca8091 Thanks! Also my pageviews are being double counted. Any idea why?

    • @djelasrca8091
      @djelasrca8091 ปีที่แล้ว

      No clue.

    • @Assoehosselaar
      @Assoehosselaar ปีที่แล้ว

      @@nerd_abroad useeffect runs twice because of in dev env. It does not happen in the production build

  • @suaeb175
    @suaeb175 ปีที่แล้ว

    Thanks 🎉

  • @kingsley577
    @kingsley577 ปีที่แล้ว

    Really helpful 🙌

  • @husseinhassan2389
    @husseinhassan2389 8 หลายเดือนก่อน

    thanks!

  • @TRAGICbliss
    @TRAGICbliss ปีที่แล้ว

    Super helpful!

  • @mehdijafarzade2381
    @mehdijafarzade2381 11 หลายเดือนก่อน

    Awesome bro

  • @BelalKhalifa-t1b
    @BelalKhalifa-t1b ปีที่แล้ว

    More cookie please