From 5d637ee7b7a9a80a7b02ae2dcf1c98507d0f10a6 Mon Sep 17 00:00:00 2001
From: Cory Dransfeldt <cory.dransfeldt@gmail.com>
Date: Sat, 26 Aug 2023 19:42:28 -0700
Subject: [PATCH] chore: restore popular posts

---
 .env                      |  3 ++-
 .vscode/settings.json     |  3 +++
 config/filters.js         |  4 ++--
 src/404.html              |  2 ++
 src/_data/analytics.js    | 14 ++++++++++++++
 src/_includes/post.liquid |  3 ++-
 src/search.html           |  3 ++-
 7 files changed, 27 insertions(+), 5 deletions(-)
 create mode 100644 .vscode/settings.json
 create mode 100644 src/_data/analytics.js

diff --git a/.env b/.env
index c5481b3b..250e80e3 100644
--- a/.env
+++ b/.env
@@ -1,7 +1,8 @@
 API_KEY_LASTFM=
-API_KEY_FATHOM=
 API_KEY_TRAKT=
 API_KEY_WEBMENTIONS_CORYD_DEV=
 API_TOKEN_READWISE=
+SITE_ID_CLICKY=
+SITE_KEY_CLICKY=
 SECRET_FEED_ALBUM_RELEASES=
 SECRET_FEED_GOODREADS=
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 00000000..0db6bb9a
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+  "liquid.format.enable": false
+}
diff --git a/config/filters.js b/config/filters.js
index 16c44ff1..8784d0ce 100644
--- a/config/filters.js
+++ b/config/filters.js
@@ -28,10 +28,10 @@ module.exports = {
   getPopularPosts: (posts, analytics) => {
     return posts
       .filter((post) => {
-        if (analytics.find((p) => p.pathname === post.url)) return true
+        if (analytics.find((p) => p.url.includes(post.url))) return true
       })
       .sort((a, b) => {
-        const visitors = (page) => analytics.filter((p) => p.pathname === page.url).pop().visitors
+        const visitors = (page) => analytics.filter((p) => p.url.includes(page.url)).pop().value
         return visitors(b) - visitors(a)
       })
   },
diff --git a/src/404.html b/src/404.html
index 747673c2..a3e5dec1 100644
--- a/src/404.html
+++ b/src/404.html
@@ -18,3 +18,5 @@ permalink: 404.html
     </a>
   </div>
 </div>
+<hr />
+{% render "partials/popular-posts.liquid", posts: collections.posts, analytics: analytics %}
\ No newline at end of file
diff --git a/src/_data/analytics.js b/src/_data/analytics.js
new file mode 100644
index 00000000..d97d911c
--- /dev/null
+++ b/src/_data/analytics.js
@@ -0,0 +1,14 @@
+const EleventyFetch = require('@11ty/eleventy-fetch')
+
+module.exports = async function () {
+  const SITE_ID_CLICKY = process.env.SITE_ID_CLICKY
+  const SITE_KEY_CLICKY = process.env.SITE_KEY_CLICKY
+  const url = `https://api.clicky.com/api/stats/4?site_id=${SITE_ID_CLICKY}&sitekey=${SITE_KEY_CLICKY}&type=pages&output=json`
+  const res = EleventyFetch(url, {
+    duration: '1h',
+    type: 'json',
+  }).catch()
+  const data = await res
+  const pages = data[0].dates[0].items
+  return pages.filter((p) => p.url.includes('posts'))
+}
diff --git a/src/_includes/post.liquid b/src/_includes/post.liquid
index 14a73d18..677a57a9 100644
--- a/src/_includes/post.liquid
+++ b/src/_includes/post.liquid
@@ -13,4 +13,5 @@ layout: main
 </article>
 {% render "partials/post-tags.liquid", tags: tags %}
 {% render "partials/webmentions/container.liquid", webmentions: webmentions, page: page %}
-{% render "partials/author.liquid", site: site %}
\ No newline at end of file
+{% render "partials/author.liquid", site: site %}
+{% render "partials/popular-posts.liquid", posts: collections.posts, analytics: analytics %}
\ No newline at end of file
diff --git a/src/search.html b/src/search.html
index b413e6cd..9b6eccf3 100644
--- a/src/search.html
+++ b/src/search.html
@@ -35,4 +35,5 @@ meta:
   }
 </style>
 <div id="search" class="search"></div>
-<script src="https://coryd.dev/_pagefind/pagefind-ui.js" onload="new PagefindUI({ element: '#search', showImages: false, processTerm: (term) => {clicky.log('/search', term, 'pageview');} });"></script>
\ No newline at end of file
+<script src="https://coryd.dev/_pagefind/pagefind-ui.js" onload="new PagefindUI({ element: '#search', showImages: false, processTerm: (term) => {clicky.log('/search', term, 'pageview');} });"></script>
+{% render "partials/popular-posts.liquid", posts: collections.posts, analytics: analytics %}
\ No newline at end of file