12

I've already tried to put it as custom directive, but JS reject script tag in any strings (unterminated literal string). Also, vue-google-adsense and vue-adsense plugins doesn't work for me, because they don't get all parameters that Adsense gives, so ads becomes not responsive etc.

3
  • Have a look here, maybe it can help: stackoverflow.com/questions/6197768/… Commented Feb 17, 2019 at 11:31
  • Thank you, but I've seen it before :( Commented Feb 17, 2019 at 18:39
  • I used vue-meta and it works great for me. Article: the-koi.com/projects/… Commented Feb 23, 2022 at 13:19

2 Answers 2

13

In the index.html file, add the adsense code out of the #app :

<div id="app"></div>
<div id="divadsensedisplaynone" style="display:none;">
    <!-- put here all adsense code -->
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
        style="display:block"
        data-ad-client="ca-pub-xxxxxx"
        data-ad-slot="xxxxxx"
        data-ad-format="auto"
        data-full-width-responsive="true"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
</div>

In your main App.vue or any Vue file, add this where you want the ad to be shown (you are free to change the style) :

<div id="adsgoeshere" style="background: #1d1f29; padding-top:60px; text-align: center;" v-html="adsenseContent"></div>

In the data add :

 adsenseContent: ''

Finally, in the mounted function, add :

this.adsenseContent = document.getElementById('divadsensedisplaynone').innerHTML

And that's it ! You don't need any library.

Sign up to request clarification or add additional context in comments.

7 Comments

It throws this:adsbygoogle.js:1 Uncaught H message: "adsbygoogle.push() error: Fluid responsive ads must be at least 250px wide: availableWidth=0" :(
Try with a banner non responsive but fixed size like horizontal banner 320x50
Probably it will work, but I need responsive ad. I tried to make style of divadsensedisplaynone div equal to "visibility: hidden", and it started to work! Buuut... I had two banners - one on the top of the page, and one in place where I added it :D I am close to giving up with this because I lost at least two days on it. This is stupid
I spent a lot of time too. I don't understand why adsense JS code is so poor with many document.write() and no Vue.JS support. Sorry it doesn't work for you.
This is not the type of hack I'm looking for.
|
2

This is what I got working for me in vue3.

<template>
  <div id="ad-container"></div>
</template>
<script>

export default {
  name: 'AdComponent',
  mounted() {
    this.loadAd();
  },
  methods: {
    loadAd() {
      let adContainer = document.getElementById('ad-container'); 

      let ins = document.createElement('ins');
      ins.className = 'adsbygoogle';
      ins.style.display = 'block';
      ins.setAttribute('data-ad-client', 'ca-pub-xxxxxx');
      ins.setAttribute('data-ad-slot', 'xxxxxx');
      ins.setAttribute('data-ad-format', 'auto');
      ins.setAttribute('data-full-width-responsive', 'true');
      adContainer.appendChild(ins);


      (adsbygoogle = window.adsbygoogle || []).push({});
    }
  }
}
<script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.