Used for counting page visit data, including document visit statistics and other access records, supporting various record types and access path statistics.
Precautions
- This interface uses the GET method
- Used to count page visit volume and record visit data
- Supports document visit volume statistics and other types of visit records
- The HTML template will automatically inject this API call by default
- If using Next.js or other frontend frameworks, you need to manually call this interface
- HTTP status code and access path parameters must be provided
Request address
{域名地址}/api/log
Description:{域名地址}Need to replace it with your domain address, such ashttps://en.anqicms.com/api/log
Request syntax
GET {域名地址}/api/log
Request Parameters
| Field Name | Type | Required | Description |
|---|---|---|---|
| action | string | No | Record type, supported values: views: Document access, counts the number of document accesses, other: Other values |
| id | integer | No | Document ID |
| type | string | No | Record type, supported values: archive when action=views is needed |
| code | integer | Yes | HTTP status code |
| Path | string | Yes | Access path |
Return parameters
None
Example Usage
Document Access Statistics Request Example
GET /api/log?action=views&id=1&type=archive&code=200&path=/article/1.html HTTP/1.1
Host: www.anqicms.com
Other Type Access Record Request Example
GET /api/log?action=other&code=404&path=/nonexistent-page.html HTTP/1.1
Host: www.anqicms.com
500 Error Page Statistics Request Example
GET /api/log?action=other&code=500&path=/error-page HTTP/1.1
Host: www.anqicms.com
Front-end JavaScript Call Example
// 统计文档访问
function trackPageView(articleId, path) {
const url = `/api/log?action=views&id=${articleId}&type=archive&code=200&path=${encodeURIComponent(path)}`;
// 使用fetch发送请求,不等待响应
fetch(url, { method: 'GET' })
.catch(err => console.error('统计请求失败:', err));
}
// 统计其他页面访问
function trackOtherPage(path, statusCode = 200) {
const url = `/api/log?action=other&code=${statusCode}&path=${encodeURIComponent(path)}`;
// 使用图片方式发送请求,避免CORS问题
new Image().src = url;
}
// 页面加载完成后统计访问
document.addEventListener('DOMContentLoaded', function() {
const currentPath = window.location.pathname;
const articleId = getArticleIdFromPath(currentPath); // 自定义函数获取文章ID
if (articleId) {
trackPageView(articleId, currentPath);
} else {
trackOtherPage(currentPath);
}
});
Example in Next.js
import { useEffect } from 'react';
import { useRouter } from 'next/router';
export default function LogTracker() {
const router = useRouter();
useEffect(() => {
const trackPageView = () => {
const path = router.asPath;
const articleId = extractArticleId(path); // 自定义函数提取文章ID
let logUrl = `/api/log?code=200&path=${encodeURIComponent(path)}`;
if (articleId) {
logUrl += `&action=views&id=${articleId}&type=archive`;
} else {
logUrl += '&action=other';
}
// 发送统计请求
fetch(logUrl).catch(err => console.error('统计请求失败:', err));
};
// 路由变化时统计
trackPageView();
router.events.on('routeChangeComplete', trackPageView);
return () => {
router.events.off('routeChangeComplete', trackPageView);
};
}, [router]);
}