Skip to content

Instantly share code, notes, and snippets.

@hostz-frank
Created May 22, 2018 20:49
Show Gist options
  • Save hostz-frank/f514772e210548ba84bd74b17a2ee074 to your computer and use it in GitHub Desktop.
Save hostz-frank/f514772e210548ba84bd74b17a2ee074 to your computer and use it in GitHub Desktop.
Das Plugin "Cookie Notice" für WordPress ermöglicht Opt-In in Facebook-Pixel und Google Analytics ...
<?php
/**
* Cookie-abhängiger Tracking-Code: Google Analytics und Facebook-Pixel.
*
* Falls Autoptimze installiert ist, wird der JS-Code
* auch minimiert (empfohlen).
*
* ACHTUNG: FB-Pixel-ID "12345678901234" und Google-Analytics-ID "UA-xxxxxxxxxx"
* müssen unten im Code natürlich angepasst werden!!
*/
add_action( 'wp_footer', function() {
ob_start(); ?>
<script type="text/javascript" id="child-theme-tracking-code">
function readCookie(k){ return(document.cookie.match('(^|; )'+k+'=([^;]*)')||0)[2]; }
document.addEventListener( "DOMContentLoaded", function() {
if( readCookie( 'divi-child-cookie-notice-hidden' ) === 'true' ) {
console.log('Hiddener runs.');
var notice = document.getElementById( 'cookie-notice' );
if( typeof notice !== 'undefined' ) {
notice.style.display = 'none';
notice.parentNode.removeChild(notice);
}
}
});
function hideCookieNotice() {
document.cookie = 'divi-child-cookie-notice-hidden=true; max-age=' + 60*60*24*60 + '; path=/';
location.reload();
}
function allowTracking() {
document.cookie = 'cookie_notice_accepted=true; max-age=' + 60*60*24*60 + '; path=/';
location.reload();
}
function disallowTracking() {
document.cookie = 'cookie_notice_accepted=false; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
location.reload();
}
if( readCookie( 'cookie_notice_accepted') === 'true' ) {
<?php /**** Facebook Pixel Code ****/ ?>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaed=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '12345678901234');
fbq('track', 'PageView');
<?php /**** End Facebook Pixel Code ****/ ?>
<?php /**** Google Analytics ****/ ?>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxxxxx', 'auto');
ga('set', 'anonymizeIp', true);
ga('require', 'outboundLinkTracker');
ga('send', 'pageview');
<?php /**** Ende GA ****/ ?>
}
</script><?php
$to_minify = ob_get_clean();
if ( class_exists( 'JSMin' ) && apply_filters( 'autoptimize_js_do_minify' , true ) ) {
if ( @is_callable( array( "JSMin", "minify" ) ) ) {
$minified = trim( JSMin::minify( $to_minify ) );
} else {
$minified = '';
}
}
print empty( $minified ) ? $to_minify : $minified;
}, 1001 );
/**
* Shortcode [decide_tracking] zeigt in der Datenschutzerklärung den aktuellen Status
* des FB- und GA-Trackings an und ermöglicht, diesen per Klick zu ändern.
*
* => Abhängig vom Plugin "Cookie Notice"!
*/
function decide_tracking_shortcode( $content ) {
$out = '<div id="active-track-state"><noscript><p>Da <strong>in diesem Browser JavaScript deaktiviert</strong> ist, ';
$out .= 'findet auf unserer Website ohnehin kein Tracking durch Google Analytics oder das Facebook-Pixel statt.</p></noscript></div>';
$out .= "\n<script>\n";
$out .= "function myreadCookie(k){return(document.cookie.match('(^|; )'+k+'=([^;]*)')||0)[2];}\n";
$out .= "if(myreadCookie('cookie_notice_accepted')==='true'){var tracking=true}else{var tracking=false}\n";
$out .= "if(tracking){var state = 'aktiviert';var button=' (<a href=\"#\" onclick=\"disallowTracking()\">Deaktivieren</a>';}";
$out .= "else{var state='deaktiviert';var button=' (<a href=\"#\" onclick=\"allowTracking()\">Aktivieren</a>';}\n";
$out .= "var msg = 'Das Tracking durch Google Analytics sowie das Setzen des Facebook-Pixels ist in diesem Browser: <strong>' + state + '</strong>.';\n";
$out .= "if(myreadCookie('cookie_notice_accepted') === 'true' || myreadCookie('divi-child-cookie-notice-hidden') === 'true'){var banner=')';}else{";
$out .= "var banner=' / <a href=\"#\" onclick=\"hideCookieNotice()\">Cookie-Banner verbergen</a>)';}\n";
$out .= "msg = '<p>' + msg + button + banner + '</p>';\n";
$out .= "document.getElementById('active-track-state').innerHTML = msg;\n";
$out .= '</script>';
return $out;
}
add_shortcode( 'decide_tracking', 'decide_tracking_shortcode' );
@hostz-frank
Copy link
Author

Wahrscheinlich sollte das Ganze besser ein Plugin sein. Aber so wie oben in der functions.php eines Child-Themes funktioniert es jedenfalls und ist getestet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment