Мне удалось получить ID категории для страниц категорий и тем, но когда я перехожу на страницу FAQ или другую страницу, также отображается ID категории с предыдущей страницы. Как это можно исправить?
Вот мой код, он работает, но я в целом не знаком с JS.
<script type="text/discourse-plugin" version="0.8">
const container = Discourse.__container__;
api.onPageChange((url, title) => {
const topic = container.lookup('controller:topic');
const topic_cat_id = topic.get("model.category_id");
if(typeof topic_cat_id !== "undefined")
{
gtag('event', 'Categories Views Analytics', {
'event_category': 'Topic Page',
'event_label': topic_cat_id,
});
console.log("Topic " + topic_cat_id);
} else {
const category = container.lookup('controller:navigation/category');
const category_cat_id = category.get("category.id");
if(typeof category_cat_id !== "undefined")
{
gtag('event', 'Categories Views Analytics', {
'event_category': 'Category Page',
'event_label': category_cat_id,
});
console.log("Category " + category_cat_id);
} else {
gtag('event', 'Categories Views Analytics', {
'event_category': 'Other Page',
'event_label': 'Other Page'
});
console.log("Other");
}
}
});
</script>
Наверное, я что-то напутал с переменными или мне нужно получить контроллер другого типа страницы.