Fine-tuned operation: How can Anqi CMS customize statistical code for a specific page?
In daily website operations, we often encounter such needs: not all pages need to load the same statistical code.Perhaps it is to track the conversion of a promotional activity more accurately, or to conduct A/B testing on the user behavior of a specific product page, or to load specific third-party service code on some important pages.If all the code is put all at once at the top or bottom of the entire site, it may not only cause waste of resources, affect the page loading speed, but also make data analysis chaotic.
How, as a content management system known for its efficiency and customization, AnQiCMS (AnQiCMS) helps us implement this fine-grained page-level statistical code insertion?Don't worry, it's not complicated, the flexible template mechanism and content management functions of Anqi CMS provide us with a clear solution.
Farewell to the universal site, embrace the exclusive page
First, let's think about why it is not recommended to load the entire site.In addition to the loading speed and data redundancy issues mentioned earlier, there are also considerations in terms of security and compliance (such as the limitations of GDPR or domestic regulations on the collection of specific data).pluginJsCodeLabel), but these are more for macro or specific types of global needs.For the personalized code implantation we want to achieve on the 'specific page', we need to delve deeper into the template level.
The design philosophy of Anqi CMS is one of 'flexible content model' and 'modular design', which is fully reflected in template processing.It allows us to apply different front-end templates to different content types even specific content items.This is the key to implementing page-level code customization.
Method one: tailor-made templates for independent pages (articles, products, single pages)
The AnQi CMS provides powerful flexibility in content management, whether it is publishing articles, products, or creating independent single-page applications, you can specify a dedicated template file for them.This means, you can create a unique template for pages that require specific statistical code.
The entire process can be operated in this way:
Create or copy a dedicated template file: Generally, the template files of AnQi CMS are located in
/template/你的模板目录/. For example, an article may use the default template that isarchive/detail.html. If you want to add code to a specific article (such as an article with ID 100), you can copy onearchive/detail.html, and then name itarchive/detail-100.htmlor something more meaningful, such asarchive/landing-page-special.html. Remember, according to the document conventions, template files are uniformly used.htmlsuffix.embed statistical codes in the exclusive template: Open the exclusive template file you just created. You can
{% extends 'base.html' %}In the content block after (if your template inherits the basic skeleton){% block ... %}Find the appropriate position to insert your statistics code. For example, if you want to insert a statistics code on the page<head>Insert code within, you can name it ahead_scriptsofblockAdd:{% block head_scripts %} {{ super() }} {# 保留父模板中的内容,如果有的话 #} <!-- 特定页面的Google Analytics事件追踪代码 --> <script> gtag('event', 'view_item', { 'item_id': 'specific_product_id', 'item_name': '特定商品名称' }); </script> <!-- 其他自定义JS或CSS --> {% endblock %}If you want to insert code before the end
</body>There will usually be abody_end_scriptsor similarblock:{% block body_end_scripts %} {{ super() }} <script> console.log('这个页面加载了特定的统计脚本。'); // 其他需要执行的脚本 </script> {% endblock %}If your template does not explicitly define these
blockyou may need to check or edit yourbase.htmlto add these expandable areas.specify this dedicated template on the backendThis is the most critical step, linking the front-end code with the back-end content.
- For articles/products:Enter the AnQi CMS backend, find the specific article or product you want to add statistical code to. In the editing page, you will see an option called "Document Template" (refer to the
help-content-archive.mdHere, fill in the relative path and name of the template file you just created, such asarchive/landing-page-special.html. - For a single page: Similarly, go to the "Page Resources" under the "Page Management" section, edit the single page you are targeting. In the "Single Page Template" field (refer to
help-source-page.mdPlease fill in your custom template name, for examplepage/special-about-us.html.
- For articles/products:Enter the AnQi CMS backend, find the specific article or product you want to add statistical code to. In the editing page, you will see an option called "Document Template" (refer to the
After saving the changes, only when the specified page is visited, the exclusive statistics code you embedded will be loaded and executed.
Method two: Apply custom code to a specific category page
Similar to a single content item, AnQi CMS also allows you to apply different list templates for specific content categories (such as the "Market Activity Information" category or the "Promotional Products" category).This means you can统一植入 statistical code for all pages under a category (usually a list page).
Create or copy a dedicated category list template: If you want to add code to the category with ID 5, you can copy a default category list template (such as
article/list.html), and then name itarticle/list-5.htmlorarticle/promotions-list.html.Embed statistical code in the dedicated category template: Insert your statistical code at the appropriate position in the template, just like an independent page.
Specify the category template on the backendEnter the 'Content Management' -> 'Document Category' page of the AnQi CMS backend and edit the target category. Enter the 'Category Template' field (refer to
help-content-category.md), fill in