krata/_javascript/utils/pageviews.js

251 lines
5.6 KiB
JavaScript
Raw Normal View History

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