Views counter

I would like to have built-in views counter for individuals notes: how many times a note has been viewed. Similar to the view counter of each thread in this forum.

A workaround is to manually increase a variable named e.g. view_counter in the YAML front matter, however it would be nicer if Obsidian supports this out of the box.

6 Likes

I really like the frontmatter idea as a plugin. And it may be straightforward enough for me to tinker with as a first plugin (and a good sandbox as I’d like to build some more things involving editing frontmatter). I’ll let you know if I come up with anything.

(Don’t let this stop other developers. If you have interest in working on this, assume I’m not. I make no guarantees as to the time I’ll be able to put forward on this :slight_smile: )

2 Likes

it would also be great if you consider support for the variable rating in YAML front matter as well :grinning:

I’ve opened an issue on the Buttons plugin github to add YAML increment/decrement as a button action. It’s likely that automatic triggering of these button actions is outside the scope of that plugin, but if the YAML integration is implemented, it would at least give the option to click a button when you load a note to add to the counter.

There are sooo many features that could benefit from/would depend on robust CRUD control of frontmatter. I wonder if a single frontmatter-CRUD plugin would be helpful as a sort of frontmatter API that other plugins could take advantage of. I suppose ideally this would be implemented in the official Obsidian plugin API.

Just thinking out loud :slight_smile:

As for rating, I saw somewhere (can’t find it now) the idea of using tags to rate a note, e.g.: #★, #★★, #★★★. And I see your discussion here about ratings. What are you looking for in a YAML rating variable setup?

1 Like

I’d like to use rating as a way to record the usefulness of the notes to me. For example, I was looking for some info and found it in a note that helped me to refresh the info very efficiently. Then I would increase the rating for that note. Similar to rating at online shops, but only for myself.

Another way to do that could be something like facebook “likes”: how many times I read and found a note useful. Combined with the views_counter this could be more helpful than rating.

It does seem like Buttons would be a great option for this use case; if not now, then perhaps eventually. The developer pointed me to the MetaEdit plugin, also, which could be used independently or perhaps as the action that is triggered by Buttons.

I haven’t given this workflow a try yet, though, so it’s possible it’s not as seamless as one would like, or at this stage faster than manually editing the frontmatter.

I was able to get a counter working with Templater plugin (and a bunch of 1st time badly written JS) and Buttons plugin but it does not refresh the preview page when clicked. You have to toggle back to edit to see that it was changed and then back to preview to see it in preview mode. Probably my lack of skill with the API and JS. Now if you are already in edit mode, using a hotkey against the Templater script works nicely. If you want me to share it (and forgive me for my JS skills) I’d be happy to.

Can someone please show me how they got this working? Keen to add a view metric in my YAML frontmatter. I can read JS but don’t know how to setup the MetaEdit/ Templater/ Buttons plugins

This is a little bit late, but hope this can help :grinning:

  1. Install Templater plugin.
  2. Create a folder call “script” (or whatever name you prefer)
  3. Add this folder to Templater → UserScript Function
  4. Create a file call counter.js or name you prefer but xxx.js.
  5. Paste this below code:
let pageSet = new Set()
let initPage = true

const openFileListener = (callback) => {
    return (counter_name) => {app.workspace.on("active-leaf-change", async () => {
    	newSet = new Set()
    	app.workspace.iterateRootLeaves((rootLeaf) =>{
    		if (rootLeaf.getViewState().type != "markdown"){
    			return
    		}
    		newSet.add(rootLeaf.getViewState().state.file)
    	})
		if (initPage){
			pageSet = newSet
			initPage = false
			return
		}
    	const difference = new Set([...newSet].filter(x => !pageSet.has(x)));
		pageSet = newSet
		difference.forEach(item => {
			callback(counter_name, item)
		})
    })}
}

const eventRef = openFileListener(async (counter_name, file_name) => {
	const file = app.vault.getAbstractFileByPath(file_name)
	try{
		await app.fileManager.processFrontMatter(file, (frontmatter)=>{
            if (typeof frontmatter[counter_name] === 'number') {
                frontmatter[counter_name] += 1;
                console.log(`Counter: ${file_name} view counts: ${frontmatter[counter_name]}`)
            } else {
                console.log(`Counter: No property ${counter_name} on Page: ${file_name}`)
            }
		})
	}catch(error){
		console.error(`Error processing front matter: ${error}`);
	}
});

module.exports = eventRef;
  1. If you dont have a startup template: Create one and add to Templater setting → Startup Templates
  2. In your start up template add
 <% tp.user.counter("Counter") %>  

(Replace counter with your the file name(without.js); replace "Counter" to you desired counter display name: “View Count”, “Counts” or whatever

  1. Restart.

This will add 1 to your page counter every time you open a note that is not exist in your current opened file. If no Counter property exists, it does nothing. If you have a property that match the name you provided, it will add one to the value.

I have made a plugin that accommodates the functionality that you are looking for. It is called the Obsidian “View Count” plugin in the community plugin store. Please check it out.

Obsidian plugins - View count plugin

Here is the GitHub repository as well

Cheers! :smiley: