// /web/js/aquadrop-analytics.js (function(){ window.dataLayer = window.dataLayer || []; function dl(obj){ window.dataLayer.push(obj); } // 外部リンク判定 function isExternal(url){ try { var a = document.createElement('a'); a.href = url; return a.hostname && a.hostname !== location.hostname; } catch(e){ return false; } } // 1) CTAクリック(予約・問い合わせなど) function trackCTA(name, params){ dl(Object.assign({ event: 'cta_click', cta_name: name || '', page_path: location.pathname, page_title: document.title }, params || {})); } // 2) 電話クリック function bindTelClicks(){ document.querySelectorAll('a[href^="tel:"]').forEach(function(a){ a.addEventListener('click', function(){ dl({ event: 'call_click', phone: (a.getAttribute('href')||'').replace('tel:',''), page_path: location.pathname }); }); }); } // 3) 外部リンク(アソビュー・LINEなど) function bindOutboundLinks(){ document.querySelectorAll('a[href]').forEach(function(a){ var href = a.getAttribute('href'); if(!href) return; if(href.indexOf('javascript:') === 0) return; if(isExternal(href)){ a.addEventListener('click', function(){ dl({ event: 'outbound_click', link_url: href, link_text: (a.textContent||'').trim().slice(0,100), page_path: location.pathname }); }); } }); } // 4) スクロール深度(25/50/75/100) function bindScrollDepth(){ var marks = {25:false,50:false,75:false,100:false}; function onScroll(){ var h = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight); var pos = (window.scrollY + window.innerHeight) / h * 100; [25,50,75,100].forEach(function(p){ if(!marks[p] && pos >= p){ marks[p] = true; dl({ event: 'scroll_depth', percent: p, page_path: location.pathname }); } }); if(marks[100]) window.removeEventListener('scroll', onScroll); } window.addEventListener('scroll', onScroll); } // 5) フォーム送信成功(完了ページで呼び出し) function formSubmitSuccess(name){ dl({ event: 'form_submit_success', form_name: name || 'unknown', page_path: location.pathname }); } // 初期化 document.addEventListener('DOMContentLoaded', function(){ bindTelClicks(); bindOutboundLinks(); bindScrollDepth(); }); window.AquaTrack = { trackCTA: trackCTA, formSubmitSuccess: formSubmitSuccess }; })();