|
| 1 | +# Consent Manager – Dev Kurzhilfe (Schnellstart) |
| 2 | + |
| 3 | +Kurz und praktisch: Einbindung, Consent-Abfrage und typische Snippets. |
| 4 | + |
| 5 | +## 1) Einbindung wählen |
| 6 | + |
| 7 | +### Option A: Auto-Inject (empfohlen) |
| 8 | +1. Backend öffnen: `Consent Manager -> Domains -> Domain bearbeiten` |
| 9 | +2. `Automatische Frontend-Einbindung` auf **Aktiviert** setzen |
| 10 | +3. Speichern |
| 11 | + |
| 12 | +Ergebnis: Der Consent Manager wird automatisch vor `</head>` eingebunden. |
| 13 | + |
| 14 | +### Option B: Manuell im Template (komplett) |
| 15 | + |
| 16 | +```php |
| 17 | +<?php |
| 18 | +use FriendsOfRedaxo\ConsentManager\Frontend; |
| 19 | + |
| 20 | +echo Frontend::getFragment(0, 0, 'ConsentManager/box_cssjs.php'); |
| 21 | +``` |
| 22 | + |
| 23 | +### Option C: Manuell, Komponenten getrennt |
| 24 | + |
| 25 | +```php |
| 26 | +<?php |
| 27 | +use FriendsOfRedaxo\ConsentManager\Frontend; |
| 28 | +?> |
| 29 | +<style><?= Frontend::getCSS() ?></style> |
| 30 | +<script<?= Frontend::getNonceAttribute() ?>> |
| 31 | +<?= Frontend::getJS() ?> |
| 32 | +</script> |
| 33 | +<?= Frontend::getBox() ?> |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## 2) Consent in PHP abfragen |
| 39 | + |
| 40 | +```php |
| 41 | +<?php |
| 42 | +use FriendsOfRedaxo\ConsentManager\Utility; |
| 43 | + |
| 44 | +if (Utility::has_consent('google-analytics')) { |
| 45 | + // Script/Markup nur bei erteiltem Consent ausgeben |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## 3) Consent in JavaScript abfragen |
| 52 | + |
| 53 | +Wichtig: Eigene Skripte sollten warten, bis der Consent Manager initialisiert ist. |
| 54 | + |
| 55 | +```javascript |
| 56 | +document.addEventListener('consent_manager-ready', function (e) { |
| 57 | + if (!e.detail.initialized) { |
| 58 | + console.warn('Consent Manager nicht bereit:', e.detail.reason); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (typeof consent_manager_hasconsent === 'function' && consent_manager_hasconsent('google-analytics')) { |
| 63 | + console.log('GA darf geladen werden'); |
| 64 | + } |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +Direkte Abfrage (wenn bereits sicher initialisiert): |
| 69 | + |
| 70 | +```javascript |
| 71 | +if (typeof consent_manager_hasconsent === 'function' && consent_manager_hasconsent('google-analytics')) { |
| 72 | + // Consent vorhanden |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## 4) Auf Consent-Änderung reagieren |
| 79 | + |
| 80 | +```javascript |
| 81 | +document.addEventListener('consent_manager-saved', function (e) { |
| 82 | + var consents = JSON.parse(e.detail); |
| 83 | + // z. B. Tracking dynamisch starten/stoppen |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## 5) Einstellungen-Dialog per Link öffnen |
| 90 | + |
| 91 | +Standard (ohne Reload): |
| 92 | + |
| 93 | +```html |
| 94 | +<a href="#" data-consent-action="settings">Cookie-Einstellungen</a> |
| 95 | +``` |
| 96 | + |
| 97 | +Variante mit Reload nach Speichern: |
| 98 | + |
| 99 | +```html |
| 100 | +<a href="#" data-consent-action="settings,reload">Cookie-Einstellungen</a> |
| 101 | +``` |
| 102 | + |
| 103 | +Tipp: `reload` ist sinnvoll, wenn externe Skripte keinen sauberen Live-Reinit unterstützen und erst nach einem Seiten-Reload korrekt starten/stoppen. |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +## 6) Häufige Stolpersteine |
| 108 | + |
| 109 | +- `consent_manager_hasconsent is not defined`: Consent Manager ist noch nicht geladen oder gar nicht eingebunden. |
| 110 | +- `TypeError: Load failed` beim Speichern: meist URL-/Host-/HTTPS-Mismatch im Setup; aktuelle Version nutzt same-origin Logging. |
| 111 | +- Falsche Quotes in JS vermeiden: normale `'` oder `"` verwenden, keine typografischen Anführungszeichen. |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## 7) Spezialfälle (Tipps) |
| 116 | + |
| 117 | +### A) Mehrere Services gleichzeitig prüfen |
| 118 | + |
| 119 | +```javascript |
| 120 | +function hasAllConsents(serviceKeys) { |
| 121 | + if (typeof consent_manager_hasconsent !== 'function') return false; |
| 122 | + return serviceKeys.every(function (key) { |
| 123 | + return consent_manager_hasconsent(key); |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +document.addEventListener('consent_manager-ready', function () { |
| 128 | + if (hasAllConsents(['google-analytics', 'google-tag-manager'])) { |
| 129 | + // Nur wenn beide erlaubt sind |
| 130 | + } |
| 131 | +}); |
| 132 | +``` |
| 133 | + |
| 134 | +### B) Script erst nach Consent dynamisch laden |
| 135 | + |
| 136 | +```javascript |
| 137 | +function loadScript(src) { |
| 138 | + var script = document.createElement('script'); |
| 139 | + script.src = src; |
| 140 | + script.async = true; |
| 141 | + document.head.appendChild(script); |
| 142 | +} |
| 143 | + |
| 144 | +document.addEventListener('consent_manager-ready', function () { |
| 145 | + if (typeof consent_manager_hasconsent === 'function' && consent_manager_hasconsent('google-analytics')) { |
| 146 | + loadScript('https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX'); |
| 147 | + } |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +### C) Reaktion auf spätere Änderungen (Opt-in/Opt-out) |
| 152 | + |
| 153 | +```javascript |
| 154 | +document.addEventListener('consent_manager-saved', function () { |
| 155 | + if (typeof consent_manager_hasconsent !== 'function') return; |
| 156 | + |
| 157 | + if (consent_manager_hasconsent('google-analytics')) { |
| 158 | + // Tracking aktivieren |
| 159 | + } else { |
| 160 | + // Tracking deaktivieren / keine neuen Events senden |
| 161 | + } |
| 162 | +}); |
| 163 | +``` |
| 164 | + |
| 165 | +### D) Fallback, wenn eigenes Script sehr früh läuft |
| 166 | + |
| 167 | +```javascript |
| 168 | +function onConsentManagerReady(callback) { |
| 169 | + document.addEventListener('consent_manager-ready', function (e) { |
| 170 | + if (e.detail.initialized) callback(); |
| 171 | + }); |
| 172 | + |
| 173 | + document.addEventListener('DOMContentLoaded', function () { |
| 174 | + if (typeof consent_manager_hasconsent === 'function') { |
| 175 | + callback(); |
| 176 | + } |
| 177 | + }); |
| 178 | +} |
| 179 | + |
| 180 | +onConsentManagerReady(function () { |
| 181 | + // hier sicher mit consent_manager_hasconsent arbeiten |
| 182 | +}); |
| 183 | +``` |
0 commit comments