Alter page field from the front-end

Hey guys,

I’ve recently built my portfolio site and I want to build a downloads section for design resources. I want to show a download count on each resource page, and I assume it shouldn’t be very difficult to build something like this, e.g. when you hit “Download”, the downloadsCount field increases by 1.

Where should I look for stuff like this? I checked the field methods section of the Docs, but didn’t find anything.

Thanks!

The $page->increment() method is what your are looking for: https://getkirby.com/docs/cheatsheet/page/increment

If you have heavy traffic, and downloads that are even fractions of seconds apart – or even sometimes a few seconds apart – you might lose some increments. The scenario is:

  1. Script execution #1 reads the content file, gets the value “15”.
  2. Script execution #2 reads the content file, gets the value “15”.
  3. Script execution #1 writes “16” to the content file.
  4. Script execution #2 writes “16” to the content file.

You get one increment instead of two. The way to avoid this, if you think it might be an issue, is to use a database and store a new entry for each increment, then count the number of rows. You could have your own database (separate from Kirby, though Kirby offers some methods for working with databases), or rely on a third party service like Google Analytics or Piwik (self-hosted) to track user actions (basically you add an event listener to a button/link and tell your Analytics script to record some text information like a category, URL, and description string).

If it’s a small site with few visitors, you probably don’t need this.
But if you do have an analytics solution on the website, it’s probably better to save this information in this one place rather than saving some records in the analytics app and other in Kirby pages.

1 Like

Ah thank you very much guys! Gonna give it a try!