From 48e4c7e6d3552cb1ec465c99538f7ce30c662fdc Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Tue, 6 Apr 2021 01:04:09 +0800 Subject: [PATCH] Fix the PV fetching failed when local cache is disabled also improve the PV report logic --- _includes/head.html | 7 ++-- assets/js/_utils/pageviews.js | 64 ++++++++++++++++++---------------- assets/js/dist/pvreport.min.js | 2 +- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/_includes/head.html b/_includes/head.html index b8f720b..30e5eef 100644 --- a/_includes/head.html +++ b/_includes/head.html @@ -7,19 +7,20 @@ {% if page.layout == 'home' or page.layout == 'post' %} - {% if site.google_analytics.pv.enabled %} + {% if site.google_analytics.pv.proxy_endpoint != '' - and site.google_analytics.pv.proxy_endpoint %} + and site.google_analytics.pv.proxy_endpoint %} {% endif %} {% if site.google_analytics.pv.cache %} - + {% endif %} {% endif %} + {% endif %} {% seo title=false %} diff --git a/assets/js/_utils/pageviews.js b/assets/js/_utils/pageviews.js index caa68cb..dc1514d 100644 --- a/assets/js/_utils/pageviews.js +++ b/assets/js/_utils/pageviews.js @@ -19,19 +19,20 @@ const getInitStatus = (function () { const PvOpts = (function () { return { - 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"); + 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_CREATION = "pv_created_date"; const KEY_PV_SRC = "pv_source"; @@ -71,12 +72,12 @@ const PvCache = (function () { return get(KEY_PV_SRC) === Source.PROXY; }, isExpired() { - if (PvCache.isFromOrigin()) { + if (PvData.isFromOrigin()) { let date = new Date(get(KEY_CREATION)); date.setDate(date.getDate() + 1); /* update origin records every day */ return Date.now() >= date.getTime(); - } else if (PvCache.isFromProxy()) { + } else if (PvData.isFromProxy()) { let date = new Date(get(KEY_CREATION)); date.setHours(date.getHours() + 1); /* update proxy records per hour */ return Date.now() >= date.getTime(); @@ -84,10 +85,10 @@ const PvCache = (function () { return false; }, getAllPageviews() { - return PvCache.getData().totalsForAllResults["ga:pageviews"]; + return PvData.getData().totalsForAllResults["ga:pageviews"]; }, newerThan(pv) { - return PvCache.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"]; + return PvData.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"]; }, inspectKeys() { if (localStorage.getItem(KEY_PV) === null @@ -98,7 +99,7 @@ const PvCache = (function () { } }; -}()); /* PvCache */ +}()); /* PvData */ function countUp(min, max, destId) { @@ -173,7 +174,7 @@ function fetchProxyPageviews() { dataType: "jsonp", jsonpCallback: "displayPageviews", success: (data, textStatus, jqXHR) => { - PvCache.saveProxyCache(JSON.stringify(data)); + PvData.saveProxyCache(JSON.stringify(data)); }, error: (jqXHR, textStatus, errorThrown) => { console.log("Failed to load pageviews from proxy server: " + errorThrown); @@ -182,18 +183,18 @@ function fetchProxyPageviews() { } -function fetchPageviews(fetchOrigin = true, filterOrigin = false) { - if (PvOpts.isEnabled() && fetchOrigin) { +function fetchPageviews(fetchOrigin = true, coverOrigin = false) { + if (fetchOrigin) { fetch(PvOpts.getLocalData()) .then((response) => response.json()) .then((data) => { - if (filterOrigin) { - if (PvCache.newerThan(data)) { + if (coverOrigin) { + if (PvData.newerThan(data)) { return; } } displayPageviews(data); - PvCache.saveOriginCache(JSON.stringify(data)); + PvData.saveOriginCache(JSON.stringify(data)); }) .then(() => fetchProxyPageviews()); @@ -205,28 +206,29 @@ function fetchPageviews(fetchOrigin = true, filterOrigin = false) { $(function() { - if ($(".pageviews").length > 0) { - PvCache.inspectKeys(); - let cache = PvCache.getData(); + if ($(".pageviews").length <= 0) { + return; + } - if (cache) { - displayPageviews(cache); + PvData.inspectKeys(); + let data = PvData.getData(); - if (PvCache.isExpired()) { - fetchPageviews(true, PvCache.isFromProxy()); + if (data) { + displayPageviews(data); - } else { - - if (PvCache.isFromOrigin()) { - fetchPageviews(false); - } - - } + if (PvData.isExpired()) { + fetchPageviews(true, PvData.isFromProxy()); } else { - fetchPageviews(); + + if (PvData.isFromOrigin()) { + fetchPageviews(false); + } + } + } else { + fetchPageviews(PvOpts.hasLocalData()); } }); diff --git a/assets/js/dist/pvreport.min.js b/assets/js/dist/pvreport.min.js index 520361f..c2c6f73 100644 --- a/assets/js/dist/pvreport.min.js +++ b/assets/js/dist/pvreport.min.js @@ -3,4 +3,4 @@ * © 2019 Cotes Chung * 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(tr&&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())}); \ No newline at end of file +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(er&&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()))}); \ No newline at end of file