#What's the best method to add google adsense in astro js?

4 messages · Page 1 of 1 (latest)

little maple
#

I found this component

import React, { useEffect  } from 'react';

const AdsComponent = (props) => {
    const { dataAdSlot } = props;  



    useEffect(() => {

        try {
            (window.adsbygoogle = window.adsbygoogle || []).push({});
        }

        catch (e) {

        }

    },[]);



    return (
        <>
            <ins className="adsbygoogle"
                style={{ display: "block" }}
                data-ad-client="ca-pub-XXXXXXXXXXXXXXX"
                data-ad-slot={dataAdSlot}
                data-ad-format="auto"
                data-full-width-responsive="true">
            </ins>
        </>
    );
};

export default AdsComponent;

Or just add the script tag in head?

<head>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1234567890123456" crossorigin="anonymous"></script>
This is the head of your page.
<title>Example HTML page</title>
</head>
fierce pelican
#

yep. adding it as a script tag in the head is usually the easier way

little maple
fierce pelican
#

adding is:inline or possibly also type="module" should opt-out from Astro's script bundling