Skip to content

Commit d050157

Browse files
committed
Improved the PV fetching.
Discard some of the old permalink PV records.
1 parent 7f13e01 commit d050157

6 files changed

Lines changed: 61 additions & 73 deletions

File tree

_includes/js-selector.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@
88

99
{% if page.layout == 'home' %}
1010

11-
{% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %}
12-
<script src="{{ '/assets/js/data/pv-cache.js' | relative_url }}"></script>
13-
{% endif %}
14-
1511
<script src="{{ '/assets/js/home.min.js' | relative_url }}" async></script>
1612

1713
{% elsif page.layout == 'post' %}
1814

19-
{% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %}
20-
<script src="{{ '/assets/js/data/pv-cache.js' | relative_url }}"></script>
21-
{% endif %}
22-
2315
<script src="{{ '/assets/js/post.min.js' | relative_url }}" async></script>
2416

2517
{% if page.math %}

assets/js/_home-post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
{% if site.google_analytics.pv.enabled %}
1515

16-
const proxyData = '{"url": "{{ site.google_analytics.pv.proxy_endpoint }}"}';
16+
{% include_relative _pv-config.js %}
1717

1818
{% include_relative _utils/pageviews.js %}
1919

assets/js/_pv-config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*!
2+
PV configuration and Javascript conversion.
3+
*/
4+
5+
const proxyEndpoint = "{{ site.google_analytics.pv.proxy_endpoint }}";
6+
7+
8+
{% if site.google_analytics.pv.cache and site.google_analytics.pv.enabled %}
9+
{% assign enabled = true %}
10+
{% else %}
11+
{% assign enabled = false %}
12+
{% endif %}
13+
14+
const pvCacheEnabled = {{ enabled }};

assets/js/_utils/pageviews.js

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,13 @@ function countUp(min, max, destId) {
2424

2525

2626
function countPV(path, rows) {
27-
/* path permalink looks like: '/posts/post-title/' */
28-
var fileName = path.replace(/\/posts\//g, '').replace(/\//g, '.html'); /* e.g. post-title.html */
2927
var count = 0;
3028

31-
var _v2_url = path.replace(/posts\//g, ''); /* the v2.0+ blog permalink: "/post-title/" */
32-
3329
for (var i = 0; i < rows.length; ++i) {
3430
var gaPath = rows[i][0];
35-
if (gaPath == path ||
36-
gaPath == _v2_url ||
37-
gaPath.concat('/') == _v2_url ||
38-
gaPath.slice(gaPath.lastIndexOf('/') + 1) === fileName) { /* old permalink record */
31+
if (gaPath == path) { /* path format see: site.permalink */
3932
count += parseInt(rows[i][1]);
33+
break;
4034
}
4135
}
4236

@@ -95,9 +89,9 @@ var getInitStatus = (function() {
9589
var PvCache = (function() {
9690
const KEY_PV = "pv";
9791
const KEY_CREATION = "pv_created_date";
98-
const KEY_PV_TYPE = "pv_type";
92+
const KEY_PV_SRC = "pv_source";
9993

100-
var PvType = {
94+
var Source = {
10195
ORIGIN: "origin",
10296
PROXY: "proxy"
10397
};
@@ -116,29 +110,29 @@ var PvCache = (function() {
116110
},
117111
saveOriginCache: function(pv) {
118112
set(KEY_PV, pv);
119-
set(KEY_PV_TYPE, PvType.ORIGIN );
113+
set(KEY_PV_SRC, Source.ORIGIN );
120114
set(KEY_CREATION, new Date().toJSON() );
121115
},
122116
saveProxyCache: function(pv) {
123117
set(KEY_PV, pv);
124-
set(KEY_PV_TYPE, PvType.PROXY );
118+
set(KEY_PV_SRC, Source.PROXY );
125119
set(KEY_CREATION, new Date().toJSON() );
126120
},
127-
isOriginCache: function() {
128-
return get(KEY_PV_TYPE) == PvType.ORIGIN;
121+
isFromOrigin: function() {
122+
return get(KEY_PV_SRC) == Source.ORIGIN;
129123
},
130-
isProxyCache: function() {
131-
return get(KEY_PV_TYPE) == PvType.PROXY;
124+
isFromProxy: function() {
125+
return get(KEY_PV_SRC) == Source.PROXY;
132126
},
133127
isExpired: function() {
134-
if (PvCache.isOriginCache() ) {
128+
if (PvCache.isFromOrigin() ) {
135129
let date = new Date(get(KEY_CREATION));
136-
date.setDate(date.getDate() + 1); /* fetch origin-data every day */
130+
date.setDate(date.getDate() + 1); /* update origin records every day */
137131
return Date.now() >= date.getTime();
138132

139-
} else if (PvCache.isProxyCache() ) {
133+
} else if (PvCache.isFromProxy() ) {
140134
let date = new Date(get(KEY_CREATION) );
141-
date.setHours(date.getHours() + 1); /* proxy-data is updated every hour */
135+
date.setHours(date.getHours() + 1); /* update proxy records per hour */
142136
return Date.now() >= date.getTime();
143137
}
144138
return false;
@@ -151,7 +145,7 @@ var PvCache = (function() {
151145
},
152146
inspectKeys: function() {
153147
if (localStorage.getItem(KEY_PV) == null
154-
|| localStorage.getItem(KEY_PV_TYPE) == null
148+
|| localStorage.getItem(KEY_PV_SRC) == null
155149
|| localStorage.getItem(KEY_CREATION) == null) {
156150
localStorage.clear();
157151
}
@@ -161,20 +155,33 @@ var PvCache = (function() {
161155
})(); /* PvCache */
162156

163157

164-
function fetchOriginPageviews(pvData) {
165-
if (pvData === undefined) {
166-
return;
158+
function fetchPageviews(fetchOrigin = true, filterOrigin = false) {
159+
/* pvCacheEnabled › see: /assets/js/_pv-config.js */
160+
if (pvCacheEnabled && fetchOrigin) {
161+
fetch('/assets/js/data/pageviews.json')
162+
.then(response => response.json())
163+
.then(data => {
164+
if (filterOrigin) {
165+
if (PvCache.newerThan(data)) {
166+
return;
167+
}
168+
}
169+
displayPageviews(data);
170+
PvCache.saveOriginCache(JSON.stringify(data));
171+
})
172+
.then(() => fetchProxyPageviews());
173+
174+
} else {
175+
fetchProxyPageviews();
167176
}
168-
displayPageviews(pvData);
169-
PvCache.saveOriginCache(JSON.stringify(pvData));
177+
170178
}
171179

172180

173181
function fetchProxyPageviews() {
174-
let proxy = JSON.parse(proxyData); /* see file '/assets/data/pv-data.json' */
175182
$.ajax({
176183
type: 'GET',
177-
url: proxy.url,
184+
url: proxyEndpoint, /* see: /assets/js/_pv-config.js */
178185
dataType: 'jsonp',
179186
jsonpCallback: "displayPageviews",
180187
success: function(data, textStatus, jqXHR) {
@@ -192,41 +199,24 @@ $(function() {
192199
if ($('.pageviews').length > 0) {
193200

194201
PvCache.inspectKeys();
195-
196202
let cache = PvCache.getData();
197203

198204
if (cache) {
199-
if (PvCache.isExpired()) {
200-
if (PvCache.isProxyCache() ) {
201-
let originPvData = pageviews ? JSON.parse(pageviews) : undefined;
202-
if (originPvData) {
203-
if (PvCache.newerThan(originPvData)) {
204-
displayPageviews(cache);
205-
} else {
206-
fetchOriginPageviews(originPvData);
207-
}
208-
}
205+
displayPageviews(cache);
209206

210-
fetchProxyPageviews();
211-
212-
} else if (PvCache.isOriginCache() ) {
213-
fetchOriginPageviews(originPvData);
214-
fetchProxyPageviews();
215-
}
207+
if (PvCache.isExpired()) {
208+
fetchPageviews(true, PvCache.isFromProxy());
216209

217-
} else { /* still valid */
218-
displayPageviews(cache);
210+
} else {
219211

220-
if (PvCache.isOriginCache() ) {
221-
fetchProxyPageviews();
212+
if (PvCache.isFromOrigin()) {
213+
fetchPageviews(false);
222214
}
223215

224216
}
225217

226218
} else {
227-
let originPvData = pageviews ? JSON.parse(pageviews) : undefined;
228-
fetchOriginPageviews(originPvData);
229-
fetchProxyPageviews();
219+
fetchPageviews();
230220
}
231221

232222
}

assets/js/data/cache-list.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ const include = [
106106
];
107107

108108
const exclude = [
109-
'/assets/js/data/pv-cache.js',
109+
{%- if site.google_analytics.pv.proxy_url and site.google_analytics.pv.enabled -%}
110+
'{{ site.google_analytics.pv.proxy_url }}',
111+
{%- endif -%}
112+
'/assets/js/data/pageviews.json',
110113
'/img.shields.io/'
111114
];

assets/js/data/pv-cache.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)