document.addEventListener("DOMContentLoaded", function (){
document.body.classList.add("loaded");
});
(function (){
const url=new URL(window.location.href);
if(url.searchParams.get('utm_source')) return;
const ref=document.referrer;
if(!ref) return;
const refURL=new URL(ref);
const host=refURL.hostname;
if(host===window.location.hostname) return;
let source='direct';
let medium='none';
let campaign='auto_campaign';
if(host.includes('facebook.com')){
source='facebook';
medium='social';
}else if(host.includes('instagram.com')){
source='instagram';
medium='social';
}else if(host.includes('t.co')||host.includes('twitter.com')){
source='twitter';
medium='social';
}else if(host.includes('t.me')||host.includes('telegram.org')){
source='telegram';
medium='social';
}else if(host.includes('google.com')){
source='google';
medium='organic';
}else{
source=host.replace('www.', '');
medium='referral';
}
url.searchParams.set('utm_source', source);
url.searchParams.set('utm_medium', medium);
url.searchParams.set('utm_campaign', campaign);
url.searchParams.set('utm_source_platform', source);
url.searchParams.set('utm_content', 'auto');
url.searchParams.set('utm_term', 'none');
url.searchParams.set('lang', navigator.language||'id');
url.searchParams.set('region', Intl.DateTimeFormat().resolvedOptions().timeZone||'Asia/Jakarta');
window.history.replaceState({}, '', url);
})();
function updateShareCount(postId){
fetch(jobnas_ajax.ajax_url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'action=update_share_count&post_id=' + encodeURIComponent(postId)
})
.then(response=> response.json())
.then(data=> {
if(data.success){
const shareCountSpan=document.querySelector(`#share-icon-${postId} .share-count`);
if(shareCountSpan){
shareCountSpan.textContent=data.data.share_count;
}}
});
}
document.addEventListener('DOMContentLoaded', function (){
const postId=document.body.dataset.postId;
fetch(jobnas_ajax.ajax_url + '?action=get_share_count&post_id=' + encodeURIComponent(postId))
.then(res=> res.json())
.then(data=> {
if(data.success){
const el=document.querySelector(`#share-icon-${postId} .share-count`);
if(el){
el.textContent=data.data.share_count;
}}
});
});
function copyShareUrl(postId, url){
navigator.clipboard.writeText(url)
.then(()=> {
updateShareCount(postId);
alert("Link disalin!");
})
.catch(()=> {
alert("Gagal menyalin link.");
});
}
document.addEventListener('DOMContentLoaded', function (){
document.querySelectorAll('.share-buttons').forEach(trigger=> {
trigger.addEventListener('click', function (e){
const parent=trigger.closest('.share-buttons');
const tooltip=parent.querySelector('.tombol-share');
if(tooltip.classList.contains('active-tooltip')){
tooltip.classList.remove('active-tooltip');
}else{
document.querySelectorAll('.tombol-share.active-tooltip').forEach(t=> t.classList.remove('active-tooltip'));
tooltip.classList.add('active-tooltip');
}});
});
document.addEventListener('click', function (e){
if(!e.target.closest('.share-buttons')){
document.querySelectorAll('.tombol-share.active-tooltip').forEach(t=> t.classList.remove('active-tooltip'));
}});
});