الحصول على اسم الفئة باستخدام JS

تمكنت من الحصول على معرف الفئة (Category ID) لصفحات الفئة والمواضيع،
لكن عندما أذهب إلى صفحة الأسئلة الشائعة (FAQ) أو أي صفحة أخرى، يتم عرض معرف الفئة من الصفحة السابقة أيضًا.
كيف يمكنني استبعاد ذلك؟

إليك الكود الخاص بي، وهو يعمل، لكنني لا أعرف JavaScript بشكل عام.

<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>

لابد أنني أخطأت في شيء ما في المتغيرات، أو أحتاج إلى الحصول على وحدة تحكم (controller) لنوع مختلف من الصفحات.