cookie_consent icon indicating copy to clipboard operation
cookie_consent copied to clipboard

Analytics / Matomo Opt-In

Open webregie opened this issue 6 years ago • 2 comments

Wie realisiert man mit dem Plugin denn nun einen Opt-In für Analytics und Matomo?

Das Problem ist, wenn ich in das Feld etwas eintrage, dann kann man keine Skripte hinzufügen, weil es an der falschen Stelle eingreift. Irgendwer muss doch mit diesem Plugin das schon sinnvoll eingebaut haben?

webregie avatar Oct 23 '19 10:10 webregie

hab das mal so umgesetzt (wobei die Implementierung von opt-out/opt-in im Addon m.M.n. falsch ist. Bei opt-in müsste es eigentlich die Buttons "allow" und "deny" geben. Es kommt aber "allow" und "dismiss". "dismiss" erzeugt aber didConsent == true. daher hab ich opt-out genommen.

<script>
  window.addEventListener("load", function() {
    window.cookieconsent.initialise({
    "theme": "block",
    "position": "bottom",
    "content": {
        "message": "We are using cookies to ensure that we give you the best experience on our website. If you continue we assume that you consent to receive cookies. <a href='/privacy-policy'>Privacy Policy</a>",
        "dismiss": "OK",
        "deny": "No Cookies",
        "allow": "",
        "link": "",
        "href": "\/privacy-policy\/"
    },
    "type": "opt-in",
    "elements": {
        "messagelink": "<span id='cookieconsent:desc' class='cc-message'>{{message}} <a aria-label='learn more about cookies' tabindex='0' class='cc-link' href='{{href}}''>{{link}}<\/a><\/span>"
    },
    "palette": {
        "popup": {
            "background": "black",
            "text": "white"
        },
        "button": {
            "background": "white",
            "text": "black"
        }
    },
    onInitialise: function (status) {
      var type = this.options.type;
      var didConsent = this.hasConsented();
      if (type == 'opt-out' && didConsent && status == "dismiss") {
          // enable cookies
          matomoTracking();
      }
      if (type == 'opt-in' && !didConsent) {
          // disable cookies
      }
    },
    onStatusChange: function(status, chosenBefore) {
      var type = this.options.type;
      var didConsent = this.hasConsented();
      if (type == 'opt-out' && didConsent && status == "dismiss") {
          // enable cookies
          matomoTracking();
      }
      if (type == 'opt-in' && !didConsent) {
          // disable cookies
      }
    }
  });
});   
</script>

und die Funktion matotoTracking:

<script>
  var _paq = _paq || [];
    
   function matomoTracking() {
  	_paq.push(['trackPageView']);
  	_paq.push(['enableLinkTracking']);
     
    var u="//yourmatomo.url/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js';					    	s.parentNode.insertBefore(g,s);
   }  
</script>

fietstouring avatar Oct 23 '19 11:10 fietstouring

ich würde das cookie consent JS durch das aktuelle ersetzen https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js

fietstouring avatar Oct 24 '19 07:10 fietstouring