The number of views for a particular content is generally calculated based on the number of pages viewed.
The parameters required for adding statistical code into the template of the content details page are usually placed here.
Before ending the tag, make sure to count the number of code calls before proceeding.<script>
var _xy_cms = { siteId: '${Site.siteId}', catalogId: '${Catalog.catalogId}', contentId: '${Content.contentId}' }
</script>
Add the statistical code to the page:
<script type="text/javascript">
(function () {
var q = "sid=" + _xy_cms.siteId + "&cid=" + _xy_cms.catalogId + "&id=" +_xy_cms.contentId;
var s = document.createElement("script");
s.src = '${ApiPrefix}api/stat/visit?' + q.join('&');
(document.getElementsByTagName("head")[0] ||
document.getElementsByTagName("body")[0]).appendChild(s);
})();
</script>
Add tasks for dynamic data statistics and updates to the backend scheduled tasks.
The statistical data is stored in Redis by default, and is synchronized to the database through scheduled tasks.
Open the backend menu System Monitoring > Scheduled Tasks Add scheduled tasks:Save dynamic content data tasksGenerally, it is sufficient to set the interval to once every 10 minutes.
$.ajax({
type: "GET",
url: _xy_cms.api_prefix + "api/cms/content/data",
dataType: 'json',
data: {
ids: "123,456,789" // Separate multiple content IDs with commas
},
success: function (res) {
if (res.code == 200) {
// res.data represents a list of dynamic content data.
// Format: [{"contentId": "Content ID", "favorites": "Number of favorites", "likes": "Number of likes", "views": "Number of views", "comments": "Number of comments", "contributorId": "Member ID of the contributor"}, ...]
}
}
})
Response data format:
[
{
"contentId": "Content ID",
"favorites": "Bookmark count",
"likes": "Like count",
"views": "View count",
"comments": "Comment count",
"contributorId": "Contributing member ID",
"contributor": {
"uid": "Member user ID",
"displayName": "Display name",
"avatar": "Avatar",
"avatarSrc": "Avatar preview URL",
"slogan": "Profile tagline",
"stat": { // Member statistics
"comment": "Comment count",
"contribute": "Submission count",
"follow": "Following count",
"follower": "Follower count",
}
}
},
...
]