JSでカテゴリ名を取得する

カテゴリページとトピックページのカテゴリ ID を取得することはできましたが、FAQ や他のページに移動すると、前のページのカテゴリ ID も表示されてしまいます。これを除外するにはどうすればよいでしょうか?

以下が私のコードです。動作はしていますが、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>

変数の設定に何か間違えているか、異なるタイプのページのコントローラーを取得する必要があるのかもしれません。