From ab0e3f6ee485d83f730fde37f56f964e9954f24c Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Mon, 18 Jan 2021 23:25:09 +0800 Subject: [PATCH] Fix PV display when baseurl exists --- _posts/2021-01-03-enable-google-pv.md | 18 ++++++++++-------- assets/js/_pv-config.js | 2 ++ assets/js/_utils/pageviews.js | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/_posts/2021-01-03-enable-google-pv.md b/_posts/2021-01-03-enable-google-pv.md index 8b772c1..0089ca5 100644 --- a/_posts/2021-01-03-enable-google-pv.md +++ b/_posts/2021-01-03-enable-google-pv.md @@ -7,7 +7,7 @@ tags: [google analytics, pageviews] --- -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 you website has low traffic, the page views count would discourage you to write more blogs. With that said, let's start with the setup. +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. ## Set up Google Analytics @@ -181,7 +181,7 @@ There is a detailed [tutorial](https://developers.google.com/analytics/solutions 7. Click on **Authorize Users** and make sure to add yourself as a managed user. -8. If you get any errors, please google it. The errors are self-explanatory and should be easy to fix. +8. If you get any errors, please Google it. The errors are self-explanatory and should be easy to fix. If everything went good, you'll get this screen: @@ -193,19 +193,21 @@ Head to `https://PROJECT_ID.REGION_ID.r.appspot.com/admin` and create a query af The query parameters are as follows: -- `start-date` - fill in the first day of blog posting +- **start-date**: fill in the first day of blog posting -- `end-date` - fill in `today` (this is a parameter supported by GA Report, which means that it will always end according to the current query date) +- **end-date**: fill in `today` (this is a parameter supported by GA Report, which means that it will always end according to the current query date) -- `metrics` - select `ga:pageviews` +- **metrics**: select `ga:pageviews` -- `dimensions` - select `ga:pagePath` +- **dimensions**: select `ga:pagePath` In order to reduce the returned results and reduce the network bandwidth, we add custom filtering rules [^ga-filters]: -- `filters` - fill in `ga:pagePath=~^/posts/.*/$;ga:pagePath!@=` +- **filters**: fill in `ga:pagePath=~^/posts/.*/$;ga:pagePath!@=` - Among them, `;` means using _logical AND_ to concatenate two rules, `!@=` means excluding `=`. + Among them, `;` means using _logical AND_ to concatenate two rules. + + If the `site.baseurl` is specified, change the first filtering rule to `ga:pagePath=~^/BASE_URL/posts/.*/$`, where `BASE_URL` is the value of `site.baseurl`. After Run Query, copy the generated contents of **API Query URI** at the bottom of the page, and fill in the **Encoded URI for the query** of SuperProxy on GAE. diff --git a/assets/js/_pv-config.js b/assets/js/_pv-config.js index 29acbbe..f560273 100644 --- a/assets/js/_pv-config.js +++ b/assets/js/_pv-config.js @@ -12,3 +12,5 @@ const proxyEndpoint = "{{ site.google_analytics.pv.proxy_endpoint }}"; {% endif %} const pvCacheEnabled = {{ enabled }}; + +const pvCacheData = "{{ '/assets/js/data/pageviews.json' | relative_url }}"; diff --git a/assets/js/_utils/pageviews.js b/assets/js/_utils/pageviews.js index 34038f0..5cb0201 100644 --- a/assets/js/_utils/pageviews.js +++ b/assets/js/_utils/pageviews.js @@ -173,9 +173,9 @@ function fetchProxyPageviews() { function fetchPageviews(fetchOrigin = true, filterOrigin = false) { - /* pvCacheEnabled › see: /assets/js/_pv-config.js */ + /* pvCacheEnabled, pvCacheData › see: /assets/js/_pv-config.js */ if (pvCacheEnabled && fetchOrigin) { - fetch("/assets/js/data/pageviews.json") + fetch(pvCacheData) .then((response) => response.json()) .then((data) => { if (filterOrigin) {