Installing Adsense Script

Guide to installing Adsense script on themes.

Basically, every time you add a new ad unit, Google Adsense has provided a complete code that can be directly pasted into the theme. The problem is that if there is more than one ad unit added, there will be a repetition of writing the adsbygoogle.js script code in the theme, this is not efficient, even though the Adsense script is only written/called once in the <head> tag.

Related document:

Activate the adsbygoogle.js script

This guide is optional, you can choose to apply it or stick to the standard way according to Adsense settings.

  1. In Blogger dashboard, click on Theme.
  2. Click icon next to the Customize button.
  3. Click Edit HTML, you will be redirected to the editing page.
  4. Find the closing </head> tag in the HTML code, then paste the code below right before it:
    <!--[ Adsense script ]-->
    <script type='deferjs' src='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-0000000000000000' crossorigin='anonymous'/>
  5. Replace the marked code with your Adsense Publisher ID.
  6. Click Save.

The code above includes auto-ads, you can customize the ad format you want to display through the Adsense dashboard. We also added the type='deferjs' attribute which is useful for delaying loading the Adsense script after all blog content has been loaded.

LazyAdsense for better blog performance (optional)

Adsense scripts are one of the main causes of decreased PageSpeed Insight scores and this cannot be avoided, what can be used is to delay the loading of Adsense scripts using lazyAdsense, but ads will only appear when the user makes the first scroll or a few seconds after the page is fully loaded. This will probably result in a reduced ad serving rate but if you want to try it use one of the codes below:

LazyAdsense With Scroll

With this script the ad will load when the user makes the first scroll on the page.

<!--[ Lazy Adsense by scroll ]-->
<script>/*<![CDATA[*/ let adsenseId = 'ca-pub-0000000000000000',
    Ad = document.createElement('script'),
    lazyAd = false;
window.addEventListener('scroll', () => {
    (0 != document.documentElement.scrollTop && false === lazyAd || 0 != document.body.scrollTop && !1 === lazyAd) && (! function() {
        Ad.setAttribute('crossorigin', 'anonymous'),
            Ad.async = !0,
            Ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' + adsenseId,
            document.head.appendChild(Ad);
    }(), lazyAd = true);
}, true); /*]]>*/</script>

LazyAdsense With Timer

This script is triggered based on a countdown time and is not affected by scroll activity, the ad will be called at the specified number of seconds.

<!--[ Lazy Adsense by timer ]-->
<script>/*<![CDATA[*/ let adsenseId = 'ca-pub-0000000000000000',
    Ad = document.createElement('script'),
    lazyAdTime = 3;
setTimeout(() => {
    Ad.setAttribute('crossorigin', 'anonymous'),
        Ad.async = !0,
        Ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' + adsenseId,
        document.head.appendChild(Ad);
}, lazyAdTime * 1000); /*]]>*/</script>

Based on the script above, the ad will be triggered after countdown 3 which means 3 seconds, to change it replace the value lazyAdTime = 3.

LazyAdsense With Scroll and localStorage

Same as the previous option, ads will load when scroll or tap activity is detected on the screen but only for the first time visit, ads will be displayed asynchronously if the user returns without having to wait for scroll or tap activity again.

The settings will be reset if users delete their browser cookies, this will slightly minimize any decrease in Adsense earnings while maintaining the sites score in PageSpeed Insight.

<!--[ Lazy Adsense by scroll and localStorage ]-->
<script>/*<![CDATA[*/ function lazyAdLoad() {
    let adsenseId = 'ca-pub-0000000000000000',
        Ad = document.createElement('script');
    Ad.setAttribute('crossorigin', 'anonymous'), Ad.async = !0, Ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' + adsenseId, document.head.appendChild(Ad);
}
function detectLazy() {
    lazyAdLoad(), localStorage.setItem('lazyAd', 'true')
}
var lazyAdStorage = localStorage.getItem('lazyAd');
if ('true' != lazyAdStorage) {
    var e = !1,
        t = !1;
    window.addEventListener('scroll', () => {
        (0 != document.documentElement.scrollTop && !1 === e || 0 != document.body.scrollTop && !1 === e) && (detectLazy(), e = !0, t = !0)
    }, !0), window.addEventListener('click', () => {
        !1 === t && !1 === t && (detectLazy(), t = !0, e = !0)
    }, !0)
}
'true' === lazyAdStorage && lazyAdLoad(); /*]]>*/</script>

Choose one of the lazyAdsense codes above to use, place it before the </body> tag and replace ca-pub-0000000000000000 with your Adsense Publisher ID.

Pada dasarnya, setiap kali Anda menambahkan unit iklan baru, Google Adsense telah menyediakan kode lengkap yang bisa langsung ditempelkan ke dalam tema. Permasalahannya adalah jika ada lebih dari satu unit iklan yang ditambahkan, maka akan terjadi pengulangan penulisan kode script adsbygoogle.js di dalam tema, hal ini tidak efisien, padahal script Adsense hanya ditulis/dipanggil satu kali di tag <head>.

Mengaktifkan script adsbygoogle.js

Panduan ini bersifat opsional, Anda dapat memilih untuk menerapkannya atau tetap menggunakan cara standar sesuai dengan pengaturan Adsense.

  1. Di dasbor Blogger, klik Tema.
  2. Klik ikon di sebelah tombol Sesuaikan.
  3. Klik Edit HTML, Anda akan diarahkan ke halaman pengeditan.
  4. Temukan tag penutup </head> pada kode HTML, lalu tempelkan kode di bawah ini tepat sebelum tag tersebut:
    <!--[ Adsense script ]-->
    <script type='deferjs' src='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-0000000000000000' crossorigin='anonymous'/>
  5. Ganti kode yang ditandai dengan ID Penayang Adsense Anda.
  6. Klik Simpan.

Kode di atas termasuk iklan otomatis, Anda dapat menyesuaikan format iklan yang ingin Anda tampilkan melalui dasbor Adsense. Kami juga menambahkan atribut type='deferjs' yang berguna untuk menunda pemuatan script Adsense setelah semua konten blog dimuat.

LazyAdsense untuk performa blog lebih baik (opsional)

Script Adsense merupakan salah satu penyebab utama menurunnya nilai PageSpeed Insight dan hal ini tidak dapat dihindari, yang dapat dilakukan adalah menunda pemuatan script Adsense dengan menggunakan lazyAdsense, namun iklan hanya akan muncul ketika pengguna melakukan scroll pertama atau beberapa detik setelah halaman terisi penuh. Hal ini mungkin akan mengakibatkan tingkat penayangan iklan berkurang, namun jika Anda ingin mencobanya gunakan salah satu kode di bawah ini:

LazyAdsense Dengan Scroll

Dengan skrip ini, iklan akan dimuat ketika pengguna melakukan scroll pertama pada halaman.

<!--[ Lazy Adsense by scroll ]-->
<script>/*<![CDATA[*/ let adsenseId = 'ca-pub-0000000000000000',
    Ad = document.createElement('script'),
    lazyAd = false;
window.addEventListener('scroll', () => {
    (0 != document.documentElement.scrollTop && false === lazyAd || 0 != document.body.scrollTop && !1 === lazyAd) && (! function() {
        Ad.setAttribute('crossorigin', 'anonymous'),
            Ad.async = !0,
            Ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' + adsenseId,
            document.head.appendChild(Ad);
    }(), lazyAd = true);
}, true); /*]]>*/</script>

LazyAdsense Dengan Timer

Script ini dipicu berdasarkan waktu hitung mundur dan tidak terpengaruh oleh aktivitas scroll, iklan akan terpanggil pada jumlah detik yang ditentukan.

<!--[ Lazy Adsense by timer ]-->
<script>/*<![CDATA[*/ let adsenseId = 'ca-pub-0000000000000000',
    Ad = document.createElement('script'),
    lazyAdTime = 3;
setTimeout(() => {
    Ad.setAttribute('crossorigin', 'anonymous'),
        Ad.async = !0,
        Ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' + adsenseId,
        document.head.appendChild(Ad);
}, lazyAdTime * 1000); /*]]>*/</script>

Berdasarkan script di atas, iklan akan terpicu setelah hitungan mundur 3 yang berarti 3 detik, untuk mengubahnya ubah nilai lazyAdTime = 3.

LazyAdsense Dengan Scroll dan localStorage

Sama seperti opsi sebelumnya, iklan akan dimuat ketika aktivitas scroll atau tap terdeteksi di layar namun hanya untuk kunjungan pertama kali, iklan akan ditampilkan secara asinkron jika pengguna berkunjung kembali tanpa harus menunggu aktivitas scroll atau tap lagi.

Pengaturan akan direset jika pengguna menghapus cookie browser mereka, hal ini akan sedikit meminimalisir penurunan pendapatan Adsense sambil mempertahankan skor situs di PageSpeed Insight.

<!--[ Lazy Adsense by scroll and localStorage ]-->
<script>/*<![CDATA[*/ function lazyAdLoad() {
    let adsenseId = 'ca-pub-0000000000000000',
        Ad = document.createElement('script');
    Ad.setAttribute('crossorigin', 'anonymous'), Ad.async = !0, Ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' + adsenseId, document.head.appendChild(Ad);
}
function detectLazy() {
    lazyAdLoad(), localStorage.setItem('lazyAd', 'true')
}
var lazyAdStorage = localStorage.getItem('lazyAd');
if ('true' != lazyAdStorage) {
    var e = !1,
        t = !1;
    window.addEventListener('scroll', () => {
        (0 != document.documentElement.scrollTop && !1 === e || 0 != document.body.scrollTop && !1 === e) && (detectLazy(), e = !0, t = !0)
    }, !0), window.addEventListener('click', () => {
        !1 === t && !1 === t && (detectLazy(), t = !0, e = !0)
    }, !0)
}
'true' === lazyAdStorage && lazyAdLoad(); /*]]>*/</script>

Pilih salah satu kode lazyAdsense di atas untuk digunakan, letakkan sebelum tag </body> dan ganti ca-pub-0000000000000000 dengan ID Penayang Adsense Anda.