← back to CelebritySignatures

public/assets/ga-events.js

56 lines

/* GA4 intent-event instrumentation for celebsignatures.com (property G-2HEVP6TD0J).
 * DTD-B verdict (2026-08-04): wire NAMED conversion/engagement events, not vanity metrics.
 * Pure DELEGATED listeners in the capture phase — never touches existing page handlers,
 * so it can't break the gallery, the mural order flow, or the upload form. No-op if gtag
 * isn't loaded yet. Safe to drop on any page; listeners simply never match what isn't there. */
(function () {
  function ev(name, params) {
    if (typeof window.gtag === 'function') window.gtag('event', name, params || {});
  }
  var onMurals = location.pathname.indexOf('mural') !== -1;

  document.addEventListener('click', function (e) {
    var t = e.target;
    if (!t || !t.closest) return;

    // Hero / nav CTAs (all pages)
    var cta = t.closest('.hbtn');
    if (cta) { ev('cta_click', { label: (cta.textContent || '').trim().slice(0, 60), href: cta.getAttribute('href') || '' }); return; }

    // Signature detail open (home gallery)
    if (t.closest('.info-chip')) { ev('view_signature', { source: 'info_chip' }); return; }

    // Murals — revenue + engagement
    if (t.closest('#payBtn'))       { ev('mural_order_click', { location: 'order_panel' }); return; }
    if (t.closest('[data-place]'))  { ev('mural_place_on_wall'); return; }
    if (t.closest('.roster-trig'))  { ev('mural_roster_open'); return; }

    // Upload / account (contribution funnel)
    if (t.closest('#uploadBtn')) { ev('signature_upload', { step: 'submit_click' }); return; }
    if (t.closest('#loginBtn'))  { ev('account_login_click'); return; }
    if (t.closest('#signupBtn')) { ev('account_signup_click'); return; }

    // Games (game.html) — capture the chosen mode/difficulty/category on Begin
    var gcard = t.closest('.gcard[data-g]');
    if (gcard) { ev('game_mode_select', { mode: gcard.getAttribute('data-g') }); return; }
    if (t.closest('#goBtn')) {
      var pick = function (sel, attr) { var el = document.querySelector(sel); return el ? el.getAttribute(attr) : ''; };
      // #catOpts is display:none for modes where category is irrelevant (art/match/early/lightning) —
      // only log category when the control is actually visible, else GA4 accumulates a stale junk dimension.
      var catOpts = document.querySelector('#catOpts');
      var catVisible = catOpts && catOpts.style.display !== 'none';
      ev('game_start', {
        mode:       pick('.gcard.on[data-g]', 'data-g'),
        difficulty: pick('#diffOpts .opt.on[data-d]', 'data-d'),
        category:   catVisible ? pick('#catOpts .opt.on[data-c]', 'data-c') : ''
      });
      return;
    }
  }, true);

  // Murals "Request a quote instead" is a form submit, not a click
  document.addEventListener('submit', function () {
    if (onMurals) ev('mural_quote_request');
  }, true);
})();