Skip to content

Commit 829b593

Browse files
committed
Simplify the PV config options
- Remove options `site.google_analytics.pv.enabled` and `site.google_analytics.pv.proxy_url` - Rename options `site.google_analytics.pv.cache` to `site.google_analytics.pv.cache_path`
1 parent 4c5701d commit 829b593

File tree

9 files changed

+80
-125
lines changed

9 files changed

+80
-125
lines changed

_config.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ google_site_verification: google_meta_tag_verification # change to your verifica
5050
# --------------------------
5151

5252
google_analytics:
53-
id: '' # Fill with your Google Analytics ID
53+
id: '' # fill in your Google Analytics ID
54+
# Google Analytics pageviews report settings
5455
pv:
55-
# The Google Analytics pageviews switch.
56-
# DO NOT enable it unless you know how to deploy the Google Analytics superProxy.
57-
enabled: false
58-
# the next options only valid when `google_analytics.pv` is enabled.
59-
proxy_url: ''
60-
proxy_endpoint: ''
61-
cache: false # pv data local cache, good for the users from GFW area.
56+
proxy_endpoint: # fill in the Google Analytics superProxy endpoint of Google App Engine
57+
cache_path: # the local PV cache data, friendly to visitors from GFW region
6258

6359
# Prefer color scheme setting.
6460
#

_includes/head.html

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88

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

11-
{% if site.google_analytics.pv.enabled %}
12-
13-
{% if site.google_analytics.pv.proxy_endpoint != ''
14-
and site.google_analytics.pv.proxy_endpoint %}
15-
<meta name="pv-proxy-endpoint" content="{{ site.google_analytics.pv.proxy_endpoint }}">
16-
{% endif %}
17-
18-
{% if site.google_analytics.pv.cache %}
19-
<meta name="pv-cache-path" content="{{ '/assets/js/data/pageviews.json' | relative_url }}">
20-
{% endif %}
11+
{% if site.google_analytics.pv.proxy_endpoint %}
12+
<meta name="pv-proxy-endpoint" content="{{ site.google_analytics.pv.proxy_endpoint }}">
13+
{% endif %}
2114

15+
{% if site.google_analytics.pv.cache_path %}
16+
<meta name="pv-cache-path" content="{{ site.google_analytics.pv.cache_path | relative_url }}">
2217
{% endif %}
2318

2419
{% endif %}
@@ -46,9 +41,12 @@
4641
<link rel="preconnect" href="https://www.googletagmanager.com" crossorigin="anonymous">
4742
<link rel="dns-prefetch" href="https://www.googletagmanager.com">
4843

49-
{% if site.google_analytics.pv.proxy_url and site.google_analytics.pv.enabled %}
50-
<link rel="preconnect" href="{{ site.google_analytics.pv.proxy_url }}" crossorigin="use-credentials">
51-
<link rel="dns-prefetch" href="{{ site.google_analytics.pv.proxy_url }}">
44+
{% if site.google_analytics.pv.proxy_endpoint %}
45+
{% assign proxy_url = site.google_analytics.pv.proxy_endpoint
46+
| replace: "https://", "" | split: "/" | first | prepend: "https://" %}
47+
48+
<link rel="preconnect" href="{{ proxy_url }}" crossorigin="use-credentials">
49+
<link rel="dns-prefetch" href="{{ proxy_url }}">
5250
{% endif %}
5351
{% endif %}
5452

_includes/js-selector.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-->
44

55
{% if page.layout == 'home' or page.layout == 'post' %}
6-
{% if site.google_analytics.pv.enabled %}
6+
{% if site.google_analytics.pv.proxy_endpoint or site.google_analytics.pv.cache_path %}
77
<!-- pv-report needs countup.js -->
88
<script async src="https://cdn.jsdelivr.net/npm/countup.js@1.9.3/dist/countUp.min.js"></script>
99
<script async src="{{ '/assets/js/dist/pvreport.min.js' | relative_url }}"></script>

_layouts/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ <h1>
6868
{% include read-time.html content=post.content %}
6969

7070
<!-- page views -->
71-
{% if site.google_analytics.pv.enabled %}
71+
{% if site.google_analytics.pv.proxy_endpoint or site.google_analytics.pv.cache_path %}
7272
<i class="far fa-eye fa-fw"></i>
7373
<span id="pv_{{-post.title-}}" class="pageviews">
7474
<i class="fas fa-spinner fa-spin fa-fw"></i>

_layouts/post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h1 data-toc-skip>{{ page.title }}</h1>
3333
{% include read-time.html content=content %}
3434

3535
<!-- page views -->
36-
{% if site.google_analytics.pv.enabled %}
36+
{% if site.google_analytics.pv.proxy_endpoint or site.google_analytics.pv.cache_path %}
3737
<span id="pv" class="pageviews"><i class="fas fa-spinner fa-spin fa-fw"></i></span>
3838
{% endif %}
3939

_posts/2021-01-03-enable-google-pv.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tags: [google analytics, pageviews]
77
---
88

99

10-
This post is to enable Page Views on the [**Chirpy**][chirpy-homepage] theme based blog that you just built. This requires technical knowledge and it's recommended to keep the `google_analytics.pv` disabled unless you have a good reason. If your website has low traffic, the page views count would discourage you to write more blogs. With that said, let's start with the setup.
10+
This post is to enable Page Views on the [**Chirpy**][chirpy-homepage] theme based blog that you just built. This requires technical knowledge and it's recommended to keep the `google_analytics.pv.*` empty unless you have a good reason. If your website has low traffic, the page views count would discourage you to write more blogs. With that said, let's start with the setup.
1111

1212
## Set up Google Analytics
1313

@@ -39,14 +39,11 @@ Now, click on the new data stream and grab the **Measurement ID**. It should loo
3939

4040
```yaml
4141
google_analytics:
42-
id: 'G-V6XXXXXXX' # Fill with your Google Analytics ID
42+
id: 'G-V6XXXXXXX' # fill in your Google Analytics ID
43+
# Google Analytics pageviews report settings
4344
pv:
44-
# The Google Analytics pageviews switch.
45-
enabled: false
46-
# the next options only valid when `google_analytics.pv` is enabled.
47-
proxy_url: ''
48-
proxy_endpoint: ''
49-
cache: false # pv data local cache, good for the users from GFW area.
45+
proxy_endpoint: # fill in the Google Analytics superProxy endpoint of Google App Engine
46+
cache_path: # the local PV cache data, friendly to visitors from GFW region
5047
```
5148
5249
When you push these changes to your blog, you should start seeing the traffic on your Google Analytics. Play around with Google Analytics dashboard to get familiar with the options available as it takes like 5 mins to pickup your changes. You should now be able to monitor your traffic in realtime.
@@ -222,17 +219,12 @@ Once all the hard part is done, it is very easy to enable the Page View on Chirp
222219

223220
Update the `_config.yml` file of [**Chirpy**][chirpy-homepage] project with the values from your dashboard, to look similar to the following:
224221

225-
226222
```yaml
227223
google_analytics:
228-
id: 'G-XXXXXXXXXX' # Fill with your Google Analytics ID
224+
id: 'G-V6XXXXXXX' # fill in your Google Analytics ID
229225
pv:
230-
# The Google Analytics pageviews switch.
231-
enabled: true
232-
# the next options only valid when `google_analytics.pv` is enabled.
233-
proxy_url: 'https://PROJECT_ID.REGION_ID.r.appspot.com'
234226
proxy_endpoint: 'https://PROJECT_ID.REGION_ID.r.appspot.com/query?id=<ID FROM SUPER PROXY>'
235-
cache: false # pv data local cache, good for the users from GFW area.
227+
cache_path: # the local PV cache data, friendly to visitors from GFW region
236228
```
237229

238230
Now, you should see the Page View enabled on your blog.

assets/js/_utils/pageviews.js

Lines changed: 50 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,30 @@ const getInitStatus = (function () {
1818
}());
1919

2020
const PvOpts = (function () {
21+
function hasContent(selector) {
22+
let content = $(selector).attr("content");
23+
return (typeof content !== "undefined" && content !== false);
24+
}
25+
2126
return {
2227
getProxyEndpoint() {
2328
return $("meta[name=pv-proxy-endpoint]").attr("content");
2429
},
2530
getLocalData() {
2631
return $("meta[name=pv-cache-path]").attr("content");
2732
},
33+
hasProxyEndpoint() {
34+
return hasContent("meta[name=pv-proxy-endpoint]");
35+
},
2836
hasLocalData() {
29-
let path = PvOpts.getLocalData();
30-
return (typeof path !== "undefined" && path !== false);
37+
return hasContent("meta[name=pv-cache-path]");
3138
}
3239
}
3340
}());
3441

35-
const PvData = (function () {
42+
const PvStorage = (function () {
3643
const KEY_PV = "pv";
3744
const KEY_CREATION = "pv_created_date";
38-
const KEY_PV_SRC = "pv_source";
39-
40-
const Source = {
41-
ORIGIN: "origin",
42-
PROXY: "proxy"
43-
};
4445

4546
function get(key) {
4647
return localStorage.getItem(key);
@@ -51,48 +52,30 @@ const PvData = (function () {
5152
}
5253

5354
return {
54-
getData() {
55+
hasCache() {
56+
return (localStorage.getItem(KEY_PV) !== null);
57+
},
58+
getCache() {
5559
// get data from browser cache
5660
return JSON.parse(localStorage.getItem(KEY_PV));
5761
},
58-
saveOriginCache(pv) {
59-
set(KEY_PV, pv);
60-
set(KEY_PV_SRC, Source.ORIGIN);
61-
set(KEY_CREATION, new Date().toJSON());
62-
},
63-
saveProxyCache(pv) {
62+
saveCache(pv) {
6463
set(KEY_PV, pv);
65-
set(KEY_PV_SRC, Source.PROXY);
6664
set(KEY_CREATION, new Date().toJSON());
6765
},
68-
isFromOrigin() {
69-
return get(KEY_PV_SRC) === Source.ORIGIN;
70-
},
71-
isFromProxy() {
72-
return get(KEY_PV_SRC) === Source.PROXY;
73-
},
7466
isExpired() {
75-
if (PvData.isFromOrigin()) {
76-
let date = new Date(get(KEY_CREATION));
77-
date.setDate(date.getDate() + 1); /* update origin records every day */
78-
return Date.now() >= date.getTime();
79-
80-
} else if (PvData.isFromProxy()) {
81-
let date = new Date(get(KEY_CREATION));
82-
date.setHours(date.getHours() + 1); /* update proxy records per hour */
83-
return Date.now() >= date.getTime();
84-
}
85-
return false;
67+
let date = new Date(get(KEY_CREATION));
68+
date.setHours(date.getHours() + 1); // per hour
69+
return Date.now() >= date.getTime();
8670
},
8771
getAllPageviews() {
88-
return PvData.getData().totalsForAllResults["ga:pageviews"];
72+
return PvStorage.getCache().totalsForAllResults["ga:pageviews"];
8973
},
9074
newerThan(pv) {
91-
return PvData.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
75+
return PvStorage.getAllPageviews() > pv.totalsForAllResults["ga:pageviews"];
9276
},
9377
inspectKeys() {
9478
if (localStorage.getItem(KEY_PV) === null
95-
|| localStorage.getItem(KEY_PV_SRC) === null
9679
|| localStorage.getItem(KEY_CREATION) === null) {
9780
localStorage.clear();
9881
}
@@ -101,7 +84,6 @@ const PvData = (function () {
10184

10285
}()); /* PvData */
10386

104-
10587
function countUp(min, max, destId) {
10688
if (min < max) {
10789
let numAnim = new CountUp(destId, min, max);
@@ -113,7 +95,6 @@ function countUp(min, max, destId) {
11395
}
11496
}
11597

116-
11798
function countPV(path, rows) {
11899
let count = 0;
119100

@@ -130,7 +111,6 @@ function countPV(path, rows) {
130111
return count;
131112
}
132113

133-
134114
function tacklePV(rows, path, elem, hasInit) {
135115
let count = countPV(path, rows);
136116
count = (count === 0 ? 1 : count);
@@ -145,7 +125,6 @@ function tacklePV(rows, path, elem, hasInit) {
145125
}
146126
}
147127

148-
149128
function displayPageviews(data) {
150129
if (typeof data === "undefined") {
151130
return;
@@ -166,69 +145,59 @@ function displayPageviews(data) {
166145
}
167146
}
168147

169-
170148
function fetchProxyPageviews() {
171-
$.ajax({
172-
type: "GET",
173-
url: PvOpts.getProxyEndpoint(),
174-
dataType: "jsonp",
175-
jsonpCallback: "displayPageviews",
176-
success: (data, textStatus, jqXHR) => {
177-
PvData.saveProxyCache(JSON.stringify(data));
178-
},
179-
error: (jqXHR, textStatus, errorThrown) => {
180-
console.log("Failed to load pageviews from proxy server: " + errorThrown);
181-
}
182-
});
149+
if (PvOpts.hasProxyEndpoint()) {
150+
$.ajax({
151+
type: "GET",
152+
url: PvOpts.getProxyEndpoint(),
153+
dataType: "jsonp",
154+
jsonpCallback: "displayPageviews",
155+
success: (data, textStatus, jqXHR) => {
156+
PvStorage.saveCache(JSON.stringify(data));
157+
},
158+
error: (jqXHR, textStatus, errorThrown) => {
159+
console.log("Failed to load pageviews from proxy server: " + errorThrown);
160+
}
161+
});
162+
}
183163
}
184164

185-
186-
function fetchPageviews(fetchOrigin = true, coverOrigin = false) {
187-
if (fetchOrigin) {
165+
function loadPageviews(hasCache = false) {
166+
if (PvOpts.hasLocalData()) {
188167
fetch(PvOpts.getLocalData())
189168
.then((response) => response.json())
190169
.then((data) => {
191-
if (coverOrigin) {
192-
if (PvData.newerThan(data)) {
193-
return;
194-
}
170+
// The cache from the proxy will sometimes be more recent than the local one
171+
if (hasCache && PvStorage.newerThan(data)) {
172+
return;
195173
}
196174
displayPageviews(data);
197-
PvData.saveOriginCache(JSON.stringify(data));
175+
PvStorage.saveCache(JSON.stringify(data));
198176
})
199-
.then(() => fetchProxyPageviews());
177+
.then(() => {
178+
fetchProxyPageviews();
179+
});
200180

201181
} else {
202182
fetchProxyPageviews();
203183
}
204184

205185
}
206186

207-
208187
$(function() {
209188
if ($(".pageviews").length <= 0) {
210189
return;
211190
}
212191

213-
PvData.inspectKeys();
214-
let data = PvData.getData();
215-
216-
if (data) {
217-
displayPageviews(data);
218-
219-
if (PvData.isExpired()) {
220-
fetchPageviews(true, PvData.isFromProxy());
221-
222-
} else {
223-
224-
if (PvData.isFromOrigin()) {
225-
fetchPageviews(false);
226-
}
192+
PvStorage.inspectKeys();
227193

194+
if (PvStorage.hasCache()) {
195+
displayPageviews(PvStorage.getCache());
196+
if (!PvStorage.isExpired()) {
197+
return;
228198
}
229-
230-
} else {
231-
fetchPageviews(PvOpts.hasLocalData());
232199
}
233200

201+
loadPageviews(PvStorage.hasCache());
202+
234203
});

assets/js/data/cache-list.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ const include = [
5353
];
5454

5555
const exclude = [
56-
{%- if site.google_analytics.pv.proxy_url and site.google_analytics.pv.enabled -%}
57-
'{{ site.google_analytics.pv.proxy_url }}',
56+
{%- if site.google_analytics.pv.proxy_endpoint -%}
57+
'https://{{ site.google_analytics.pv.proxy_endpoint | replace: "https://", "" | split: "/" | first }}',
5858
{%- endif -%}
59-
'/assets/js/data/pageviews.json',
60-
'/img.shields.io/'
59+
'https://img.shields.io',
60+
'/assets/js/data/pageviews.json'
6161
];

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)