Insert Google Adsense after 3rd paragraph
I am trying to work out, in nextjs (reacts) how would I insert a Google Adsense after say the 3 paragraph in the article?
basically we embed the content like this
<Card.Body dangerouslySetInnerHTML={{__html: data.news.content }}>
And somehow I want to after it is loaded to check how many paragraph and insert the ad after say 3 paragraphs.
To be able to do the above I implemented the following.
// This also gets called at build time export async function getServerSideProps({ params }) { const res = await fetch(`https://APIDOMAIN.com.au/api-access/news/${params.slug}`) const data = await res.json() const currentU = `https://APIDOMIAN.com.au/news/${params.slug}` if (!data.news || data.news == "null") { return { notFound: true, } } let paraStr = data.news.content let paraArray = paraStr.split("</p>") console.log(paraArray); let NewsContentText =""; paraArray.forEach(myFunction); function myFunction(item, index) { if (index & 1){ NewsContentText += item + '<br> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-(Google_AD_ID)" data-ad-slot="(GOOGLEID)"></ins><br>'; }else{ NewsContentText += item; } } // Pass post data to the page via props return { props: { data,NewsContentText, currentU } } }