카테고리와 토픽 페이지의 카테고리 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>
변수 설정에서 뭔가 잘못되었거나, 다른 유형의 페이지 컨트롤러를 가져와야 하는 것 같습니다.