2021-01-25 01:20:51 +03:00
|
|
|
/*
|
2021-01-23 10:07:18 +03:00
|
|
|
* Count page views form GA or local cache file.
|
2019-09-30 15:38:41 +03:00
|
|
|
*
|
2021-01-23 10:07:18 +03:00
|
|
|
* Dependencies:
|
2019-09-30 15:38:41 +03:00
|
|
|
* - jQuery
|
2020-04-01 15:33:49 +03:00
|
|
|
* - countUp.js <https://github.com/inorganik/countUp.js>
|
2019-09-30 15:38:41 +03:00
|
|
|
*/
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
const getInitStatus = (function () {
|
|
|
|
let hasInit = false;
|
2020-08-19 07:26:45 +03:00
|
|
|
return () => {
|
2020-04-01 15:33:49 +03:00
|
|
|
let ret = hasInit;
|
|
|
|
if (!hasInit) {
|
2019-12-18 22:07:02 +03:00
|
|
|
hasInit = true;
|
|
|
|
}
|
2020-04-01 15:33:49 +03:00
|
|
|
return ret;
|
2020-08-19 07:26:45 +03:00
|
|
|
};
|
|
|
|
}());
|
2019-12-18 22:07:02 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
const PvOpts = (function () {
|
2021-04-10 14:50:55 +03:00
|
|
|
function getContent(selector) {
|
|
|
|
return $(selector).attr("content");
|
|
|
|
}
|
|
|
|
|
2021-04-06 09:05:55 +03:00
|
|
|
function hasContent(selector) {
|
2021-04-10 14:50:55 +03:00
|
|
|
let content = getContent(selector);
|
2021-04-06 09:05:55 +03:00
|
|
|
return (typeof content !== "undefined" && content !== false);
|
|
|
|
}
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
return {
|
2021-04-10 14:50:55 +03:00
|
|
|
getProxyMeta() {
|
|
|
|
return getContent("meta[name=pv-proxy-endpoint]");
|
2021-01-23 10:07:18 +03:00
|
|
|
},
|
2021-04-10 14:50:55 +03:00
|
|
|
getLocalMeta() {
|
|
|
|
return getContent("meta[name=pv-cache-path]");
|
2021-04-05 20:04:09 +03:00
|
|
|
},
|
2021-04-10 14:50:55 +03:00
|
|
|
hasProxyMeta() {
|
2021-04-06 09:05:55 +03:00
|
|
|
return hasContent("meta[name=pv-proxy-endpoint]");
|
|
|
|
},
|
2021-04-10 14:50:55 +03:00
|
|
|
hasLocalMeta() {
|
2021-04-06 09:05:55 +03:00
|
|
|
return hasContent("meta[name=pv-cache-path]");
|
2021-01-23 10:07:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}());
|
2019-12-18 22:07:02 +03:00
|
|
|
|
2021-04-06 09:05:55 +03:00
|
|
|
const PvStorage = (function () {
|
2021-04-10 14:50:55 +03:00
|
|
|
const Keys = {
|
|
|
|
KEY_PV: "pv",
|
|
|
|
KEY_PV_SRC: "pv_src",
|
|
|
|
KEY_CREATION: "pv_created_date"
|
|
|
|
};
|
|
|
|
|
|
|
|
const Source = {
|
|
|
|
LOCAL: "same-origin",
|
|
|
|
PROXY: "cors"
|
|
|
|
};
|
2020-04-01 15:33:49 +03:00
|
|
|
|
|
|
|
function get(key) {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set(key, val) {
|
|
|
|
localStorage.setItem(key, val);
|
|
|
|
}
|
|
|
|
|
2021-04-10 14:50:55 +03:00
|
|
|
function saveCache(pv, src) {
|
|
|
|
set(Keys.KEY_PV, pv);
|
|
|
|
set(Keys.KEY_PV_SRC, src);
|
|
|
|
set(Keys.KEY_CREATION, new Date().toJSON());
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:33:49 +03:00
|
|
|
return {
|
2021-04-10 14:50:55 +03:00
|
|
|
keysCount() {
|
|
|
|
return Object.keys(Keys).length;
|
|
|
|
},
|
2021-04-06 09:05:55 +03:00
|
|
|
hasCache() {
|
2021-04-10 14:50:55 +03:00
|
|
|
return (localStorage.getItem(Keys.KEY_PV) !== null);
|
2021-04-06 09:05:55 +03:00
|
|
|
},
|
|
|
|
getCache() {
|
2021-04-10 14:50:55 +03:00
|
|
|
return JSON.parse(localStorage.getItem(Keys.KEY_PV));
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2021-04-10 14:50:55 +03:00
|
|
|
saveLocalCache(pv) {
|
|
|
|
saveCache(pv, Source.LOCAL);
|
|
|
|
},
|
|
|
|
saveProxyCache(pv) {
|
|
|
|
saveCache(pv, Source.PROXY);
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
isExpired() {
|
2021-04-10 14:50:55 +03:00
|
|
|
let date = new Date(get(Keys.KEY_CREATION));
|
|
|
|
date.setHours(date.getHours() + 1); // per hour
|
2021-04-06 09:05:55 +03:00
|
|
|
return Date.now() >= date.getTime();
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2021-04-10 14:50:55 +03:00
|
|
|
isFromLocal() {
|
|
|
|
return get(Keys.KEY_PV_SRC) === Source.LOCAL;
|
|
|
|
},
|
|
|
|
isFromProxy() {
|
|
|
|
return get(Keys.KEY_PV_SRC) === Source.PROXY;
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
newerThan(pv) {
|
2021-04-10 14:50:55 +03:00
|
|
|
return PvStorage.getCache().totalsForAllResults["ga:pageviews"] > pv.totalsForAllResults["ga:pageviews"];
|
2020-06-23 16:02:46 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
inspectKeys() {
|
2021-04-10 14:50:55 +03:00
|
|
|
if (localStorage.length !== PvStorage.keysCount()) {
|
|
|
|
localStorage.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:01:23 +03:00
|
|
|
for(let i = 0; i < localStorage.length; i++){
|
|
|
|
const key = localStorage.key(i);
|
|
|
|
switch (key) {
|
2021-04-10 14:50:55 +03:00
|
|
|
case Keys.KEY_PV:
|
|
|
|
case Keys.KEY_PV_SRC:
|
|
|
|
case Keys.KEY_CREATION:
|
2021-04-07 10:01:23 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
localStorage.clear();
|
|
|
|
return;
|
|
|
|
}
|
2020-06-23 16:02:46 +03:00
|
|
|
}
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
|
|
|
};
|
2021-04-07 10:01:23 +03:00
|
|
|
}()); /* PvStorage */
|
2020-08-19 07:26:45 +03:00
|
|
|
|
|
|
|
function countUp(min, max, destId) {
|
|
|
|
if (min < max) {
|
2021-01-23 10:07:18 +03:00
|
|
|
let numAnim = new CountUp(destId, min, max);
|
2020-08-19 07:26:45 +03:00
|
|
|
if (!numAnim.error) {
|
|
|
|
numAnim.start();
|
|
|
|
} else {
|
|
|
|
console.error(numAnim.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function countPV(path, rows) {
|
2021-01-23 10:07:18 +03:00
|
|
|
let count = 0;
|
2020-08-19 07:26:45 +03:00
|
|
|
|
|
|
|
if (typeof rows !== "undefined" ) {
|
2021-01-23 10:07:18 +03:00
|
|
|
for (let i = 0; i < rows.length; ++i) {
|
|
|
|
const gaPath = rows[parseInt(i, 10)][0];
|
2020-08-19 07:26:45 +03:00
|
|
|
if (gaPath === path) { /* path format see: site.permalink */
|
2020-08-19 14:17:03 +03:00
|
|
|
count += parseInt(rows[parseInt(i, 10)][1], 10);
|
2020-08-19 07:26:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tacklePV(rows, path, elem, hasInit) {
|
2021-01-23 10:07:18 +03:00
|
|
|
let count = countPV(path, rows);
|
2020-08-19 07:26:45 +03:00
|
|
|
count = (count === 0 ? 1 : count);
|
|
|
|
|
|
|
|
if (!hasInit) {
|
|
|
|
elem.text(new Intl.NumberFormat().format(count));
|
|
|
|
} else {
|
2021-01-23 10:07:18 +03:00
|
|
|
const initCount = parseInt(elem.text().replace(/,/g, ""), 10);
|
2020-08-19 07:26:45 +03:00
|
|
|
if (count > initCount) {
|
|
|
|
countUp(initCount, count, elem.attr("id"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function displayPageviews(data) {
|
|
|
|
if (typeof data === "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
let hasInit = getInitStatus();
|
|
|
|
const rows = data.rows; /* could be undefined */
|
2020-08-19 07:26:45 +03:00
|
|
|
|
|
|
|
if ($("#post-list").length > 0) { /* the Home page */
|
|
|
|
$(".post-preview").each(function() {
|
2021-01-23 10:07:18 +03:00
|
|
|
const path = $(this).find("a").attr("href");
|
2020-08-19 07:26:45 +03:00
|
|
|
tacklePV(rows, path, $(this).find(".pageviews"), hasInit);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if ($(".post").length > 0) { /* the post */
|
2021-01-23 10:07:18 +03:00
|
|
|
const path = window.location.pathname;
|
2020-08-19 07:26:45 +03:00
|
|
|
tacklePV(rows, path, $("#pv"), hasInit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchProxyPageviews() {
|
2021-04-10 14:50:55 +03:00
|
|
|
if (PvOpts.hasProxyMeta()) {
|
2021-04-06 09:05:55 +03:00
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
2021-04-10 14:50:55 +03:00
|
|
|
url: PvOpts.getProxyMeta(),
|
2021-04-06 09:05:55 +03:00
|
|
|
dataType: "jsonp",
|
|
|
|
jsonpCallback: "displayPageviews",
|
2021-04-10 14:50:55 +03:00
|
|
|
success: (data) => {
|
|
|
|
PvStorage.saveProxyCache(JSON.stringify(data));
|
2021-04-06 09:05:55 +03:00
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
|
|
|
console.log("Failed to load pageviews from proxy server: " + errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-19 07:26:45 +03:00
|
|
|
}
|
2020-04-01 15:33:49 +03:00
|
|
|
|
2021-04-10 14:50:55 +03:00
|
|
|
function fetchLocalPageviews(hasCache = false) {
|
|
|
|
return fetch(PvOpts.getLocalMeta())
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => {
|
|
|
|
if (hasCache) {
|
2021-04-06 09:05:55 +03:00
|
|
|
// The cache from the proxy will sometimes be more recent than the local one
|
2021-04-10 14:50:55 +03:00
|
|
|
if (PvStorage.isFromProxy() && PvStorage.newerThan(data)) {
|
2021-04-06 09:05:55 +03:00
|
|
|
return;
|
2020-07-12 17:59:05 +03:00
|
|
|
}
|
2021-04-10 14:50:55 +03:00
|
|
|
}
|
|
|
|
displayPageviews(data);
|
|
|
|
PvStorage.saveLocalCache(JSON.stringify(data));
|
|
|
|
});
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
|
|
|
|
2019-09-30 15:38:41 +03:00
|
|
|
$(function() {
|
2021-04-05 20:04:09 +03:00
|
|
|
if ($(".pageviews").length <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-01 15:33:49 +03:00
|
|
|
|
2021-04-06 09:05:55 +03:00
|
|
|
PvStorage.inspectKeys();
|
2020-04-01 15:33:49 +03:00
|
|
|
|
2021-04-06 09:05:55 +03:00
|
|
|
if (PvStorage.hasCache()) {
|
|
|
|
displayPageviews(PvStorage.getCache());
|
2021-04-10 14:50:55 +03:00
|
|
|
|
|
|
|
if (PvStorage.isExpired()) {
|
|
|
|
if (PvOpts.hasLocalMeta()) {
|
|
|
|
fetchLocalPageviews(true).then(fetchProxyPageviews);
|
|
|
|
} else {
|
|
|
|
fetchProxyPageviews();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (PvStorage.isFromLocal()) {
|
|
|
|
fetchProxyPageviews();
|
|
|
|
}
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2021-04-10 14:50:55 +03:00
|
|
|
} else { // no cached
|
|
|
|
|
|
|
|
if (PvOpts.hasLocalMeta()) {
|
|
|
|
fetchLocalPageviews().then(fetchProxyPageviews);
|
|
|
|
} else {
|
|
|
|
fetchProxyPageviews();
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 09:05:55 +03:00
|
|
|
|
2020-03-19 13:56:54 +03:00
|
|
|
});
|