Pat*_*ogt 3 javascript php cookies wordpress plugins
关于 GDPR(德语 DSGVO),我想在我的 WordPress 网站上使用 dFactory 的插件 Cookie 声明。如果您拒绝使用 cookie,则可能不再存储非功能性 cookie,例如 Google Analytics。
你们谁能告诉我要写哪些代码?不幸的是,我在互联网上找不到任何说明。也许有人已经实施了类似的事情。
先感谢您。
在尝试了多种方法后,我找到了解决方案。我只需将来自 google Analytics 的 javascript 代码放在 phpif条件之间,如下所示:
<?php if (cn_cookies_accepted()) { ?>
<script type="text/javascript">
(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-#', 'auto');
ga('set', 'forceSSL', true);
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
<?php } ?>
Run Code Online (Sandbox Code Playgroud)
还有另一种使用插件Code Snippets的方法。
只需创建一个新代码片段并使用 Wordpresswp_head操作挂钩来调用一个函数,如果接受 cookie,则将在该函数中执行 Google 分析代码。
例子:
function do_google_analytics() {
if ( function_exists('cn_cookies_accepted') && cn_cookies_accepted() )
{
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-999"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-999');
</script>
<?php
}
}
add_action('wp_head', 'do_google_analytics');
Run Code Online (Sandbox Code Playgroud)