Google Analytics(分析)选择加入/退出不起作用

时间:2018-07-09 15:28:35

标签: javascript cookies google-analytics

在关注了几个主题之后,我仍在努力使我的退出/加入功能正常工作。

据我了解,如果用户选择退出,则该代码应在G分析代码中更改。但是什么都没有改变。

我实际上尝试了10种不同的方法,但是没有运气。 JavaScript确实不是我的强项。

有人在这里看到我在做什么错吗?

var gaProperty = 'UA-111111-1';
    var disableStr = 'ga-disable-' + gaProperty;
    if (document.cookie.indexOf(disableStr + '=true') > -1) {
        window[disableStr] = true;
    }
    // Opt-out function
    function gaOptout() {
        document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
        window[disableStr] = true;
        location.reload(true);
    }
    // Opt-in function
    function gaOptin() {
        document.cookie = disableStr + '=false; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
        window[disableStr] = false;
        location.reload(true);
    }
    
    
    
     (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-111111-1', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('set', 'forceSSL', true);
    ga('send', 'pageview');
<div>We use cookies to give you an incredible user experience.&nbsp;&nbsp;<a href="javascript:gaOptout()">Opt-out</a> <a href="javascript:gaOptin()">Accept Cookies</a></div>

0 个答案:

没有答案
相关问题