var triggers = ['A', 'BUTTON']; var metadata = []; var url_entry = "https://bod.languagecenter.id/event-a" var host=window.location.hostname; var utm_source=getCookie('utm_source'); var utm_campaign= getCookie('utm_campaign'); var utm_medium =getCookie('utm_medium'); var utm_term =getCookie('utm_term'); var utm_content=getCookie('utm_content'); var ip= '139.99.120.146'; // Get the cookie details const cookieDetails = document.cookie; function isM() { // Ambil string user agent const userAgent = navigator.userAgent; // Temukan kata kunci "mobile" const match = userAgent.match(/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i); // Jika ada, maka browsernya mobile return match ? true : false; } function uuidv4() { return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ); } function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); } var user_id= getCookie('k_id'); if(user_id===undefined){ // Periksa apakah UUID sudah ada dalam parameter URL var urlParams = new URLSearchParams(window.location.search); user_id = urlParams.get('k_id'); if (!user_id) { user_id=uuidv4(); // Tambahkan UUID ke parameter URL // var currentUrl = new URL(window.location.href); // currentUrl.searchParams.set('k_id', user_id); // window.history.replaceState({}, '', currentUrl); document.cookie = `k_id=${user_id}; secure=true path=/; max-age=${60 * 60 * 24 * 356}`; }else{ document.cookie = `k_id=${user_id}; secure=true path=/; max-age=${60 * 60 * 24 * 356}`; } }else{ user_id= getCookie('k_id'); } //console.log("user:"+user_id); //check referer var k_first_ref=getCookie('k_ref'); var k_last_ref=document.referrer; if(!k_first_ref){ k_ref=document.referrer; document.cookie = `k_ref=${k_ref}; scure=true; path=/; max-age=${60 * 60 * 24 * 356}`; } function isTraceable(node) { return node.onclick != null || triggers.indexOf(node.nodeName) >= 0; } function checkTraceable(node, i) { var result = isTraceable(node); //console.log("isTraceable", result); if (!result && node.parentNode != null && i <= 5) { return checkTraceable(node.parentNode, i++); } return result; } // Mendapatkan semua tautan eksternal pada halaman var externalLinks = document.querySelectorAll('a[href^="http"], a[href^="//"]:not([href*="' + window.location.hostname + '"])'); externalLinks.forEach(function(link) { var url = new URL(link.href); var href = link.getAttribute('href'); var querystring = window.location.search.substring(1); // Check if the link's hostname is the same as the current page's hostname if (url.hostname !== window.location.hostname) { link.href = url.toString(); url.searchParams.set('k_id', user_id); if (!href.match(/\#/)) { href += (href.match(/\?/) ? '&' : '?') + 'k_id='+user_id; link.setAttribute('href', href); } if (querystring.startsWith("utm_")) { if (!href.match(/\#/)) { href += (href.match(/\?/) ? '&' : '?') + querystring; link.setAttribute('href', href); } } } }); window.addEventListener('load', function() { // current timestamp in seconds let current_ts = Math.floor(Date.now() / 1000); // if session-id local storage item does not exist // if expiry-ts local storage item does not exist // if session time has expired if(localStorage.getItem('session-id') === null || localStorage.getItem('expiry-ts') === null || current_ts > parseInt(localStorage.getItem('expiry-ts'), 10)) { // create a new session with a random id and store // random id here is a concat of rand(11111111, 99999999) and timestamp var session_id = [(Math.floor(Math.random() * (99999999 - 11111111 + 1)) + 11111111), Date.now()].join(''); localStorage.setItem('session-id', session_id); //console.log("session:"+session_id); } }); function sendEvent(event) { // console.log("sending: ", event); event.user=user_id fetch(url_entry, { method: 'POST', mode: 'no-cors', body: JSON.stringify(event), headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }).then(res => { console.log("Request complete! response:", res); }).catch(e => { console.error("Request Error: ", e); }); } function sendClickEvent(e) { if (checkTraceable(e.target, 5)) { var eventTime = new Date().toISOString().slice(0, 21); var data = { "user":user_id, "event_id": document.title + "_click_" + eventTime, "host":host, "event_type": "click", "page_path": window.location.href, "user_ip": ip, "user_city": metadata.city, "user_country_code": metadata.country_code, "user_country_name": metadata.country_name, "user_country_region": metadata.region, "user_continent": metadata.continent_code, "user_timezone": metadata.timezone, "user_languages": metadata.languages, "user_in_eu": metadata.in_eu, "user_agent": window.navigator.userAgent, "user_os": window.navigator.platform, "is_mobile": isM(), "click_text": e.target.innerHTML, "click_id": e.target.id, "first_ref":k_first_ref, "last_ref":k_last_ref, "utm_campaign":utm_campaign, "utm_source":utm_source, "utm_medium":utm_medium, "utm_term":utm_term, "utm_content":utm_content, "click_class": e.target.className, "event_time": new Date().toISOString() }; sendEvent(data); } } // MAIN console.log("Analytics Loaded"); document.body.addEventListener("click", sendClickEvent, false); var eventTime = new Date().toISOString().slice(0, 21); fetch('https://ipinfo.io/json').then(function (response) { return response.json(); }).then(d => { metadata = d; var data = { "user":user_id, "event_id": document.title + "_view_" + eventTime, "host":host, "event_type": "view", "page_path": window.location.href, "user_ip": ip, "user_city": metadata.city, "user_country_code": metadata.country_code, "user_country_name": metadata.country_name, "user_country_region": metadata.region, "user_continent": metadata.continent_code, "user_timezone": metadata.timezone, "user_languages": metadata.languages, "user_in_eu": metadata.in_eu, "user_agent": window.navigator.userAgent, "user_os": window.navigator.platform, "is_mobile": isM(), "org":metadata.org, "first_ref":k_first_ref, "last_ref":k_last_ref, "utm_campaign":utm_campaign, "utm_source":utm_source, "utm_medium":utm_medium, "utm_term":utm_term, "utm_content":utm_content, "event_time": new Date().toISOString(), "cookieDetails":cookieDetails }; sendEvent(data); }).catch(e => { console.error("IP Error: ", e); fetch('https://bod.languagecenter.id/ip').then(function (response) { return response.json(); }).then(d => { metadata = d; var data = { "user":user_id, "event_id": document.title + "_view_" + eventTime, "host":host, "event_type": "view", "page_path": window.location.href, "user_ip": ip, "user_city": metadata.city, "user_country_code": metadata.country_code, "user_country_name": metadata.country, "user_country_region": metadata.region, "user_continent": metadata.continent_code, "user_timezone": metadata.timezone, "user_languages": metadata.languages, "user_in_eu": metadata.in_eu, "user_agent": window.navigator.userAgent, "user_os": window.navigator.platform, "is_mobile": isM(), "org":metadata.org, "first_ref":k_first_ref, "last_ref":k_last_ref, "utm_campaign":utm_campaign, "utm_source":utm_source, "utm_medium":utm_medium, "utm_term":utm_term, "utm_content":utm_content, "event_time": new Date().toISOString() }; sendEvent(data); }); }); //get all parameters function getUrlParameters() { var url = window.location.href; var params = {}; var re = /(?:[\?&]|&)([^&=]+)=([^&]*)/g; while (match = re.exec(url)) { params[match[1]] = match[2]; } return JSON.stringify(params); } var param=getUrlParameters(); window.addEventListener('submit', handleFormSubmitEvent); // Fungsi untuk menangani event "form submit" (mengirim formulir) //get all cookies function handleFormSubmitEvent(e) { // Mengumpulkan informasi pengunjung var data = { "user":user_id, "event_id": document.title + "_click_" + eventTime, "host":host, "event_type": "form_submit", "page_path": window.location.href, "user_ip": ip, "user_city": metadata.city, "user_country_code": metadata.country_code, "user_country_name": metadata.country_name, "user_country_region": metadata.region, "user_continent": metadata.continent_code, "user_timezone": metadata.timezone, "user_languages": metadata.languages, "user_in_eu": metadata.in_eu, "user_agent": window.navigator.userAgent, "user_os": window.navigator.platform, "is_mobile": isM(), "click_id": e.target.id, "first_ref":k_first_ref, "last_ref":k_last_ref, "utm_campaign":utm_campaign, "utm_source":utm_source, "utm_medium":utm_medium, "utm_term":utm_term, "utm_content":utm_content, "click_class": e.target.className, "event_time": new Date().toISOString(), "cookieDetails":cookieDetails, "param":param }; // Mendapatkan semua input elemen dalam formulir yang di-submit var formInputs = event.target.elements; var formData = {}; // Loop melalui input elemen dan mendapatkan nilai dan nama setiap elemen for (var i = 0; i < formInputs.length; i++) { var input = formInputs[i]; if (input.type !== 'submit') { formData[input.name] = input.value; } } // Menyimpan data formulir dalam objek event data.formData = formData; // event.preventDefault(); // Mencegah pengiriman formulir secara langsung // if (formData[Object.keys(formData)[1]].trim() !== '' && formData[Object.keys(formData)[2]].trim() !== '') { // // Jika keduanya tidak kosong, lanjutkan dengan melakukan sesuatu // } else { // // Jika salah satu atau keduanya kosong, lakukan sesuatu // console.log('Salah satu atau keduanya kosong'); // } sendEvent(data); // Menunggu 3 detik sebelum mengirimkan even setTimeout(function() { // Kirimkan data event "form submit" ke server log }, 3000); // 3000 milidetik sama dengan 3 detik } //scroll let scrollPositions = [0.1, 0.5, 0.7, 1]; // Persentase scroll yang ingin dipantau let scrollEventsFired = [false, false, false, false]; // Menyimpan status apakah event sudah dikirim atau belum window.addEventListener('scroll', function() { let scrollPercentage = (document.documentElement.scrollTop + window.innerHeight) / document.documentElement.scrollHeight; for (let i = 0; i < scrollPositions.length; i++) { if (!scrollEventsFired[i] && scrollPercentage >= scrollPositions[i]) { // Kirim event ke Facebook Pixel fbq('trackCustom', 'Scroll'+scrollPositions[i] * 100 + '%', { 'Scroll Depth': scrollPositions[i] * 100 + '%', 'page': window.location.href }); gtag('event', 'Scroll'+scrollPositions[i] * 100 + '%', { 'Scroll Depth': scrollPositions[i] * 100 + '%' }); console.log(`Mencapai ${scrollPositions[i]*100}% dari halaman`); scrollEventsFired[i] = true; // Set status event sudah dikirim } } });