【问题标题】:Wordpress dFactory Cookie Notice PluginWordpress dFactory Cookie 通知插件
【发布时间】:2018-05-23 04:41:14
【问题描述】:

关于 GDPR,在德语 DSGVO 中,我想将 dFactory 的插件 Cookie 通知用于我的 Wordpress 网站。如果您拒绝使用 cookie,则可能不再存储非功能性 cookie,例如 Google Analytics。

Documentation of Cookie Notice by dFactory

谁能告诉我要写哪个代码?不幸的是,我在互联网上找不到任何说明。也许有人已经实现了类似的东西。

提前谢谢你。

【问题讨论】:

  • 您从哪里了解到这种可能性?没有第三方 (GA) Cookie 的帮助和对该域的客户端请求,您无法操纵它们。
  • @janh 我在他发布的链接中读到了它。插件会延迟 GA javascript 的加载,直到客户端接受 cookie,如果他们拒绝它们,则永远不会加载 javascript。

标签: javascript php wordpress cookies plugins


【解决方案1】:

在尝试了多种方法后,我找到了解决方案。我只需要将来自谷歌分析的 javascript 代码放在 php if 条件之间,如下所示:

<?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 } ?>

【讨论】:

  • 我将代码放在cookie通知插件的设置中。有一个输入框可以放。或者你可以像@Tarod 建议的那样做。
【解决方案2】:

还有另一种使用插件Code Snippets的方法。

只需创建一个新的 sn-p 并使用 Wordpress wp_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');

【讨论】:

    【解决方案3】:

    上面的答案对我不起作用,这个答案对我有用。

    我将它放在“Cookie Notice”插件的“脚本阻止”编辑框中(确保将 UA-xxxxxxx 替换为您自己的 Google Analytics 帐户中的 ID)

    <script>
     (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-xxxxxx', 'auto');
    ga('set', 'forceSSL', true);
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
    </script>
    

    【讨论】:

    • 这个对我有用,你也可以把它和!current_user_can('administrator')结合起来,在GA中禁止管理轨道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    相关资源
    最近更新 更多