<script>
  document.querySelectorAll('a.booknow, .owners').forEach(btn => {
    btn.addEventListener('click', function() {
      gtag('event', 'cta_click', {
        button_text: btn.innerText,
        page_title: document.title,
        page_location: window.location.href
      });
    });
  });
</script>

<script>
  document.querySelectorAll('a[href*="sixonn.capetown"], a[href*="eighty2onm.com"], a[href*="one26onm.com"], a[href*="three43onb.com"]').forEach(link => {
    link.addEventListener('click', function() {
      gtag('event', 'outbound_property_click', {
        destination: link.href,
        page_title: document.title,
        page_location: window.location.href
      });
    });
  });
</script>

<script>
  let scrollFired50 = false;
  let scrollFired75 = false;

  window.addEventListener('scroll', function() {
    let scrolled = (window.scrollY + window.innerHeight) / document.body.scrollHeight;

    if (!scrollFired50 && scrolled > 0.5) {
      scrollFired50 = true;
      gtag('event', 'scroll_50_percent', {
        page_title: document.title,
        page_location: window.location.href
      });
    }

    if (!scrollFired75 && scrolled > 0.75) {
      scrollFired75 = true;
      gtag('event', 'scroll_75_percent', {
        page_title: document.title,
        page_location: window.location.href
      });
    }
  });
</script>

<script>
  document.querySelectorAll('form.newsletter-form').forEach(form => {
    form.addEventListener('submit', function() {
      gtag('event', 'newsletter_signup', {
        form_id: form.id,
        page_title: document.title
      });
    });
  });
</script>
