From e915dc5ecf58ef96618a1fc9f6281a47b3aa56a6 Mon Sep 17 00:00:00 2001 From: paullabkorea Date: Wed, 4 Sep 2024 17:58:19 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9C=84=EB=8B=88=EB=B8=8C=20=EC=95=A0?= =?UTF-8?q?=EB=84=90=EB=A6=AC=ED=8B=B1=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 1 + src/js/analytics.js | 105 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/js/analytics.js diff --git a/index.html b/index.html index 6f6fc04..4cb74c1 100644 --- a/index.html +++ b/index.html @@ -43,6 +43,7 @@ + diff --git a/src/js/analytics.js b/src/js/analytics.js new file mode 100644 index 0000000..d187482 --- /dev/null +++ b/src/js/analytics.js @@ -0,0 +1,105 @@ +const BASE_URL = 'https://www.analytics.weniv.co.kr'; + +//------------------------------------------------------------ +// @post /collect/pageview +function collectPageView(session_id) { + const header = { + 'Content-Type': 'application/json', + }; + const payload = { + url: window.location.href, + }; + + if (session_id) { + header['Session-Id'] = session_id; + payload.session_id = session_id; + } + + fetch(`${BASE_URL}/collect/pageview`, { + method: 'POST', + headers: header, + body: JSON.stringify(payload), + }) + .then((response) => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then((data) => { + if (!session_id) { + localStorage.setItem('session_id', data.session_id); + } + }) + .catch((error) => console.error('Error:', error)); +} +window.addEventListener('load', (e) => { + const session_id = localStorage.getItem('session_id'); + const lastPage = localStorage.getItem('lastPage'); + + if (lastPage !== window.location.pathname) { + collectPageView(session_id); + } + + localStorage.setItem('lastPage', window.location.pathname); +}); + +//------------------------------------------------------------ +// @post /collect/anchor-click +async function collectAnchorClick(event, type) { + event.preventDefault(); // 기본 동작 막기 + + const ANCHOR = event.currentTarget; + + const session_id = localStorage.getItem('session_id'); + + const source_url = window.location.href; + const target_url = ANCHOR.href; + const target_tar = ANCHOR.target || '_self'; + + try { + const response = await fetch(`${BASE_URL}/collect/anchor-click`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Session-Id': session_id, + }, + body: JSON.stringify({ source_url, target_url, type }), + }); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + } catch (error) { + console.error('Error:', error); + } finally { + window.open(target_url, target_tar); + } +} + +// 외부 링크 +document.querySelectorAll('.kebab-list a').forEach((anchor) => { + anchor.addEventListener('click', (event) => + collectAnchorClick(event, `교육서비스:${anchor.innerText}`), + ); +}); + +//------------------------------------------------------------ +// @post /collect/sql +function collectSQL() { + const session_id = localStorage.getItem('session_id'); + const contents = window.editor.getValue(); + fetch(`${BASE_URL}/collect/sql`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Session-Id': session_id, + }, + body: JSON.stringify({ contents }), + }) + .then((response) => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + }) + .catch((error) => console.error('Error:', error)); +}