I want to be able to track the clicks going on those buttons. I can't just track the links because they are some of the most common spots on our site. I gave each button a label as seen in one of the below code snippets. I think I set up my GTM tag up correctly where I made a new event, added my action and value of {{click element}} and label with a value of {{click text}}. Ideally I want to be able to see each click on each button in GA4. this is my GTM tag set up for reference
Here is my current 404.php file in my child theme.
<?php
/**
* The template for displaying 404 pages (not found).
*
* @package Flatsome\Templates
* @flatsome-version 3.16.0
*/
get_header(); ?>
<?php do_action( 'flatsome_before_404' ); ?>
<?php
if ( get_theme_mod( '404_block' ) ) :
echo do_shortcode( '[block id="' . get_theme_mod( '404_block' ) . '"]' );
else :
?>
<div id="primary" class="content-area">
<main id="main" class="site-main container pt" role="main">
<section class="error-404 not-found mt mb">
<div class="row">
<div class="col medium-3"><span class="header-font" style="font-size: 6em; font-weight: bold; opacity: .3">404</span></div>
<div class="col medium-9">
<header class="page-title">
<h1 class="page-title"><?php esc_html_e( 'Oops! That page can’t be found.', 'flatsome' ); ?></h1>
</header>
<div class="page-content">
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try one of the links below or a search?', 'flatsome' ); ?></p>
<?php get_search_form(); ?>
<!-- Button Click Tracking Code -->
<script>
document.addEventListener('DOMContentLoaded', function() {
let buttons = document.querySelectorAll('.button');
buttons.forEach(function(button) {
button.addEventListener('click', function() {
gtag('event', 'click', {
'event_category': '404_button_click',
'event_label': button.textContent
});
});
});
});
</script>
<!-- End Button Click Tracking Code -->
</div>
</div>
</div>
</section>
</main>
</div>
<?php endif; ?>
<?php do_action( 'flatsome_after_404' ); ?>
<?php get_footer(); ?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=INSERT GTM_ID HERE"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
Below are the buttons that I have on my current 404 page design (this is what I pulled from the flatsome UX builder which is why the syntax is like that).
[button text="Shop" color="white" radius="10" depth="1" depth_hover="4" icon="icon-angle-right" link="https://www.memoriapress.com/curriculum/classical-core-curriculum/" class="404-shop" data-404-click="true" data-404-label="Shop"]
[button text="Go To Blog" color="white" radius="10" depth="1" depth_hover="4" icon="icon-angle-right" link="https://www.memoriapress.com/articles/" class="404-blog" data-404-click="true" data-404-label="Go To Blog"]
[button text="YouTube" color="white" radius="10" depth="1" depth_hover="4" icon="icon-angle-right" link="https://www.youtube.com/memoriapress" class="404-youtube" data-404-click="true" data-404-label="YouTube"]
[button text="Contact Us" color="white" radius="10" depth="1" depth_hover="4" icon="icon-angle-right" link="https://www.memoriapress.com/support/contact-us/" class="404-contact" data-404-click="true" data-404-label="Contact Us"]