From c0ba181246ebc50bc797071d176eb3691d89f254 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Sun, 12 Jul 2020 22:59:05 +0800 Subject: [PATCH] Improved the PV fetching. Discard some of the old permalink PV records. --- _includes/js-selector.html | 8 --- assets/js/_home-post.js | 2 +- assets/js/_pv-config.js | 14 ++++++ assets/js/_utils/pageviews.js | 94 ++++++++++++++++------------------- assets/js/data/cache-list.js | 5 +- assets/js/data/pv-cache.js | 11 ---- 6 files changed, 61 insertions(+), 73 deletions(-) create mode 100644 assets/js/_pv-config.js delete mode 100644 assets/js/data/pv-cache.js diff --git a/_includes/js-selector.html b/_includes/js-selector.html index 83afe3c..4fb6c70 100644 --- a/_includes/js-selector.html +++ b/_includes/js-selector.html @@ -8,18 +8,10 @@ {% if page.layout == 'home' %} - {% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %} - - {% endif %} - {% elsif page.layout == 'post' %} - {% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %} - - {% endif %} - {% if page.math %} diff --git a/assets/js/_home-post.js b/assets/js/_home-post.js index 48f5dc4..c802777 100644 --- a/assets/js/_home-post.js +++ b/assets/js/_home-post.js @@ -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 %} diff --git a/assets/js/_pv-config.js b/assets/js/_pv-config.js new file mode 100644 index 0000000..29acbbe --- /dev/null +++ b/assets/js/_pv-config.js @@ -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 }}; diff --git a/assets/js/_utils/pageviews.js b/assets/js/_utils/pageviews.js index 931b5a5..2b34d19 100644 --- a/assets/js/_utils/pageviews.js +++ b/assets/js/_utils/pageviews.js @@ -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) { - return; +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(data); + PvCache.saveOriginCache(JSON.stringify(data)); + }) + .then(() => fetchProxyPageviews()); + + } else { + fetchProxyPageviews(); } - displayPageviews(pvData); - PvCache.saveOriginCache(JSON.stringify(pvData)); + } 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); - } else { - fetchOriginPageviews(originPvData); - } - } + fetchPageviews(true, PvCache.isFromProxy()); - fetchProxyPageviews(); + } else { - } 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(); } } diff --git a/assets/js/data/cache-list.js b/assets/js/data/cache-list.js index 31db1b8..ab88073 100644 --- a/assets/js/data/cache-list.js +++ b/assets/js/data/cache-list.js @@ -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/' ]; diff --git a/assets/js/data/pv-cache.js b/assets/js/data/pv-cache.js deleted file mode 100644 index 2e309bf..0000000 --- a/assets/js/data/pv-cache.js +++ /dev/null @@ -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 }}';