Skip to content

Commit

Permalink
Ignore visitor count from localhost (#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang authored Nov 22, 2019
1 parent 1417248 commit 33d3603
Show file tree
Hide file tree
Showing 11 changed files with 7 additions and 43 deletions.
7 changes: 0 additions & 7 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,6 @@ add_this_id:
# Google Analytics
google_analytics:
tracking_id: # <app_id>
localhost_ignored: true
# By default, NexT will load an external gtag.js script on your site.
# If you only need the pageview feature, set the following option to true to get a better performance.
only_pageview: false
Expand All @@ -705,12 +704,6 @@ growingio_analytics: # <project_id>
# CNZZ count
cnzz_siteid:

# Tencent analytics
tencent_analytics: # <app_id>

# Tencent MTA
tencent_mta: # <app_id>

# Show number of visitors to each article.
# You can visit https://leancloud.cn to get AppID and AppKey.
leancloud_visitors:
Expand Down
1 change: 1 addition & 0 deletions layout/_partials/head/head.swig
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {
hostname: new URL('{{ config.url }}').hostname,
root: '{{ config.root }}',
scheme: '{{ theme.scheme }}',
version: '{{ next_version }}',
Expand Down
6 changes: 2 additions & 4 deletions layout/_third-party/analytics/google-analytics.swig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
{%- if not theme.google_analytics.only_pageview %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ theme.google_analytics.tracking_id }}"></script>
<script{{ pjax }}>
var host = window.location.hostname;
if (host !== "localhost" || !{{ theme.google_analytics.localhost_ignored }}) {
if (CONFIG.hostname === location.hostname) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
Expand All @@ -14,8 +13,7 @@
{%- if theme.google_analytics.only_pageview %}
<script>
function sendPageView() {
var host = window.location.hostname;
if (host == "localhost" && {{ theme.google_analytics.localhost_ignored }}) return;
if (CONFIG.hostname !== location.hostname) return;
var uid = localStorage.getItem('uid') || (Math.random() + '.' + Math.random());
localStorage.setItem('uid', uid);
navigator.sendBeacon('https://www.google-analytics.com/collect', new URLSearchParams({
Expand Down
3 changes: 1 addition & 2 deletions layout/_third-party/statistics/firestore.swig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

function getCount(doc, increaseCount) {
// IncreaseCount will be false when not in article page

return doc.get().then(d => {
var count = 0;
if (!d.exists) { // Has no data, initialize count
Expand Down Expand Up @@ -48,7 +47,7 @@
if (CONFIG.page.isPost) { // Is article page
var title = document.querySelector('.post-title').innerText.trim();
var doc = articles.doc(title);
var increaseCount = true;
var increaseCount = CONFIG.hostname === location.hostname;
if (localStorage.getItem(title)) {
increaseCount = false;
} else {
Expand Down
2 changes: 0 additions & 2 deletions layout/_third-party/statistics/index.swig
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
{% include 'cnzz-analytics.swig' %}
{% include 'firestore.swig' %}
{% include 'lean-analytics.swig' %}
{% include 'tencent-analytics.swig' %}
{% include 'tencent-mta.swig' %}
3 changes: 1 addition & 2 deletions layout/_third-party/statistics/lean-analytics.swig
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@
});
};
if (CONFIG.page.isPost) {
const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
if (localhost.test(document.URL)) return;
if (CONFIG.hostname !== location.hostname) return;
addCount(Counter);
} else if (document.querySelectorAll('.post-title-link').length >= 1) {
showTime(Counter);
Expand Down
10 changes: 0 additions & 10 deletions layout/_third-party/statistics/tencent-analytics.swig

This file was deleted.

13 changes: 0 additions & 13 deletions layout/_third-party/statistics/tencent-mta.swig

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ hexo.on('generateAfter', () => {
const path = require('path');
const { version } = require(path.normalize('../../package.json'));
https.get('https://api.github.com/repos/theme-next/hexo-theme-next/releases/latest', {
headers : {
headers: {
'User-Agent': 'Theme NexT Client'
}
}, res => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/helpers/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ hexo.extend.helper.register('post_nav', function(post) {
<div class="post-nav">
<div class="post-nav-item">${left}</div>
<div class="post-nav-item">${right}</div>
</div>`
</div>`;
});

hexo.extend.helper.register('gitalk_md5', function(path) {
Expand Down
1 change: 0 additions & 1 deletion source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ NexT.utils = {
hasMobileUA: function() {
var ua = navigator.userAgent;
var pa = /iPad|iPhone|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP|IEMobile|Symbian/g;

return pa.test(ua);
},

Expand Down

0 comments on commit 33d3603

Please sign in to comment.