web/assets/js/_src/pageviews.js

119 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-09-30 15:38:41 +03:00
/**
* Count pageviews form GA or local cache file.
*
* Dependences:
* - jQuery
* - countUp.js(https://github.com/inorganik/countUp.js)
*
2020-01-02 16:17:49 +03:00
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
2019-09-30 15:38:41 +03:00
* © 2018-2019 Cotes Chung
* MIT License
*/
2020-03-19 13:56:54 +03:00
function countUp(min, max, destId) {
2019-09-30 15:38:41 +03:00
if (min < max) {
2020-03-19 13:56:54 +03:00
var numAnim = new CountUp(destId, min, max);
2019-09-30 15:38:41 +03:00
if (!numAnim.error) {
numAnim.start();
} else {
console.error(numAnim.error);
}
}
}
2020-03-19 13:56:54 +03:00
2019-09-30 15:38:41 +03:00
function countPV(path, rows) {
/* path permalink looks like: '/posts/post-title/' */
var fileName = path.replace(/\/posts\//g, '').replace(/\//g, '.html'); /* e.g. post-title.html */
var count = 0;
var _v2_url = path.replace(/posts\//g, ''); /* the v2.0+ blog permalink: "/post-title/" */
for (var i = 0; i < rows.length; ++i) {
var gaPath = rows[i][0];
if (gaPath == path ||
gaPath == _v2_url ||
gaPath.concat('/') == _v2_url ||
gaPath.slice(gaPath.lastIndexOf('/') + 1) === fileName) { // old permalink record
count += parseInt(rows[i][1]);
}
}
return count;
}
2020-03-19 13:56:54 +03:00
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, ''));
if (count > initCount) {
countUp(initCount, count, elem.attr('id'));
}
}
}
function displayPageviews(data) {
if (data === undefined) {
2019-09-30 15:38:41 +03:00
return;
}
var hasInit = getInitStatus();
var rows = data.rows;
2019-09-30 15:38:41 +03:00
if ($("#post-list").length > 0) { // the Home page
$(".post-preview").each(function() {
var path = $(this).children("h1").children("a").attr("href");
2020-03-19 13:56:54 +03:00
tacklePV(rows, path, $(this).find('.pageviews'), hasInit);
2019-09-30 15:38:41 +03:00
});
} else if ($(".post").length > 0) { // the post
var path = window.location.pathname;
2020-03-19 13:56:54 +03:00
tacklePV(rows, path, $('#pv'), hasInit);
2019-09-30 15:38:41 +03:00
}
}
var getInitStatus = (function() {
var hasInit = false;
return function() {
if (hasInit) {
return true;
} else {
hasInit = true;
return false;
}
}
})();
2019-09-30 15:38:41 +03:00
$(function() {
// load pageview if this page has .pageviews
if ($('.pageviews').length > 0) {
// Get data from daily cache.
$.getJSON('/assets/data/pageviews.json', displayPageviews);
2019-09-30 15:38:41 +03:00
$.getJSON('/assets/data/proxy.json', function(meta) {
2019-09-30 15:38:41 +03:00
$.ajax({
type: 'GET',
url: meta.proxyUrl,
2019-09-30 15:38:41 +03:00
dataType: 'jsonp',
jsonpCallback: "displayPageviews",
error: function(jqXHR, textStatus, errorThrown) {
console.log("Failed to load pageviews from proxy server: " + errorThrown);
2019-09-30 15:38:41 +03:00
}
});
});
} // endif
2020-03-19 13:56:54 +03:00
});