krata/assets/js/_utils/pageviews.js

226 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-09-30 14:38:41 +02:00
/**
* Count pageviews form GA or local cache file.
*
* Dependences:
* - jQuery
2020-04-01 14:33:49 +02:00
* - countUp.js <https://github.com/inorganik/countUp.js>
2019-09-30 14:38:41 +02:00
*
2020-01-02 14:17:49 +01:00
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
2019-09-30 14:38:41 +02:00
* © 2018-2019 Cotes Chung
* MIT License
*/
2020-08-19 06:26:45 +02:00
var getInitStatus = (function () {
var hasInit = false;
2020-08-19 06:26:45 +02:00
return () => {
2020-04-01 14:33:49 +02:00
let ret = hasInit;
if (!hasInit) {
hasInit = true;
}
2020-04-01 14:33:49 +02:00
return ret;
2020-08-19 06:26:45 +02:00
};
}());
2020-08-19 06:26:45 +02:00
var PvCache = (function () {
2020-04-01 14:33:49 +02:00
const KEY_PV = "pv";
const KEY_CREATION = "pv_created_date";
const KEY_PV_SRC = "pv_source";
2020-04-01 14:33:49 +02:00
var Source = {
2020-04-01 14:33:49 +02:00
ORIGIN: "origin",
PROXY: "proxy"
};
function get(key) {
return localStorage.getItem(key);
}
function set(key, val) {
localStorage.setItem(key, val);
}
return {
2020-08-19 06:26:45 +02:00
getData() {
2020-04-01 14:33:49 +02:00
return JSON.parse(localStorage.getItem(KEY_PV) );
},
2020-08-19 06:26:45 +02:00
saveOriginCache(pv) {
2020-04-01 14:33:49 +02:00
set(KEY_PV, pv);
set(KEY_PV_SRC, Source.ORIGIN );
2020-04-01 14:33:49 +02:00
set(KEY_CREATION, new Date().toJSON() );
},
2020-08-19 06:26:45 +02:00
saveProxyCache(pv) {
2020-04-01 14:33:49 +02:00
set(KEY_PV, pv);
set(KEY_PV_SRC, Source.PROXY );
2020-04-01 14:33:49 +02:00
set(KEY_CREATION, new Date().toJSON() );
},
2020-08-19 06:26:45 +02:00
isFromOrigin() {
return get(KEY_PV_SRC) === Source.ORIGIN;
2020-04-01 14:33:49 +02:00
},
2020-08-19 06:26:45 +02:00
isFromProxy() {
return get(KEY_PV_SRC) === Source.PROXY;
2020-04-01 14:33:49 +02:00
},
2020-08-19 06:26:45 +02:00
isExpired() {
if (PvCache.isFromOrigin() ) {
2020-04-01 14:33:49 +02:00
let date = new Date(get(KEY_CREATION));
date.setDate(date.getDate() + 1); /* update origin records every day */
2020-04-01 14:33:49 +02:00
return Date.now() >= date.getTime();
} else if (PvCache.isFromProxy() ) {
2020-04-01 14:33:49 +02:00
let date = new Date(get(KEY_CREATION) );
date.setHours(date.getHours() + 1); /* update proxy records per hour */
2020-04-01 14:33:49 +02:00
return Date.now() >= date.getTime();
}
return false;
},
2020-08-19 06:26:45 +02:00
getAllPagevies() {
2020-04-01 14:33:49 +02:00
return PvCache.getData().totalsForAllResults["ga:pageviews"];
},
2020-08-19 06:26:45 +02:00
newerThan(pv) {
2020-04-01 14:33:49 +02:00
return PvCache.getAllPagevies() > pv.totalsForAllResults["ga:pageviews"];
},
2020-08-19 06:26:45 +02:00
inspectKeys() {
if (localStorage.getItem(KEY_PV) === null
|| localStorage.getItem(KEY_PV_SRC) === null
|| localStorage.getItem(KEY_CREATION) === null) {
localStorage.clear();
}
2020-04-01 14:33:49 +02:00
}
};
2020-08-19 06:26:45 +02:00
}()); /* PvCache */
function countUp(min, max, destId) {
if (min < max) {
var numAnim = new CountUp(destId, min, max);
if (!numAnim.error) {
numAnim.start();
} else {
console.error(numAnim.error);
}
}
}
function countPV(path, rows) {
var count = 0;
if (typeof rows !== "undefined" ) {
for (var i = 0; i < rows.length; ++i) {
2020-08-19 13:17:03 +02:00
var gaPath = rows[parseInt(i, 10)][0];
2020-08-19 06:26:45 +02:00
if (gaPath === path) { /* path format see: site.permalink */
2020-08-19 13:17:03 +02:00
count += parseInt(rows[parseInt(i, 10)][1], 10);
2020-08-19 06:26:45 +02:00
break;
}
}
}
return count;
}
function tacklePV(rows, path, elem, hasInit) {
var count = countPV(path, rows);
count = (count === 0 ? 1 : count);
if (!hasInit) {
elem.text(new Intl.NumberFormat().format(count));
} else {
var initCount = parseInt(elem.text().replace(/,/g, ""), 10);
if (count > initCount) {
countUp(initCount, count, elem.attr("id"));
}
}
}
function displayPageviews(data) {
if (typeof data === "undefined") {
return;
}
var hasInit = getInitStatus();
var rows = data.rows; /* could be undefined */
if ($("#post-list").length > 0) { /* the Home page */
$(".post-preview").each(function() {
2020-12-17 08:18:02 +01:00
var path = $(this).find("a").attr("href");
2020-08-19 06:26:45 +02:00
tacklePV(rows, path, $(this).find(".pageviews"), hasInit);
});
} else if ($(".post").length > 0) { /* the post */
var path = window.location.pathname;
tacklePV(rows, path, $("#pv"), hasInit);
}
}
function fetchProxyPageviews() {
$.ajax({
type: "GET",
url: proxyEndpoint, /* see: /assets/js/_pv-config.js */
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 14:33:49 +02:00
function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
/* pvCacheEnabled see: /assets/js/_pv-config.js */
if (pvCacheEnabled && fetchOrigin) {
2020-08-19 06:26:45 +02:00
fetch("/assets/js/data/pageviews.json")
.then((response) => response.json())
.then((data) => {
if (filterOrigin) {
if (PvCache.newerThan(data)) {
return;
}
}
displayPageviews(data);
PvCache.saveOriginCache(JSON.stringify(data));
})
.then(() => fetchProxyPageviews());
} else {
fetchProxyPageviews();
2020-04-01 14:33:49 +02:00
}
2020-04-01 14:33:49 +02:00
}
2019-09-30 14:38:41 +02:00
$(function() {
2020-04-01 14:33:49 +02:00
2020-08-19 06:26:45 +02:00
if ($(".pageviews").length > 0) {
2019-09-30 14:38:41 +02:00
PvCache.inspectKeys();
2020-04-01 14:33:49 +02:00
let cache = PvCache.getData();
if (cache) {
displayPageviews(cache);
2020-04-01 14:33:49 +02:00
if (PvCache.isExpired()) {
fetchPageviews(true, PvCache.isFromProxy());
2019-09-30 14:38:41 +02:00
} else {
2020-04-01 14:33:49 +02:00
if (PvCache.isFromOrigin()) {
fetchPageviews(false);
2020-04-01 14:33:49 +02:00
}
}
} else {
fetchPageviews();
2020-04-01 14:33:49 +02:00
}
}
2019-09-30 14:38:41 +02:00
2020-03-19 11:56:54 +01:00
});