How to embed google adsense in react js

前端 未结 3 1031
青春惊慌失措
青春惊慌失措 2020-12-13 01:20

I am beginner in reactjs and I want to embed google inline ads in loops. The ad is showing only at first time. When I inspect the element tag shows in loop. Can I please kno

相关标签:
3条回答
  • 2020-12-13 01:30

    Answer by @jpgbarbosa is great. I'll add better practice for Server Side Rendered React applications which have multiple pages, for scalability, I suggest you use this method to keep code base maintainable.

    export default class HomePage extends React.Component {
      componentDidMount() {
        const installGoogleAds = () => {
          const elem = document.createElement("script");
          elem.src =
            "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
          elem.async = true;
          elem.defer = true;
          document.body.insertBefore(elem, document.body.firstChild);
        };
        installGoogleAds();
        (adsbygoogle = window.adsbygoogle || []).push({});
      }
    
      render() {
        return (
          <ins className='adsbygoogle'
               style={{ display: 'block' }}
               data-ad-client='ca-pub-12121212'
               data-ad-slot='12121212'
               data-ad-format='auto' />
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-13 01:42

    This seems a duplicated question. You can find it in here. But I think it isn't 100% clear. So, I came across once with this blog post which is more clear.

    From Google you have this:

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
    </script>
    
    <ins class="adsbygoogle"
          style="display:block"
          data-ad-client="ca-pub-12121212"
          data-ad-slot="12121212"
          data-ad-format="auto"/>
    
    <script>
      (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    

    Now, in your react app:

    Include the following snippet in your index.html

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    

    Create your react component like:

    import React from 'react';
    
    export default class AdComponent extends React.Component {
      componentDidMount () {
        (window.adsbygoogle = window.adsbygoogle || []).push({});
      }
    
    render () {
        return (
            <ins className='adsbygoogle'
              style={{ display: 'block' }}
              data-ad-client='ca-pub-12121212'
              data-ad-slot='12121212'
              data-ad-format='auto' />
        );
      }
    }
    

    Now, to render it multiple times you can simply wrap the ins html tag with an iterator like map. However, I don't fully understand your need here.

    If you want to show them all at once, then do your map like this.

    If you want to randomise your ad, add a state to your component and a tick state so that it can re-render every X seconds. Check it in this SO answer

    Notes:

    1. From you google sense add, rename class attribute to className
    2. Update style attribute to be wrapped like this: style={{ display: 'block' }}
    0 讨论(0)
  • 2020-12-13 01:42

    npm install --save react-adsense

    import React from 'react';
    import AdSense from 'react-adsense';
    
    export default class AdsenseWidget extends React.Component {
     componentDidMount() {
            const installGoogleAds = () => {
              const elem = document.createElement("script");
              elem.src =
                "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
              elem.async = true;
              elem.defer = true;
              document.body.insertBefore(elem, document.body.firstChild);
            };
            installGoogleAds();
        }
    
    
    render () {
        return ( 
            <AdSense.Google
              client='ca-pub-3236992787867833'
              slot='1686195266'
              style={{ display: 'block' }}
              format='auto'
              responsive='true'
            />
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题