Skip to main content

Why are counter field values not updated?

Counter fields store values separately from content data for performance optimization. The displayed value and the sort value may diverge until they are synchronized.

How counter fields work

Counter fields are designed for values that are frequently updated, such as access counts or like counts. To improve performance, Kuroco separates counter values from the content itself and updates them asynchronously.

Specifically, counter values are managed in two locations:

  1. Counter table: The latest counter value. Updated immediately when a counter operation occurs.
  2. Content table: A cached copy of the counter value stored alongside the content data. Used for display and sorting in API responses.

When do values become separated?

The values in these two locations diverge whenever the counter is updated. This happens in the following cases:

  • When the {increment_counter} or {update_counter} Smarty plugins are used: These plugins update the counter table directly without synchronization.
  • When the {googleanalytics} Smarty plugin updates access counts: The plugin writes new values to the counter table, but the content table is not updated at that point.

Because the sort value used in API list responses is read from the content table, sorting by a counter field may produce unexpected results until the values are synchronized.

How to synchronize counter values

Updating from the content editing screen

In the content editing screen, you can synchronize the counter by checking the Update counter checkbox and saving. This updates both the counter table and the content table at the same time.

Updating via Topics::increment

When a counter is incremented via the Topics::increment endpoint, the counter table is updated immediately and a sync_counter batch process is scheduled to synchronize the content table.
This batch reads the latest values from the counter table and writes them back to the content table. The content table update is temporarily delayed, but this is resolved once the batch completes.

Manual synchronization using the sync_counter Smarty plugin

You can manually trigger synchronization using the {sync_counter} Smarty plugin. This is useful when you want to synchronize values at any point after using {increment_counter} or {update_counter}.

{sync_counter ids="1,2,3"}

Synchronization via batch processing with googleanalytics

In the {googleanalytics} Smarty plugin example, counter values are synchronized by calling the sync_counter batch after the googleanalytics plugin in the same batch process:

{googleanalytics
var="result"
update_column_slug="pv"
update_target_dimension="customEvent:slug"
updated_topics_ids='updated_topics_ids'
topics_group_id=1}

{assign_array var=ext_data values=''}
{assign var=ext_data.topics_ids value=$updated_topics_ids}
{batch module='topics' name='sync_counter' ext_data=$ext_data}

Alternative: Use a number field instead of a counter field

If you need the value to always be in sync with the content and do not require asynchronous high-frequency updates, consider using a number field instead of a counter field. Number fields store values directly in the content table, so there is no separation between the displayed value and the sort value.


Support

If you have any other questions, please contact us or check out Our Slack Community.