It appears that you have integrated a cookie consent banner into your WordPress website and are using Google Tag Manager (GTM) and Google Analytics. The script you've provided is meant to handle user consent for cookies and then adjust the settings for Google Analytics accordingly. Here's a breakdown of your code and potential issues:
Initial Consent Setting:
The first script block checks if localStorage has a consentMode item. If not, it sets the default consent for ad_storage and analytics_storage to denied.
This is a good practice to ensure user consent is handled before any data collection occurs.
Google Tag Manager (GTM) Integration:
Your second script integrates GTM into your site. This is necessary for managing various tags, including Google Analytics, in a centralized manner.
Ensure that GTM-MYSECRETKEY is replaced with your actual GTM container ID.
Consent Update Function:
The third script block defines a function setConsent to update the consent settings based on user interaction.
It correctly updates both the Google Analytics consent settings and the localStorage item.
Event Listeners for Consent Buttons:
You're adding event listeners to buttons with IDs btn_yes and btn_no, which presumably are part of your consent banner.
This setup seems appropriate for capturing user consent.
Potential Issues and Solutions:
Correct GTM Container ID:
Ensure you've replaced GTM-MYSECRETKEY with the correct GTM container ID.
Consistency in Consent Data:
Ensure that the data structure used in setConsent matches what Google Analytics expects. The consent object and consentMode in localStorage should be consistent.
Button IDs Existence:
Verify that the buttons with IDs btn_yes and btn_no exist in your HTML. If these IDs are incorrect or the elements are not present, the event listeners won't work.
Check Google Analytics Configuration in GTM:
In GTM, make sure that Google Analytics is set up to respect the consent settings. This usually involves configuring the Google Analytics tag to read the ad_storage and analytics_storage variables.
Debugging with GTM Preview Mode:
Use GTM's preview mode to debug the tag firing. This can help you see if the Google Analytics tag is firing as expected when consent is granted or denied.
Ensure GTM is Loaded Correctly:
Make sure that the GTM script is loading correctly on your website. Sometimes, issues like ad blockers can prevent GTM from loading, affecting tag firing.
Check for Console Errors:
Look for any errors in the browser console that might indicate issues with the script execution.
If after checking these aspects the issue still persists, you may need to further investigate the specific configuration of your Google Analytics and GTM setup, or consider getting in touch with a professional who can assist with debugging the issue.