In website operation, we often need to track user behavior, evaluate the effectiveness of marketing through different channels.Adding tracking parameters dynamically to website links is an effective method.AnQi CMS is an efficient and flexible content management system that provides a powerful template engine and a rich set of filters, allowing us to easily meet this requirement.
Today, let's discuss how to use the Anqi CMS'saddfilter to dynamically add custom tracking parameters to the links of the "Tag (Tag)".
Why do we need to add tracking parameters to label links?
Tags (Tag) play an important role in the organization of website content, they not only help users discover related content but are also crucial for search engines to understand the structure of the website. Adding tracking parameters to tag links, such as UTM parameters, can help us:
- Analyze the source of traffic in detail:Understand how users enter specific content pages through which tags.
- Evaluate the effectiveness of marketing activities:If the tag link is used for external promotion, it can track the traffic and conversion it brings.
- Optimize content strategy:By analyzing users' preferences for different tags, adjust and optimize the direction of content production.
- A/B testing:Test the display form or parameters of links to different tags, and find out **practice.
AnQi CMS uses Django template engine syntax, its built-inaddfilters are the tools to achieve this function.
Get to knowaddFilter
in the AnQi CMS template world,addThe filter plays the role of a 'connector'.Its main function is to concatenate two values, whether it is adding numbers or concatenating strings.addThe filter is very useful because it can combine the original link string with the parameter string we define.
Its basic usage method is very intuitive:{{ 变量 | add: "要添加的内容" }}For example,{{ "安企" | add: "CMS" }}The result is安企CMSThis ability to concatenate strings is the basis for adding parameters to URLs.
Locate the tag link in the Anqi CMS
In Anqi CMS templates, tag links are usually obtained through the following two tags:
tagListTags:Used to display multiple tag lists on the article detail page, sidebar, or a dedicated tag aggregation page.{% tagList tags with limit="10" %} {% for item in tags %} <a href="{{item.Link}}">{{item.Title}}</a> {% endfor %} {% endtagList %}here,
{{item.Link}}That is the original link address of each tag.tagDetailTags:Used on the detail page of a single tag (such astag/detail.html) Get the detailed information of the current tag, including its link.<a href="{% tagDetail with name="Link" %}">前往此标签</a>Or
{% tagDetail tagInfo with name="Link" %} <a href="{{tagInfo}}">前往此标签</a>here,
{% tagDetail with name="Link" %}or{{tagInfo}}That is the link of the current tag.
In either case, the core is to obtain the original link variable of the tag and then utilizeaddFilter it.
Practice: Add tracking parameters dynamically to tag links.
Now, we are ready to manually add custom tracking parameters to these tag links.
Scenario one: TotagListAdd a fixed tracking parameter to the generated tag list
Suppose we want to add a fixed UTM parameter to all tag links, such as?utm_source=anqicms_tag&utm_medium=list.
We can do it intagListthe loop, toitem.LinkapplyaddFilter:
{% tagList tags with limit="10" %}
{% for item in tags %}
<a href="{{ item.Link|add:"?utm_source=anqicms_tag&utm_medium=list" }}">
{{ item.Title }}
</a>
{% endfor %}
{% endtagList %}
This, each tag link will become similarhttps://www.yourdomain.com/tag/seo.html?utm_source=anqicms_tag&utm_medium=listin the form of.
Scenario two: fortagDetailAdd a fixed tracking parameter to the individual tag link obtained
On a tag's detail page, if you want to add tracking parameters to the current page's tag link, the operation is similar:
{# 假设这是 tag/detail.html 模板中 #}
<a href="{% tagDetail with name="Link" %}|add:"?utm_source=anqicms_tag&utm_medium=detail" }}">
进入此标签的详情页
</a>
Or, if you first assign the link to a variable:
{% tagDetail tagLink with name="Link" %}
<a href="{{ tagLink|add:"?utm_source=anqicms_tag&utm_medium=detail" }}">
进入此标签的详情页
</a>
Advanced: Add dynamic parameters (such as tag ID and name)
In order to track more accurately, we may want to add the ID or title of the tag itself as a parameter. At this point, we can use it multiple times.addThe filter performs concatenation and it is necessary to encode dynamic content in URLs to prevent special characters from破坏URL structure. Anqicms providesurlencodeA filter to handle this issue.
{% tagList tags with limit="10" %}
{% for item in tags %}
{# 构造动态参数,确保对动态内容进行url编码 #}
{% set dynamic_params = "&tag_id=" | add:item.Id | add:"&tag_title=" | add:item.Title|urlencode %}
<a href="{{ item.Link|add:"?utm_source=anqicms_tag"|add:dynamic_params }}">
{{ item.Title }}
</a>
{% endfor %}
{% endtagList %}
In this example:
- We first defined a basic UTM parameter
?utm_source=anqicms_tag. - Then, by using continuously
addThe filter, to&tag_id=/item.Id/&tag_title=anditem.TitleConcatenate. - Pay special attention to,
item.TitleAs a URL parameter value, it may contain spaces, Chinese characters, and other special characters, therefore it must be used|urlencodeThe filter needs to be encoded to ensure the validity of the URL.
The generated link may be similar to:https://www.yourdomain.com/tag/seo.html?utm_source=anqicms_tag&tag_id=123&tag_title=SEO%E4%BC%98%E5%8C%96.
Points to note
In actual operation, there are several key points to pay attention to:
- The question mark in a URL (
?) is followed by a connector (&):The first parameter in a URL parameter is preceded by a question mark?, subsequent parameters are followed by a connector&. Simply useaddFilter, cannot automatically judge whether the URL already contains parameters.- Suggested solution:If you are sure that the original label link does not contain any parameters, you can use it directly.
?If the original link is possible
- Suggested solution:If you are sure that the original label link does not contain any parameters, you can use it directly.