I was able to get the Category ID for the Category and Topic pages,
But when I go to the FAQ or another page, the Category ID is also displayed, from the previous page.
How can I exclude this?
Here is my code, it works, I do not know JS in general.
<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>
I must have messed up something in the variables or need to get a controller of a different type of page.