본문 바로가기

개발관련

티스토리 무효 클릭 방지 코드 공유

728x90
반응형

2025.01.24 - [개발관련] - Supabase에 대한 정보 모음 (비용)

 

 

아래 코드를 <head> </head> 사이에 넣으시면 됩니다!

<script>
        class AdSenseProtection {
            constructor() {
                this.lastClickTime = 0;
                this.clickInterval = 1000; // 1초 간격
                this.maxClicks = 3; // 세션당 최대 클릭 수
                this.clickCount = 0;
                this.resetTime = 24 * 60 * 60 * 1000; // 24시간
                this.lastResetTimestamp = Date.now();
            }

            isValidClick() {
                const now = Date.now();

                // 클릭 간격 체크
                if (now - this.lastClickTime < this.clickInterval) {
                    return false;
                }

                // 세션 클릭 수 체크
                if (now - this.lastResetTimestamp > this.resetTime) {
                    this.clickCount = 0;
                    this.lastResetTimestamp = now;
                }

                if (this.clickCount >= this.maxClicks) {
                    return false;
                }

                this.lastClickTime = now;
                this.clickCount++;
                return true;
            }

            detectBot() {
                const botPatterns = ['bot', 'spider', 'crawler'];
                return botPatterns.some(pattern => 
                    navigator.userAgent.toLowerCase().includes(pattern)
                );
            }

            preventFraud(adElement) {
                if (this.detectBot()) return false;
                return this.isValidClick();
            }
        }

        // 클릭 보호 초기화
        const adProtection = new AdSenseProtection();

        // 모든 광고 요소에 이벤트 리스너 추가
        document.querySelectorAll('.ad-element').forEach(ad => {
            ad.addEventListener('click', (e) => {
                if (!adProtection.preventFraud(ad)) {
                    e.preventDefault();
                    console.warn('부정 클릭 차단됨');
                }
            });
        });
    </script>

 

728x90
반응형