Adding Google Analytics tracking to WordPress Gutenberg block elements

I’m working on implementing GA4 tracking through Google Tag Manager for a WordPress website that uses Gutenberg blocks. The main challenge I’m facing is getting proper analytics data from interactive elements like buttons and links that are created using Gutenberg’s block editor.

The issue seems to be that I can’t figure out how to assign custom IDs or tracking attributes to these elements. When I create a button or link through Gutenberg, I don’t see obvious options to add the necessary identifiers that Google Tag Manager needs to track user interactions properly.

Does anyone know if there’s a way to add custom HTML attributes or unique identifiers to hyperlinks and button elements when building them through the Gutenberg interface? I need this for proper event tracking in my analytics setup.

I had the same issue when setting up GA4 tracking. Skip Gutenberg’s built-in options and use Google Tag Manager instead. I set up CSS selectors that target Gutenberg’s classes - like wp-block-button__link for buttons and links. Works great for basic tracking. If you need more specific tracking, try the Advanced Custom Fields plugin. You can add data attributes right in your custom blocks, which makes everything way easier to manage. My analytics got much cleaner and way more accurate after switching to this approach.