# AdSense implementation research

## ข้อค้นพบจากเอกสาร Google ทางการ

1. การเชื่อมเว็บไซต์กับ AdSense ต้องเพิ่มเว็บไซต์ในบัญชี, วาง AdSense code หรือวิธี verify ที่ Google รองรับ, ตรวจสอบ และขอ review ก่อนจะแสดงโฆษณาได้จริง โดยการตรวจอาจใช้เวลาหลายวันหรือ 2–4 สัปดาห์ในบางกรณี [1]
2. `ads.txt` ควรอยู่ที่ root ของโดเมน เช่น `https://485tech.com/ads.txt` และต้องมี publisher ID ในรูปแบบ `google.com, pub-XXXXXXXXXXXXXXXX, DIRECT, f08c47fec0942fa0` เมื่อใช้ AdSense; Google ระบุว่า ads.txt ไม่ได้บังคับ แต่แนะนำอย่างยิ่ง และการเปลี่ยนแปลงอาจใช้เวลาหลายวันกว่าจะปรากฏในระบบ [2]
3. ผู้เผยแพร่ที่ให้บริการโฆษณาแบบ personalized แก่ผู้ใช้ใน EEA, UK หรือ Switzerland ต้องใช้ CMP ที่ Google รับรองและเชื่อมกับ IAB TCF ตามข้อกำหนดที่ Google ระบุ [3]
4. Google ระบุว่า strict CSP ที่ใช้ nonce และ `strict-dynamic` เป็นแนวทางที่รองรับกับ AdSense เมื่อเปิด CSP; ต้องใส่ nonce ให้ทุก script ที่เกี่ยวข้อง และควรทดสอบด้วย `Content-Security-Policy-Report-Only` ก่อน enforce [4]
5. AdSense ห้ามการจัดวางที่ทำให้ผู้ใช้เข้าใจว่าโฆษณาเป็น navigation, menu หรือ download, ห้ามชักจูงให้คลิก, ห้าม pop-up/pop-under และหน้าเว็บที่มีโฆษณาต้องนำทางได้ง่าย [5] [6]

## ผลตรวจ repository ปัจจุบัน

- มี AdSense client ID อยู่ใน `src/layout.html` และ `src/app-layout.html`: `ca-pub-6341158024893936`.
- มี AdSense loader ใน `public/scripts/ads.js` และ `.ad-slot` ในหน้า public บางหน้าและ standalone tool pages.
- ยังไม่พบ `public/ads.txt`.
- `window.AD_CONFIG` ปัจจุบัน render ad unit แบบ auto responsive แต่ใช้เฉพาะ `data-ad-client` ไม่ได้กำหนด `data-ad-slot`; จึงควรยืนยันในบัญชีว่าตั้งใจใช้ Auto ads หรือ manual ad units และไม่ควรเดา slot ID.
- Cloudflare Worker ใช้ strict nonce CSP และตั้ง `CSP_MODE=report-only`; production config มี allowlist เดิมสำหรับ `pagead`/`doubleclick` ผ่าน vars แต่ยังต้องตรวจ network/CSP violations หลังเปิดใช้งานจริง.
- หน้า privacy มีการกล่าวถึง AdSense และ cookie โดยทั่วไปแล้ว แต่ยังไม่มี consent UX ที่เชื่อมกับการโหลดโฆษณาอย่างชัดเจน.

## ข้อจำกัดที่ต้องขอจากเจ้าของบัญชี

การสร้าง `ads.txt` ที่ใช้งานจริงต้องใช้ publisher ID ที่ยืนยันในบัญชี AdSense และการวาง manual ad unit ต้องใช้ slot ID จากบัญชีเดียวกัน ระบบจะไม่สร้างค่า placeholder แล้วเปิดโฆษณาปลอม เพราะอาจทำให้ verify/review และการแสดงผลคลาดเคลื่อน ควรใช้ client ID ที่พบใน source ได้ต่อเมื่อเจ้าของยืนยันว่าเป็นบัญชีของเว็บไซต์นี้

## References

[1]: https://support.google.com/adsense/answer/7584263?hl=en "Connect your site to AdSense"
[2]: https://support.google.com/adsense/answer/12171612?hl=en "Ads.txt guide"
[3]: https://support.google.com/adsense/answer/13554116?hl=en "Google consent management requirements for publishers"
[4]: https://support.google.com/adsense/answer/16283098?hl=en "Integrate the AdSense ad code with a Content Security Policy"
[5]: https://support.google.com/adsense/answer/48182?hl=en "AdSense Program policies"
[6]: https://support.google.com/adsense/answer/1346295?hl=en "Ad placement policies"

## Browser validation

บน local preview หน้า Tools Hub แสดง consent banner ภาษาไทยก่อนโหลด provider script; ตรวจพบ `consent=null`, ad slot มีสถานะ `consent-pending` และ `hidden=true`, ไม่พบ provider script ใน DOM และพบ manifest `/manifest.webmanifest` ครบถ้วน จึงยืนยันได้ว่าโฆษณาไม่ถูกโหลดก่อนการเลือกของผู้ใช้ใน flow ปัจจุบัน

## Additional References

[7]: https://support.google.com/adsense/answer/16918505?hl=en "How the Google Consent Management Platform works"
[8]: https://support.google.com/adsense/answer/7670013?hl=en "Set up and manage your Consent Management Platform"
[9]: https://support.google.com/adsense/answer/14210870?hl=en "Limited ads"
[10]: https://support.google.com/adsense/answer/9042142?hl=en "Ad code examples for ads personalization settings"

ทดสอบ click flow ของปุ่ม `ยอมรับโฆษณา` สำเร็จ (`clicked=true`) บน local preview; การอ่านผลหลังรอแบบ async จาก console runner คืนค่าไม่สมบูรณ์ จึงต้องยืนยัน provider network/ads rendering บนโดเมน production หลัง deploy อีกครั้ง ซึ่งเป็นข้อจำกัดที่คาดได้จาก local preview และ provider ภายนอก

ผล synchronous validation หลัง allow: `consent=granted`, consent banner หาย, provider script `pagead2.googlesyndication.com` ถูกโหลด, AdSense queue มีรายการ 1 และ slot อยู่สถานะ `auto` (ไม่มี manual slot ID จึงไม่สร้าง ad unit ปลอม) จากนั้นทดสอบ `Ads.setConsent('denied')` สำเร็จและบันทึก `consent=denied`; production verification ยังต้องตรวจว่า Google account อนุมัติ site และ Auto Ads เปิดอยู่จริง
