Skip to content

Commit

Permalink
✨ 遵循"请勿追踪"功能 fluid-dev#710
Browse files Browse the repository at this point in the history
  • Loading branch information
zkqiang committed Apr 27, 2022
1 parent bbb27d8 commit 23644ef
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
5 changes: 5 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ custom_css:
web_analytics: # 网页访问统计
enable: false

# 遵循访客浏览器"请勿追踪"的设置,如果开启则不统计其访问
# Follow the "Do Not Track" setting of the visitor's browser
# See: https://www.w3.org/TR/tracking-dnt/
follow_dnt: true

# 百度统计的 Key,值需要获取下方链接中 `hm.js?` 后边的字符串
# Baidu analytics, get the string behind `hm.js?`
# See: https://tongji.baidu.com/sc-web/10000033910/home/site/getjs?siteId=13751376
Expand Down
80 changes: 49 additions & 31 deletions layout/_partials/plugins/analytics.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,82 @@
<% if(theme.web_analytics.baidu) { %>
<!-- Baidu Analytics -->
<script defer>
var _hmt = _hmt || [];
(function () {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?<%= theme.web_analytics.baidu %>";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
if (!Fluid.ctx.dnt) {
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?<%= theme.web_analytics.baidu %>";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
}
</script>
<% } %>
<% if (theme.web_analytics.google){ %>
<!-- Google Analytics -->
<script defer>
window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments) };
ga.l = +new Date;
ga('create', '<%= theme.web_analytics.google %>', 'auto');
ga('send', 'pageview');
if (!Fluid.ctx.dnt) {
Fluid.utils.createScript('https://www.google-analytics.com/analytics.js', function() {
window.ga = window.ga || function() { (ga.q = ga.q || []).push(arguments) };
ga.l = +new Date;
ga('create', '<%= theme.web_analytics.google %>', 'auto');
ga('send', 'pageview');
});
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<% } %>
<% if (theme.web_analytics.gtag){ %>
<!-- Google gtag.js -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<%- theme.web_analytics.gtag %>"></script>
<script defer>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<%- theme.web_analytics.gtag %>');
if (!Fluid.ctx.dnt) {
Fluid.utils.createScript('https://www.googletagmanager.com/gtag/js?id=<%= theme.web_analytics.gtag %>', function() {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<%- theme.web_analytics.gtag %>');
});
}
</script>
<% } %>
<% if(theme.web_analytics.tencent && theme.web_analytics.tencent.sid && theme.web_analytics.tencent.cid) { %>
<!-- Tencent Analytics -->
<script defer>
var _mtac = {};
(function () {
var mta = document.createElement("script");
mta.src = "//pingjs.qq.com/h5/stats.js?v2.0.4";
mta.setAttribute("name", "MTAH5");
mta.setAttribute("sid", "<%= theme.web_analytics.tencent.sid %>");
<% if(theme.web_analytics.tencent.cid) { %>
mta.setAttribute("cid", "<%= theme.web_analytics.tencent.cid %>");
<% } %>
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(mta, s);
})();
if (!Fluid.ctx.dnt) {
var _mtac = {};
(function() {
var mta = document.createElement("script");
mta.src = "//pingjs.qq.com/h5/stats.js?v2.0.4";
mta.setAttribute("name", "MTAH5");
mta.setAttribute("sid", "<%= theme.web_analytics.tencent.sid %>");
<% if(theme.web_analytics.tencent.cid) { %>
mta.setAttribute("cid", "<%= theme.web_analytics.tencent.cid %>");
<% } %>
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(mta, s);
})();
}
</script>
<% } %>
<% if(theme.web_analytics.woyaola) { %>
<!-- 51.la Analytics -->
<script defer type="text/javascript" src="//js.users.51.la/<%= theme.web_analytics.woyaola %>.js"></script>
<script defer>
if (!Fluid.ctx.dnt) {
Fluid.utils.createScript('//js.users.51.la/<%= theme.web_analytics.woyaola %>.js');
}
</script>
<% } %>
<% if(theme.web_analytics.cnzz) { %>
<!-- cnzz Analytics -->
<script defer src="//s4.cnzz.com/z_stat.php?id=<%- theme.web_analytics.cnzz %>&show=pic" type="text/javascript"></script>
<script defer>
if (!Fluid.ctx.dnt) {
Fluid.utils.createScript('//s4.cnzz.com/z_stat.php?id=<%= theme.web_analytics.cnzz %>&show=pic');
}
</script>
<% } %>
<% if(theme.web_analytics.leancloud && theme.web_analytics.leancloud.app_id && theme.web_analytics.leancloud.app_key) { %>
Expand Down
6 changes: 6 additions & 0 deletions scripts/helpers/export-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ hexo.extend.helper.register('export_config', function() {
};
return `<script id="fluid-configs">
var Fluid = window.Fluid || {};
Fluid.ctx = Object.assign({}, Fluid.ctx)
var CONFIG = ${JSON.stringify(exportConfig)};
if (CONFIG.web_analytics.follow_dnt) {
var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
}
</script>`;
});
2 changes: 1 addition & 1 deletion source/js/leancloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
}

function addCount(Counter) {
var enableIncr = CONFIG.web_analytics.enable && validHost();
var enableIncr = CONFIG.web_analytics.enable && !Fluid.ctx.dnt && validHost();
var getterArr = [];
var incrArr = [];

Expand Down

0 comments on commit 23644ef

Please sign in to comment.