aboutsummaryrefslogtreecommitdiffstats
path: root/assets/js/pages/hitcounter.js
blob: 7cbced639e32b5c6649106f84901ca2908a5046f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function countUniqueVisitors() {
      // Per-page local counter (host + path), matching the site footer logic.
      function pageKey() {
        var path = (location.pathname || '/').replace(/\/+$/, '') || '/';
        path = path.replace(/\/index(?:_[a-z]{2})?\.php$/i, '') || '/';
        path = path.replace(/_(jp|zh)\.php$/i, '.php');
        return location.hostname + path;
      }
      var page = pageKey();
      var VISITOR_KEY = 'uniqueVisitorCount_v2:' + page;
      var SESSION_KEY = 'hasVisited_v2:' + page;
      var visitorCount = localStorage.getItem(VISITOR_KEY);
      if (!visitorCount) visitorCount = 0;
      else visitorCount = parseInt(visitorCount, 10) || 0;

      if (!sessionStorage.getItem(SESSION_KEY)) {
        visitorCount++;
        localStorage.setItem(VISITOR_KEY, String(visitorCount));
        sessionStorage.setItem(SESSION_KEY, 'true');
      }

      var el = document.getElementById('visitor-counter');
      if (el) el.innerText = String(visitorCount);
    }

    countUniqueVisitors();