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 () {
|
|
|
|
return {
|
|
|
|
isEnabled() {
|
|
|
|
return "true" === $("meta[name=pv-cache-enabled]").attr("content");
|
|
|
|
},
|
|
|
|
getProxyEndpoint() {
|
|
|
|
return $("meta[name=pv-proxy-endpoint]").attr("content");
|
|
|
|
},
|
|
|
|
getLocalData() {
|
|
|
|
return $("meta[name=pv-cache-data]").attr("content");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}());
|
2019-12-18 22:07:02 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
const PvCache = (function () {
|
2020-04-01 15:33:49 +03:00
|
|
|
const KEY_PV = "pv";
|
2020-06-23 16:02:46 +03:00
|
|
|
const KEY_CREATION = "pv_created_date";
|
2020-07-12 17:59:05 +03:00
|
|
|
const KEY_PV_SRC = "pv_source";
|
2020-04-01 15:33:49 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
const Source = {
|
2020-04-01 15:33:49 +03:00
|
|
|
ORIGIN: "origin",
|
|
|
|
PROXY: "proxy"
|
|
|
|
};
|
|
|
|
|
|
|
|
function get(key) {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set(key, val) {
|
|
|
|
localStorage.setItem(key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-08-19 07:26:45 +03:00
|
|
|
getData() {
|
2021-01-23 10:07:18 +03:00
|
|
|
// get data from browser cache
|
|
|
|
return JSON.parse(localStorage.getItem(KEY_PV));
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
saveOriginCache(pv) {
|
2020-04-01 15:33:49 +03:00
|
|
|
set(KEY_PV, pv);
|
2021-01-23 10:07:18 +03:00
|
|
|
set(KEY_PV_SRC, Source.ORIGIN);
|
|
|
|
set(KEY_CREATION, new Date().toJSON());
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
saveProxyCache(pv) {
|
2020-04-01 15:33:49 +03:00
|
|
|
set(KEY_PV, pv);
|
2021-01-23 10:07:18 +03:00
|
|
|
set(KEY_PV_SRC, Source.PROXY);
|
|
|
|
set(KEY_CREATION, new Date().toJSON());
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
isFromOrigin() {
|
|
|
|
return get(KEY_PV_SRC) === Source.ORIGIN;
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
isFromProxy() {
|
|
|
|
return get(KEY_PV_SRC) === Source.PROXY;
|
2020-04-01 15:33:49 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
isExpired() {
|
2021-01-23 10:07:18 +03:00
|
|
|
if (PvCache.isFromOrigin()) {
|
2020-04-01 15:33:49 +03:00
|
|
|
let date = new Date(get(KEY_CREATION));
|
2020-07-12 17:59:05 +03:00
|
|
|
date.setDate(date.getDate() + 1); /* update origin records every day */
|
2020-04-01 15:33:49 +03:00
|
|
|
return Date.now() >= date.getTime();
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
} else if (PvCache.isFromProxy()) {
|
|
|
|
let date = new Date(get(KEY_CREATION));
|
2020-07-12 17:59:05 +03:00
|
|
|
date.setHours(date.getHours() + 1); /* update proxy records per hour */
|
2020-04-01 15:33:49 +03:00
|
|
|
return Date.now() >= date.getTime();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2021-01-23 10:07:18 +03:00
|
|
|
getAllPageviews() {
|
2020-04-01 15:33:49 +03:00
|
|
|
return PvCache.getData().totalsForAllResults["ga:pageviews"];
|
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
newerThan(pv) {
|
2021-01-23 10:07:18 +03:00
|
|
|
return PvCache.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
|
2020-06-23 16:02:46 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
inspectKeys() {
|
|
|
|
if (localStorage.getItem(KEY_PV) === null
|
|
|
|
|| localStorage.getItem(KEY_PV_SRC) === null
|
|
|
|
|| localStorage.getItem(KEY_CREATION) === null) {
|
2020-06-23 16:02:46 +03:00
|
|
|
localStorage.clear();
|
|
|
|
}
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
}()); /* PvCache */
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
|
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() {
|
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
2021-01-23 10:07:18 +03:00
|
|
|
url: PvOpts.getProxyEndpoint(),
|
2020-08-19 07:26:45 +03:00
|
|
|
dataType: "jsonp",
|
|
|
|
jsonpCallback: "displayPageviews",
|
|
|
|
success: (data, textStatus, jqXHR) => {
|
|
|
|
PvCache.saveProxyCache(JSON.stringify(data));
|
|
|
|
},
|
|
|
|
error: (jqXHR, textStatus, errorThrown) => {
|
|
|
|
console.log("Failed to load pageviews from proxy server: " + errorThrown);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-01 15:33:49 +03:00
|
|
|
|
|
|
|
|
2020-07-12 17:59:05 +03:00
|
|
|
function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
|
2021-01-23 10:07:18 +03:00
|
|
|
if (PvOpts.isEnabled() && fetchOrigin) {
|
|
|
|
fetch(PvOpts.getLocalData())
|
2020-08-19 07:26:45 +03:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => {
|
2020-07-12 17:59:05 +03:00
|
|
|
if (filterOrigin) {
|
|
|
|
if (PvCache.newerThan(data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
displayPageviews(data);
|
|
|
|
PvCache.saveOriginCache(JSON.stringify(data));
|
|
|
|
})
|
|
|
|
.then(() => fetchProxyPageviews());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fetchProxyPageviews();
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
2020-07-12 17:59:05 +03:00
|
|
|
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-30 15:38:41 +03:00
|
|
|
$(function() {
|
2020-08-19 07:26:45 +03:00
|
|
|
if ($(".pageviews").length > 0) {
|
2020-06-23 16:02:46 +03:00
|
|
|
PvCache.inspectKeys();
|
2020-04-01 15:33:49 +03:00
|
|
|
let cache = PvCache.getData();
|
|
|
|
|
|
|
|
if (cache) {
|
2020-07-12 17:59:05 +03:00
|
|
|
displayPageviews(cache);
|
2020-04-01 15:33:49 +03:00
|
|
|
|
2020-07-12 17:59:05 +03:00
|
|
|
if (PvCache.isExpired()) {
|
|
|
|
fetchPageviews(true, PvCache.isFromProxy());
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2020-07-12 17:59:05 +03:00
|
|
|
} else {
|
2020-04-01 15:33:49 +03:00
|
|
|
|
2020-07-12 17:59:05 +03:00
|
|
|
if (PvCache.isFromOrigin()) {
|
|
|
|
fetchPageviews(false);
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2020-07-12 17:59:05 +03:00
|
|
|
fetchPageviews();
|
2020-04-01 15:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2020-03-19 13:56:54 +03:00
|
|
|
});
|