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 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> <script src="{{ '/assets/js/home.min.js' | relative_url }}" async></script>
{% elsif page.layout == 'post' %} {% 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> <script src="{{ '/assets/js/post.min.js' | relative_url }}" async></script>
{% if page.math %} {% if page.math %}

View file

@ -13,7 +13,7 @@
{% if site.google_analytics.pv.enabled %} {% 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 %} {% 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) { 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 count = 0;
var _v2_url = path.replace(/posts\//g, ''); /* the v2.0+ blog permalink: "/post-title/" */
for (var i = 0; i < rows.length; ++i) { for (var i = 0; i < rows.length; ++i) {
var gaPath = rows[i][0]; var gaPath = rows[i][0];
if (gaPath == path || if (gaPath == path) { /* path format see: site.permalink */
gaPath == _v2_url ||
gaPath.concat('/') == _v2_url ||
gaPath.slice(gaPath.lastIndexOf('/') + 1) === fileName) { /* old permalink record */
count += parseInt(rows[i][1]); count += parseInt(rows[i][1]);
break;
} }
} }
@ -95,9 +89,9 @@ var getInitStatus = (function() {
var PvCache = (function() { var PvCache = (function() {
const KEY_PV = "pv"; const KEY_PV = "pv";
const KEY_CREATION = "pv_created_date"; const KEY_CREATION = "pv_created_date";
const KEY_PV_TYPE = "pv_type"; const KEY_PV_SRC = "pv_source";
var PvType = { var Source = {
ORIGIN: "origin", ORIGIN: "origin",
PROXY: "proxy" PROXY: "proxy"
}; };
@ -116,29 +110,29 @@ var PvCache = (function() {
}, },
saveOriginCache: function(pv) { saveOriginCache: function(pv) {
set(KEY_PV, pv); set(KEY_PV, pv);
set(KEY_PV_TYPE, PvType.ORIGIN ); set(KEY_PV_SRC, Source.ORIGIN );
set(KEY_CREATION, new Date().toJSON() ); set(KEY_CREATION, new Date().toJSON() );
}, },
saveProxyCache: function(pv) { saveProxyCache: function(pv) {
set(KEY_PV, pv); set(KEY_PV, pv);
set(KEY_PV_TYPE, PvType.PROXY ); set(KEY_PV_SRC, Source.PROXY );
set(KEY_CREATION, new Date().toJSON() ); set(KEY_CREATION, new Date().toJSON() );
}, },
isOriginCache: function() { isFromOrigin: function() {
return get(KEY_PV_TYPE) == PvType.ORIGIN; return get(KEY_PV_SRC) == Source.ORIGIN;
}, },
isProxyCache: function() { isFromProxy: function() {
return get(KEY_PV_TYPE) == PvType.PROXY; return get(KEY_PV_SRC) == Source.PROXY;
}, },
isExpired: function() { isExpired: function() {
if (PvCache.isOriginCache() ) { if (PvCache.isFromOrigin() ) {
let date = new Date(get(KEY_CREATION)); 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(); return Date.now() >= date.getTime();
} else if (PvCache.isProxyCache() ) { } else if (PvCache.isFromProxy() ) {
let date = new Date(get(KEY_CREATION) ); 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 Date.now() >= date.getTime();
} }
return false; return false;
@ -151,7 +145,7 @@ var PvCache = (function() {
}, },
inspectKeys: function() { inspectKeys: function() {
if (localStorage.getItem(KEY_PV) == null if (localStorage.getItem(KEY_PV) == null
|| localStorage.getItem(KEY_PV_TYPE) == null || localStorage.getItem(KEY_PV_SRC) == null
|| localStorage.getItem(KEY_CREATION) == null) { || localStorage.getItem(KEY_CREATION) == null) {
localStorage.clear(); localStorage.clear();
} }
@ -161,20 +155,33 @@ var PvCache = (function() {
})(); /* PvCache */ })(); /* PvCache */
function fetchOriginPageviews(pvData) { function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
if (pvData === undefined) { /* 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; return;
} }
displayPageviews(pvData); }
PvCache.saveOriginCache(JSON.stringify(pvData)); displayPageviews(data);
PvCache.saveOriginCache(JSON.stringify(data));
})
.then(() => fetchProxyPageviews());
} else {
fetchProxyPageviews();
}
} }
function fetchProxyPageviews() { function fetchProxyPageviews() {
let proxy = JSON.parse(proxyData); /* see file '/assets/data/pv-data.json' */
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
url: proxy.url, url: proxyEndpoint, /* see: /assets/js/_pv-config.js */
dataType: 'jsonp', dataType: 'jsonp',
jsonpCallback: "displayPageviews", jsonpCallback: "displayPageviews",
success: function(data, textStatus, jqXHR) { success: function(data, textStatus, jqXHR) {
@ -192,41 +199,24 @@ $(function() {
if ($('.pageviews').length > 0) { if ($('.pageviews').length > 0) {
PvCache.inspectKeys(); PvCache.inspectKeys();
let cache = PvCache.getData(); let cache = PvCache.getData();
if (cache) { if (cache) {
displayPageviews(cache);
if (PvCache.isExpired()) { if (PvCache.isExpired()) {
if (PvCache.isProxyCache() ) { fetchPageviews(true, PvCache.isFromProxy());
let originPvData = pageviews ? JSON.parse(pageviews) : undefined;
if (originPvData) {
if (PvCache.newerThan(originPvData)) {
displayPageviews(cache);
} else { } else {
fetchOriginPageviews(originPvData);
}
}
fetchProxyPageviews(); if (PvCache.isFromOrigin()) {
fetchPageviews(false);
} else if (PvCache.isOriginCache() ) {
fetchOriginPageviews(originPvData);
fetchProxyPageviews();
}
} else { /* still valid */
displayPageviews(cache);
if (PvCache.isOriginCache() ) {
fetchProxyPageviews();
} }
} }
} else { } else {
let originPvData = pageviews ? JSON.parse(pageviews) : undefined; fetchPageviews();
fetchOriginPageviews(originPvData);
fetchProxyPageviews();
} }
} }

View file

@ -106,6 +106,9 @@ const include = [
]; ];
const exclude = [ 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/' '/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 }}';