Score:0

What hook to use to fire a JS event when a layout is used?

cn flag

I inherited a Drupal site. I'm a Drupal noob. There are lots of things that don't seem to be in use, but I want to be certain before I delete them. Right now I'm looking at dozens of layouts that are not activated, but I know for certain some of them are used on legacy pages. I'm planning to fire a Google Analytics event with the URL and the name of the layout its using, so I can see what layouts are being used and where.

  1. What hook should I use to load the custom Google Analytics event? It needs access to the URL being loaded and the layout machine name.
  2. Is there a better "Drupal" way?

Thanks in advance.

id flag
Is the site using a Google Analytics integration module?
leymannx avatar
ne flag
Can't some layout selector like a class or ID not maybe be found in the markup? Would that help? If you have the machine name of a layout somewhere in the HTML? And you do the Analytics logic all from just a custom JavaScript file yourself? The pushing to the data layer. You would have all you need with JS then. That is loaded on every page. Then all you need to find out is how to attach a custom JS file from a custom module or theme.
leymannx avatar
ne flag
I don't know enough about layout's architecture. But another possibility would be the load all nodes programmatically and check which layouts they use and store the layouts in an array and compare that to the array with all available layouts. (If layouts is data that can be referenced from a node.)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.