Ottieni il nome della categoria usando JS

Hi, maybe someone can help.
I need to get the category name of the current page.
I need to get this in a JS script that I can add to Footer or Body.
I’ve tried a couple of solutions, but I can’t get it to be stable and given the user’s page transitions.
I plan to pass the resulting category name as an Event in Google Analytics to count visits for each category.

Potrebbe esserci un modo per farlo in modo coerente sia nelle pagine degli argomenti che in quelle delle categorie, ma ho ottenuto l’ID della categoria in due modi diversi… uno per le pagine delle categorie:

<script type="text/discourse-plugin" version="0.8">
const container = Discourse.__container__;
const controller = container.lookup('controller:navigation/category');

api.onPageChange((url, title) => {
  console.log(controller.get("category.id"));
});
</script>

e un modo simile per le pagine degli argomenti:

<script type="text/discourse-plugin" version="0.8">
const container = Discourse.__container__;
const controller = container.lookup('controller:topic');

api.onPageChange((url, title) => {
  console.log(controller.get("model.category_id"));
});
</script>

Nell’esempio della pagina della categoria, in alternativa, puoi usare category.name o category.slug, ma con il modello dell’argomento è disponibile solo category_id, quindi dovresti fare un po’ più di lavoro lì per ottenere il nome. L’ID potrebbe comunque essere un percorso migliore perché è coerente anche se il nome o lo slug della categoria cambiano.

9 Mi Piace

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.

1 Mi Piace