Custom OptOut not working

Hello everybody,

I believe my custom optout is not working.
I use Wordpress 5.5.3 wit the plugin ‘WP-Matomo 1.0.23’. The external Matomo instance is on 3.14.1.

I have followed this tutorial https://developer.matomo.org/guides/tracking-javascript-guide#optional-creating-a-custom-opt-out-form

My tracking code now looks like this:

<script type="text/javascript">
  var _paq = window._paq = window._paq || [];
  _paq.push(["disableCookies"]);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="https://MATOMOURL/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', 'SITEID']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();

  document.addEventListener("DOMContentLoaded", function(event) {
    function setOptOutText(element) {
      _paq.push([function() {
      element.checked = !this.isUserOptedOut();
      if (document.querySelector('#matomo-opt-out-label') != null){
         document.querySelector('#matomo-opt-out-label').innerHTML = this.isUserOptedOut() ? 'You are currently opted out. Click here to opt in.' : 'You are currently opted in. Click here to opt out.';
      }
    }]);
   }

  var optOut = document.getElementById("optout"); 
  optOut.addEventListener("click", function() {
    if (this.checked) {
      _paq.push(['forgetUserOptOut']);
    } else {
      _paq.push(['optUserOut']);
    }
    setOptOutText(optOut);
  });
  setOptOutText(optOut);
});
</script>

The problem is now the following:
When I load the page with the custom optout-form the checkbox is always active.
If I deactivate it and reload the page, the checkbox is active again.
The ‘piwik_ignore’ cookie is never set.
For testing purposes I also implemented the iFrame variant.
If I deactivate the checkbox in the custom form and reload the page both checkboxes are active (iFrame and custom form)

Furthermore I noticed that sometimes you have to click twice to activate the checkbox.

Did anyone else observe this behaviour or do I get the whole thing wrong?

Hi,

Would it be possible to send a direct message with the link to the opt-out form?