Replies: 4 comments 2 replies
-
Hello! I was able to code something that works, but IDK why, the page view counter on my own website is not working anymore! So, this is probably the next thing I'll be working on for this template, thanks for pointing this! |
Beta Was this translation helpful? Give feedback.
-
Hello @ranjith-b ! Here's how you can do it : 1/ First, remove the button part from the Waline Comment component, like this : 'use client'
import { useEffect } from 'react'
import { init } from '@waline/client'
import '@waline/client/style'
import siteMetadata from '@/config/siteMetadata'
import { LocaleTypes } from '@/app/[locale]/i18n/settings'
import { useParams } from 'next/navigation'
export default function Comments() {
const locale = useParams()?.locale as LocaleTypes
useEffect(() => {
init({
el: '#waline',
lang: locale,
reaction: true,
pageview: true,
serverURL: siteMetadata.walineServer,
emoji: [
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/weibo',
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/alus',
],
requiredMeta: ['nick'],
})
return () => {
// Cleanup if needed
}
}, [locale])
return (
<div className="mb-6 mt-6" id="waline" />
)
} 2/ Create an Eye icon like this : export function Eye(svgProps: SVGProps<SVGSVGElement>) {
return (
<svg
{...svgProps}
xmlns="http://www.w3.org/2000/svg"
height="18"
width="18"
fill="currentColor"
viewBox="0 0 576 512"
>
<path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM144 256a144 144 0 1 1 288 0 144 144 0 1 1 -288 0zm144-64c0 35.3-28.7 64-64 64c-7.1 0-13.9-1.2-20.3-3.3c-5.5-1.8-11.9 1.6-11.7 7.4c.3 6.9 1.3 13.8 3.2 20.7c13.7 51.2 66.4 81.6 117.6 67.9s81.6-66.4 67.9-117.6c-11.1-41.5-47.8-69.4-88.6-71.1c-5.8-.2-9.2 6.1-7.4 11.7c2.1 6.4 3.3 13.2 3.3 20.3z" />
</svg>
)
} 3/ Update your translations to include this : e.g: english in common.json file: "viewed": "Viewed", e.g: french in common.json file: "viewed": "Vu", 4/ Then, create a new generic component for the pages views, like this, where you'll import the Eye icon and use the new translations : PageView.tsx : 'use client'
import React, { useEffect } from 'react'
import { useParams } from 'next/navigation'
import { pageviewCount } from '@waline/client'
import siteMetadata from '@/config/siteMetadata'
import { LocaleTypes } from '@/app/[locale]/i18n/settings'
import { useTranslation } from '@/app/[locale]/i18n/client'
import { Eye } from './icons'
type PageviewProps = {
path: string
className?: string
}
export const Pageview = ({ path, className }: PageviewProps) => {
const locale = useParams()?.locale as LocaleTypes
const { t } = useTranslation(locale, 'common')
const handlePageviewDataFetch = async () => {
pageviewCount({
serverURL: siteMetadata.walineServer,
path: path,
})
}
useEffect(() => {
handlePageviewDataFetch()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [path])
return (
<div className={`${className} flex flex-row items-center`}>
<Eye className={'mr-2'} />
<p className="mr-2">{t('viewed')}</p>
<span className="waline-pageview-count mr-2" data-path={path} />
<p>{t('times')}</p>
</div>
)
} 5/ You can now use and import this new component in the layouts for the blog posts, like this: {process.env.NODE_ENV === 'production' && (
<Pageview className="" path={`/${locale}/blog/${slug}`} />)} Note: this is working for me, but only for english (don't know what to do for french, if I must re-deploy the whole waline server, since I made a lot of modifications for translation handling) Adapt the code and imports to your own needs, I took these for my own current implementation for my website. Explanation : the id of your page for views can only get retrieved if comments are loaded, so in order to see directly the number of page views, the logic for the button must be removed and the comment part must be directly mounted on the page! Hope this will be useful for you, let me know if you encounter any new issue! |
Beta Was this translation helpful? Give feedback.
-
Hi again! If you're in locale dev mode, I think it's normal that you get these errors. Have you tried to push the modifications? Also, double check you effectively defined your waline server in sitemetada.js (I don't think you forgot, but just in case :)) Edit : Also, be sure to update waline package to the last version! And since you're using de language, I think it's not supported yet by Waline (see there : https://waline.js.org/en/guide/features/i18n.html#set-language) but you can ask them to add de (by providing the translations yourself in a new issue, or by requesting a PR, this is what I did for french and they were very reactive) |
Beta Was this translation helpful? Give feedback.
-
You're welcome, happy to help. Thanks for following me on X! |
Beta Was this translation helpful? Give feedback.
-
Hi,
I have difficulty in the implementation of the Waline pageview counter into the project. Following the Waline documentation, I added
pageview: true
to the componentwalineComments.tsx
as follows:However, I could not see any page views on Articles/Posts. Could someone please help me understand what I'm missing? Do I need to add
Pageviews: <span class="waline-pageview-count" data-path="<Your/Path/Name>">
to PostBanner.tsx, PostLayout.tsx, and PostSimple.tsx?Thank you 🙏
Beta Was this translation helpful? Give feedback.
All reactions