Fix the PV fetching failed when local cache is disabled
also improve the PV report logic
This commit is contained in:
parent
991f53f8b2
commit
48e4c7e6d3
3 changed files with 38 additions and 35 deletions
|
@ -7,19 +7,20 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
|
|
||||||
{% if page.layout == 'home' or page.layout == 'post' %}
|
{% if page.layout == 'home' or page.layout == 'post' %}
|
||||||
<meta name="pv-cache-enabled" content="{{ site.google_analytics.pv.enabled }}">
|
|
||||||
|
|
||||||
{% if site.google_analytics.pv.enabled %}
|
{% if site.google_analytics.pv.enabled %}
|
||||||
|
|
||||||
{% if site.google_analytics.pv.proxy_endpoint != ''
|
{% if site.google_analytics.pv.proxy_endpoint != ''
|
||||||
and site.google_analytics.pv.proxy_endpoint %}
|
and site.google_analytics.pv.proxy_endpoint %}
|
||||||
<meta name="pv-proxy-endpoint" content="{{ site.google_analytics.pv.proxy_endpoint }}">
|
<meta name="pv-proxy-endpoint" content="{{ site.google_analytics.pv.proxy_endpoint }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if site.google_analytics.pv.cache %}
|
{% if site.google_analytics.pv.cache %}
|
||||||
<meta name="pv-cache-data" content="{{ '/assets/js/data/pageviews.json' | relative_url }}">
|
<meta name="pv-cache-path" content="{{ '/assets/js/data/pageviews.json' | relative_url }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% seo title=false %}
|
{% seo title=false %}
|
||||||
|
|
|
@ -19,19 +19,20 @@ const getInitStatus = (function () {
|
||||||
|
|
||||||
const PvOpts = (function () {
|
const PvOpts = (function () {
|
||||||
return {
|
return {
|
||||||
isEnabled() {
|
|
||||||
return "true" === $("meta[name=pv-cache-enabled]").attr("content");
|
|
||||||
},
|
|
||||||
getProxyEndpoint() {
|
getProxyEndpoint() {
|
||||||
return $("meta[name=pv-proxy-endpoint]").attr("content");
|
return $("meta[name=pv-proxy-endpoint]").attr("content");
|
||||||
},
|
},
|
||||||
getLocalData() {
|
getLocalData() {
|
||||||
return $("meta[name=pv-cache-data]").attr("content");
|
return $("meta[name=pv-cache-path]").attr("content");
|
||||||
|
},
|
||||||
|
hasLocalData() {
|
||||||
|
let path = PvOpts.getLocalData();
|
||||||
|
return (typeof path !== "undefined" && path !== false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
const PvCache = (function () {
|
const PvData = (function () {
|
||||||
const KEY_PV = "pv";
|
const KEY_PV = "pv";
|
||||||
const KEY_CREATION = "pv_created_date";
|
const KEY_CREATION = "pv_created_date";
|
||||||
const KEY_PV_SRC = "pv_source";
|
const KEY_PV_SRC = "pv_source";
|
||||||
|
@ -71,12 +72,12 @@ const PvCache = (function () {
|
||||||
return get(KEY_PV_SRC) === Source.PROXY;
|
return get(KEY_PV_SRC) === Source.PROXY;
|
||||||
},
|
},
|
||||||
isExpired() {
|
isExpired() {
|
||||||
if (PvCache.isFromOrigin()) {
|
if (PvData.isFromOrigin()) {
|
||||||
let date = new Date(get(KEY_CREATION));
|
let date = new Date(get(KEY_CREATION));
|
||||||
date.setDate(date.getDate() + 1); /* update origin records 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.isFromProxy()) {
|
} else if (PvData.isFromProxy()) {
|
||||||
let date = new Date(get(KEY_CREATION));
|
let date = new Date(get(KEY_CREATION));
|
||||||
date.setHours(date.getHours() + 1); /* update proxy records per hour */
|
date.setHours(date.getHours() + 1); /* update proxy records per hour */
|
||||||
return Date.now() >= date.getTime();
|
return Date.now() >= date.getTime();
|
||||||
|
@ -84,10 +85,10 @@ const PvCache = (function () {
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
getAllPageviews() {
|
getAllPageviews() {
|
||||||
return PvCache.getData().totalsForAllResults["ga:pageviews"];
|
return PvData.getData().totalsForAllResults["ga:pageviews"];
|
||||||
},
|
},
|
||||||
newerThan(pv) {
|
newerThan(pv) {
|
||||||
return PvCache.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
|
return PvData.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
|
||||||
},
|
},
|
||||||
inspectKeys() {
|
inspectKeys() {
|
||||||
if (localStorage.getItem(KEY_PV) === null
|
if (localStorage.getItem(KEY_PV) === null
|
||||||
|
@ -98,7 +99,7 @@ const PvCache = (function () {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}()); /* PvCache */
|
}()); /* PvData */
|
||||||
|
|
||||||
|
|
||||||
function countUp(min, max, destId) {
|
function countUp(min, max, destId) {
|
||||||
|
@ -173,7 +174,7 @@ function fetchProxyPageviews() {
|
||||||
dataType: "jsonp",
|
dataType: "jsonp",
|
||||||
jsonpCallback: "displayPageviews",
|
jsonpCallback: "displayPageviews",
|
||||||
success: (data, textStatus, jqXHR) => {
|
success: (data, textStatus, jqXHR) => {
|
||||||
PvCache.saveProxyCache(JSON.stringify(data));
|
PvData.saveProxyCache(JSON.stringify(data));
|
||||||
},
|
},
|
||||||
error: (jqXHR, textStatus, errorThrown) => {
|
error: (jqXHR, textStatus, errorThrown) => {
|
||||||
console.log("Failed to load pageviews from proxy server: " + errorThrown);
|
console.log("Failed to load pageviews from proxy server: " + errorThrown);
|
||||||
|
@ -182,18 +183,18 @@ function fetchProxyPageviews() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
|
function fetchPageviews(fetchOrigin = true, coverOrigin = false) {
|
||||||
if (PvOpts.isEnabled() && fetchOrigin) {
|
if (fetchOrigin) {
|
||||||
fetch(PvOpts.getLocalData())
|
fetch(PvOpts.getLocalData())
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (filterOrigin) {
|
if (coverOrigin) {
|
||||||
if (PvCache.newerThan(data)) {
|
if (PvData.newerThan(data)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
displayPageviews(data);
|
displayPageviews(data);
|
||||||
PvCache.saveOriginCache(JSON.stringify(data));
|
PvData.saveOriginCache(JSON.stringify(data));
|
||||||
})
|
})
|
||||||
.then(() => fetchProxyPageviews());
|
.then(() => fetchProxyPageviews());
|
||||||
|
|
||||||
|
@ -205,28 +206,29 @@ function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
|
||||||
|
|
||||||
|
|
||||||
$(function() {
|
$(function() {
|
||||||
if ($(".pageviews").length > 0) {
|
if ($(".pageviews").length <= 0) {
|
||||||
PvCache.inspectKeys();
|
return;
|
||||||
let cache = PvCache.getData();
|
}
|
||||||
|
|
||||||
if (cache) {
|
PvData.inspectKeys();
|
||||||
displayPageviews(cache);
|
let data = PvData.getData();
|
||||||
|
|
||||||
if (PvCache.isExpired()) {
|
if (data) {
|
||||||
fetchPageviews(true, PvCache.isFromProxy());
|
displayPageviews(data);
|
||||||
|
|
||||||
|
if (PvData.isExpired()) {
|
||||||
|
fetchPageviews(true, PvData.isFromProxy());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (PvCache.isFromOrigin()) {
|
if (PvData.isFromOrigin()) {
|
||||||
fetchPageviews(false);
|
fetchPageviews(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fetchPageviews();
|
fetchPageviews(PvOpts.hasLocalData());
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
2
assets/js/dist/pvreport.min.js
vendored
2
assets/js/dist/pvreport.min.js
vendored
|
@ -3,4 +3,4 @@
|
||||||
* © 2019 Cotes Chung
|
* © 2019 Cotes Chung
|
||||||
* MIT Licensed
|
* MIT Licensed
|
||||||
*/
|
*/
|
||||||
const getInitStatus=function(){let t=!1;return()=>{var e=t;return t=t||!0,e}}(),PvOpts={isEnabled(){return"true"===$("meta[name=pv-cache-enabled]").attr("content")},getProxyEndpoint(){return $("meta[name=pv-proxy-endpoint]").attr("content")},getLocalData(){return $("meta[name=pv-cache-data]").attr("content")}},PvCache=function(){const t="pv",a="pv_created_date",r="pv_source",n={ORIGIN:"origin",PROXY:"proxy"};function o(e){return localStorage.getItem(e)}function i(e,t){localStorage.setItem(e,t)}return{getData(){return JSON.parse(localStorage.getItem(t))},saveOriginCache(e){i(t,e),i(r,n.ORIGIN),i(a,(new Date).toJSON())},saveProxyCache(e){i(t,e),i(r,n.PROXY),i(a,(new Date).toJSON())},isFromOrigin(){return o(r)===n.ORIGIN},isFromProxy(){return o(r)===n.PROXY},isExpired(){if(PvCache.isFromOrigin()){let e=new Date(o(a));return e.setDate(e.getDate()+1),Date.now()>=e.getTime()}if(PvCache.isFromProxy()){let e=new Date(o(a));return e.setHours(e.getHours()+1),Date.now()>=e.getTime()}return!1},getAllPageviews(){return PvCache.getData().totalsForAllResults["ga:pageviews"]},newerThan(e){return PvCache.getAllPageviews()>e.totalsForAllResults["ga:pageviews"]},inspectKeys(){null!==localStorage.getItem(t)&&null!==localStorage.getItem(r)&&null!==localStorage.getItem(a)||localStorage.clear()}}}();function countUp(t,a,r){if(t<a){let e=new CountUp(r,t,a);e.error?console.error(e.error):e.start()}}function countPV(t,a){let r=0;if(void 0!==a)for(let e=0;e<a.length;++e)if(a[parseInt(e,10)][0]===t){r+=parseInt(a[parseInt(e,10)][1],10);break}return r}function tacklePV(e,t,a,r){let n=countPV(t,e);n=0===n?1:n,r?(r=parseInt(a.text().replace(/,/g,""),10),n>r&&countUp(r,n,a.attr("id"))):a.text((new Intl.NumberFormat).format(n))}function displayPageviews(e){if(void 0!==e){let t=getInitStatus();const a=e.rows;0<$("#post-list").length?$(".post-preview").each(function(){var e=$(this).find("a").attr("href");tacklePV(a,e,$(this).find(".pageviews"),t)}):0<$(".post").length&&(e=window.location.pathname,tacklePV(a,e,$("#pv"),t))}}function fetchProxyPageviews(){$.ajax({type:"GET",url:PvOpts.getProxyEndpoint(),dataType:"jsonp",jsonpCallback:"displayPageviews",success:(e,t,a)=>{PvCache.saveProxyCache(JSON.stringify(e))},error:(e,t,a)=>{console.log("Failed to load pageviews from proxy server: "+a)}})}function fetchPageviews(e=!0,t=!1){PvOpts.isEnabled()&&e?fetch(PvOpts.getLocalData()).then(e=>e.json()).then(e=>{t&&PvCache.newerThan(e)||(displayPageviews(e),PvCache.saveOriginCache(JSON.stringify(e)))}).then(()=>fetchProxyPageviews()):fetchProxyPageviews()}$(function(){var e;0<$(".pageviews").length&&(PvCache.inspectKeys(),(e=PvCache.getData())?(displayPageviews(e),PvCache.isExpired()?fetchPageviews(!0,PvCache.isFromProxy()):PvCache.isFromOrigin()&&fetchPageviews(!1)):fetchPageviews())});
|
const getInitStatus=function(){let e=!1;return()=>{var t=e;return e=e||!0,t}}(),PvOpts={getProxyEndpoint(){return $("meta[name=pv-proxy-endpoint]").attr("content")},getLocalData(){return $("meta[name=pv-cache-path]").attr("content")},hasLocalData(){var t=PvOpts.getLocalData();return void 0!==t&&!1!==t}},PvData=function(){const e="pv",a="pv_created_date",r="pv_source",n={ORIGIN:"origin",PROXY:"proxy"};function o(t){return localStorage.getItem(t)}function i(t,e){localStorage.setItem(t,e)}return{getData(){return JSON.parse(localStorage.getItem(e))},saveOriginCache(t){i(e,t),i(r,n.ORIGIN),i(a,(new Date).toJSON())},saveProxyCache(t){i(e,t),i(r,n.PROXY),i(a,(new Date).toJSON())},isFromOrigin(){return o(r)===n.ORIGIN},isFromProxy(){return o(r)===n.PROXY},isExpired(){if(PvData.isFromOrigin()){let t=new Date(o(a));return t.setDate(t.getDate()+1),Date.now()>=t.getTime()}if(PvData.isFromProxy()){let t=new Date(o(a));return t.setHours(t.getHours()+1),Date.now()>=t.getTime()}return!1},getAllPageviews(){return PvData.getData().totalsForAllResults["ga:pageviews"]},newerThan(t){return PvData.getAllPageviews()>t.totalsForAllResults["ga:pageviews"]},inspectKeys(){null!==localStorage.getItem(e)&&null!==localStorage.getItem(r)&&null!==localStorage.getItem(a)||localStorage.clear()}}}();function countUp(e,a,r){if(e<a){let t=new CountUp(r,e,a);t.error?console.error(t.error):t.start()}}function countPV(e,a){let r=0;if(void 0!==a)for(let t=0;t<a.length;++t)if(a[parseInt(t,10)][0]===e){r+=parseInt(a[parseInt(t,10)][1],10);break}return r}function tacklePV(t,e,a,r){let n=countPV(e,t);n=0===n?1:n,r?(r=parseInt(a.text().replace(/,/g,""),10),n>r&&countUp(r,n,a.attr("id"))):a.text((new Intl.NumberFormat).format(n))}function displayPageviews(t){if(void 0!==t){let e=getInitStatus();const a=t.rows;0<$("#post-list").length?$(".post-preview").each(function(){var t=$(this).find("a").attr("href");tacklePV(a,t,$(this).find(".pageviews"),e)}):0<$(".post").length&&(t=window.location.pathname,tacklePV(a,t,$("#pv"),e))}}function fetchProxyPageviews(){$.ajax({type:"GET",url:PvOpts.getProxyEndpoint(),dataType:"jsonp",jsonpCallback:"displayPageviews",success:(t,e,a)=>{PvData.saveProxyCache(JSON.stringify(t))},error:(t,e,a)=>{console.log("Failed to load pageviews from proxy server: "+a)}})}function fetchPageviews(t=!0,e=!1){t?fetch(PvOpts.getLocalData()).then(t=>t.json()).then(t=>{e&&PvData.newerThan(t)||(displayPageviews(t),PvData.saveOriginCache(JSON.stringify(t)))}).then(()=>fetchProxyPageviews()):fetchProxyPageviews()}$(function(){var t;$(".pageviews").length<=0||(PvData.inspectKeys(),(t=PvData.getData())?(displayPageviews(t),PvData.isExpired()?fetchPageviews(!0,PvData.isFromProxy()):PvData.isFromOrigin()&&fetchPageviews(!1)):fetchPageviews(PvOpts.hasLocalData()))});
|
Loading…
Reference in a new issue