Improved the PV fetching.

Discard some of the old permalink PV records.
This commit is contained in:
Cotes Chung 2020-07-12 22:59:05 +08:00
parent 313c44f238
commit c0ba181246
6 changed files with 61 additions and 73 deletions

View File

@ -8,18 +8,10 @@
{% if page.layout == 'home' %}
{% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %}
<script src="{{ '/assets/js/data/pv-cache.js' | relative_url }}"></script>
{% endif %}
<script src="{{ '/assets/js/home.min.js' | relative_url }}" async></script>
{% elsif page.layout == 'post' %}
{% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %}
<script src="{{ '/assets/js/data/pv-cache.js' | relative_url }}"></script>
{% endif %}
<script src="{{ '/assets/js/post.min.js' | relative_url }}" async></script>
{% if page.math %}

View File

@ -13,7 +13,7 @@
{% if site.google_analytics.pv.enabled %}
const proxyData = '{"url": "{{ site.google_analytics.pv.proxy_endpoint }}"}';
{% include_relative _pv-config.js %}
{% include_relative _utils/pageviews.js %}

14
assets/js/_pv-config.js Normal file
View File

@ -0,0 +1,14 @@
/*!
PV configuration and Javascript conversion.
*/
const proxyEndpoint = "{{ site.google_analytics.pv.proxy_endpoint }}";
{% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %}
{% assign enabled = true %}
{% else %}
{% assign enabled = false %}
{% endif %}
const pvCacheEnabled = {{ enabled }};

View File

@ -24,19 +24,13 @@ function countUp(min, max, destId) {
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 */
if (gaPath == path) { /* path format see: site.permalink */
count += parseInt(rows[i][1]);
break;
}
}
@ -95,9 +89,9 @@ var getInitStatus = (function() {
var PvCache = (function() {
const KEY_PV = "pv";
const KEY_CREATION = "pv_created_date";
const KEY_PV_TYPE = "pv_type";
const KEY_PV_SRC = "pv_source";
var PvType = {
var Source = {
ORIGIN: "origin",
PROXY: "proxy"
};
@ -116,29 +110,29 @@ var PvCache = (function() {
},
saveOriginCache: function(pv) {
set(KEY_PV, pv);
set(KEY_PV_TYPE, PvType.ORIGIN );
set(KEY_PV_SRC, Source.ORIGIN );
set(KEY_CREATION, new Date().toJSON() );
},
saveProxyCache: function(pv) {
set(KEY_PV, pv);
set(KEY_PV_TYPE, PvType.PROXY );
set(KEY_PV_SRC, Source.PROXY );
set(KEY_CREATION, new Date().toJSON() );
},
isOriginCache: function() {
return get(KEY_PV_TYPE) == PvType.ORIGIN;
isFromOrigin: function() {
return get(KEY_PV_SRC) == Source.ORIGIN;
},
isProxyCache: function() {
return get(KEY_PV_TYPE) == PvType.PROXY;
isFromProxy: function() {
return get(KEY_PV_SRC) == Source.PROXY;
},
isExpired: function() {
if (PvCache.isOriginCache() ) {
if (PvCache.isFromOrigin() ) {
let date = new Date(get(KEY_CREATION));
date.setDate(date.getDate() + 1); /* fetch origin-data every day */
date.setDate(date.getDate() + 1); /* update origin records every day */
return Date.now() >= date.getTime();
} else if (PvCache.isProxyCache() ) {
} else if (PvCache.isFromProxy() ) {
let date = new Date(get(KEY_CREATION) );
date.setHours(date.getHours() + 1); /* proxy-data is updated every hour */
date.setHours(date.getHours() + 1); /* update proxy records per hour */
return Date.now() >= date.getTime();
}
return false;
@ -151,7 +145,7 @@ var PvCache = (function() {
},
inspectKeys: function() {
if (localStorage.getItem(KEY_PV) == null
|| localStorage.getItem(KEY_PV_TYPE) == null
|| localStorage.getItem(KEY_PV_SRC) == null
|| localStorage.getItem(KEY_CREATION) == null) {
localStorage.clear();
}
@ -161,20 +155,33 @@ var PvCache = (function() {
})(); /* PvCache */
function fetchOriginPageviews(pvData) {
if (pvData === undefined) {
function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
/* pvCacheEnabled see: /assets/js/_pv-config.js */
if (pvCacheEnabled && fetchOrigin) {
fetch('/assets/js/data/pageviews.json')
.then(response => response.json())
.then(data => {
if (filterOrigin) {
if (PvCache.newerThan(data)) {
return;
}
displayPageviews(pvData);
PvCache.saveOriginCache(JSON.stringify(pvData));
}
displayPageviews(data);
PvCache.saveOriginCache(JSON.stringify(data));
})
.then(() => fetchProxyPageviews());
} else {
fetchProxyPageviews();
}
}
function fetchProxyPageviews() {
let proxy = JSON.parse(proxyData); /* see file '/assets/data/pv-data.json' */
$.ajax({
type: 'GET',
url: proxy.url,
url: proxyEndpoint, /* see: /assets/js/_pv-config.js */
dataType: 'jsonp',
jsonpCallback: "displayPageviews",
success: function(data, textStatus, jqXHR) {
@ -192,41 +199,24 @@ $(function() {
if ($('.pageviews').length > 0) {
PvCache.inspectKeys();
let cache = PvCache.getData();
if (cache) {
displayPageviews(cache);
if (PvCache.isExpired()) {
if (PvCache.isProxyCache() ) {
let originPvData = pageviews ? JSON.parse(pageviews) : undefined;
if (originPvData) {
if (PvCache.newerThan(originPvData)) {
displayPageviews(cache);
fetchPageviews(true, PvCache.isFromProxy());
} else {
fetchOriginPageviews(originPvData);
}
}
fetchProxyPageviews();
} else if (PvCache.isOriginCache() ) {
fetchOriginPageviews(originPvData);
fetchProxyPageviews();
}
} else { /* still valid */
displayPageviews(cache);
if (PvCache.isOriginCache() ) {
fetchProxyPageviews();
if (PvCache.isFromOrigin()) {
fetchPageviews(false);
}
}
} else {
let originPvData = pageviews ? JSON.parse(pageviews) : undefined;
fetchOriginPageviews(originPvData);
fetchProxyPageviews();
fetchPageviews();
}
}

View File

@ -106,6 +106,9 @@ const include = [
];
const exclude = [
'/assets/js/data/pv-cache.js',
{%- if site.google_analytics.pv.proxy_url and site.google_analytics.pv.enabled -%}
'{{ site.google_analytics.pv.proxy_url }}',
{%- endif -%}
'/assets/js/data/pageviews.json',
'/img.shields.io/'
];

View File

@ -1,11 +0,0 @@
---
layout: compress
---
{%- capture pv_data -%}
{%- if site.google_analytics.pv.cache and site.google_analytics.pv.enabled -%}
{% include_relative _pageviews.json %}
{%- endif -%}
{%- endcapture -%}
const pageviews = '{{ pv_data }}';