Skip to content

Commit 4c5701d

Browse files
committed
Fix the PV fetching failed when local cache is disabled
also improve the PV report logic
1 parent 255fe2d commit 4c5701d

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

_includes/head.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
88

99
{% if page.layout == 'home' or page.layout == 'post' %}
10-
<meta name="pv-cache-enabled" content="{{ site.google_analytics.pv.enabled }}">
1110

1211
{% if site.google_analytics.pv.enabled %}
12+
1313
{% if site.google_analytics.pv.proxy_endpoint != ''
14-
and site.google_analytics.pv.proxy_endpoint %}
14+
and site.google_analytics.pv.proxy_endpoint %}
1515
<meta name="pv-proxy-endpoint" content="{{ site.google_analytics.pv.proxy_endpoint }}">
1616
{% endif %}
1717

1818
{% if site.google_analytics.pv.cache %}
19-
<meta name="pv-cache-data" content="{{ '/assets/js/data/pageviews.json' | relative_url }}">
19+
<meta name="pv-cache-path" content="{{ '/assets/js/data/pageviews.json' | relative_url }}">
2020
{% endif %}
2121

2222
{% endif %}
23+
2324
{% endif %}
2425

2526
{% seo title=false %}

assets/js/_utils/pageviews.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ const getInitStatus = (function () {
1919

2020
const PvOpts = (function () {
2121
return {
22-
isEnabled() {
23-
return "true" === $("meta[name=pv-cache-enabled]").attr("content");
24-
},
2522
getProxyEndpoint() {
2623
return $("meta[name=pv-proxy-endpoint]").attr("content");
2724
},
2825
getLocalData() {
29-
return $("meta[name=pv-cache-data]").attr("content");
26+
return $("meta[name=pv-cache-path]").attr("content");
27+
},
28+
hasLocalData() {
29+
let path = PvOpts.getLocalData();
30+
return (typeof path !== "undefined" && path !== false);
3031
}
3132
}
3233
}());
3334

34-
const PvCache = (function () {
35+
const PvData = (function () {
3536
const KEY_PV = "pv";
3637
const KEY_CREATION = "pv_created_date";
3738
const KEY_PV_SRC = "pv_source";
@@ -71,23 +72,23 @@ const PvCache = (function () {
7172
return get(KEY_PV_SRC) === Source.PROXY;
7273
},
7374
isExpired() {
74-
if (PvCache.isFromOrigin()) {
75+
if (PvData.isFromOrigin()) {
7576
let date = new Date(get(KEY_CREATION));
7677
date.setDate(date.getDate() + 1); /* update origin records every day */
7778
return Date.now() >= date.getTime();
7879

79-
} else if (PvCache.isFromProxy()) {
80+
} else if (PvData.isFromProxy()) {
8081
let date = new Date(get(KEY_CREATION));
8182
date.setHours(date.getHours() + 1); /* update proxy records per hour */
8283
return Date.now() >= date.getTime();
8384
}
8485
return false;
8586
},
8687
getAllPageviews() {
87-
return PvCache.getData().totalsForAllResults["ga:pageviews"];
88+
return PvData.getData().totalsForAllResults["ga:pageviews"];
8889
},
8990
newerThan(pv) {
90-
return PvCache.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
91+
return PvData.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
9192
},
9293
inspectKeys() {
9394
if (localStorage.getItem(KEY_PV) === null
@@ -98,7 +99,7 @@ const PvCache = (function () {
9899
}
99100
};
100101

101-
}()); /* PvCache */
102+
}()); /* PvData */
102103

103104

104105
function countUp(min, max, destId) {
@@ -173,7 +174,7 @@ function fetchProxyPageviews() {
173174
dataType: "jsonp",
174175
jsonpCallback: "displayPageviews",
175176
success: (data, textStatus, jqXHR) => {
176-
PvCache.saveProxyCache(JSON.stringify(data));
177+
PvData.saveProxyCache(JSON.stringify(data));
177178
},
178179
error: (jqXHR, textStatus, errorThrown) => {
179180
console.log("Failed to load pageviews from proxy server: " + errorThrown);
@@ -182,18 +183,18 @@ function fetchProxyPageviews() {
182183
}
183184

184185

185-
function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
186-
if (PvOpts.isEnabled() && fetchOrigin) {
186+
function fetchPageviews(fetchOrigin = true, coverOrigin = false) {
187+
if (fetchOrigin) {
187188
fetch(PvOpts.getLocalData())
188189
.then((response) => response.json())
189190
.then((data) => {
190-
if (filterOrigin) {
191-
if (PvCache.newerThan(data)) {
191+
if (coverOrigin) {
192+
if (PvData.newerThan(data)) {
192193
return;
193194
}
194195
}
195196
displayPageviews(data);
196-
PvCache.saveOriginCache(JSON.stringify(data));
197+
PvData.saveOriginCache(JSON.stringify(data));
197198
})
198199
.then(() => fetchProxyPageviews());
199200

@@ -205,28 +206,29 @@ function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
205206

206207

207208
$(function() {
208-
if ($(".pageviews").length > 0) {
209-
PvCache.inspectKeys();
210-
let cache = PvCache.getData();
209+
if ($(".pageviews").length <= 0) {
210+
return;
211+
}
211212

212-
if (cache) {
213-
displayPageviews(cache);
213+
PvData.inspectKeys();
214+
let data = PvData.getData();
214215

215-
if (PvCache.isExpired()) {
216-
fetchPageviews(true, PvCache.isFromProxy());
216+
if (data) {
217+
displayPageviews(data);
217218

218-
} else {
219+
if (PvData.isExpired()) {
220+
fetchPageviews(true, PvData.isFromProxy());
219221

220-
if (PvCache.isFromOrigin()) {
221-
fetchPageviews(false);
222-
}
222+
} else {
223223

224+
if (PvData.isFromOrigin()) {
225+
fetchPageviews(false);
224226
}
225227

226-
} else {
227-
fetchPageviews();
228228
}
229229

230+
} else {
231+
fetchPageviews(PvOpts.hasLocalData());
230232
}
231233

232234
});

assets/js/dist/pvreport.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)